Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7658799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:14:55+00:00 2026-05-31T13:14:55+00:00

I’ve got quite a lot of code here, but it’s relatively straightforward. This is

  • 0

I’ve got quite a lot of code here, but it’s relatively straightforward.

This is all snippets from different classes, all references are right, but I think I’ve done a math-based error somewhere and I can’t find it. It always finds a collision on the y axis a pixel before it should. I haven’t tried it with different X axis positions but it seems to fall past blocks next to it fine.

The struct “mapSection” just contains two Vector2s- A top-left block and bottom-left block coordinate.

tileManager.def_ts is the default tile width and height (32). The player’s size is 32×64.

The toWorldSpace function does nothing right now other than return so that’s not the problem.

When I say block coordinate I mean which index the block is in the tile array (Ex 0, 0 is the first block, 0, 1 is the second block on the Y axis, 1, 3 is 1 block in on the X axis and 3 on the Y axis, I do not mean actual pixels.)

From tile engine class:

    public mapSection toMapMinMax(Vector2 position, Vector2 size)
    {
        position = toWorldSpace(position);

        position.X = (float)Math.Floor(position.X / tileManager.def_ts);
        position.Y = (float)Math.Floor(position.Y / tileManager.def_ts);

        size.X = (float)Math.Floor(size.X / tileManager.def_ts);
        size.Y = (float)Math.Floor(size.Y / tileManager.def_ts);

        return new mapSection(position, position + size);
    }

    public bool collision(Vector2 screenPosition, Vector2 size)
    {
        mapSection mapCollisionPossibilities = toMapMinMax(screenPosition, size);

        for (int y = (int)mapCollisionPossibilities.topLeft.Y; y <= mapCollisionPossibilities.bottomRight.Y; y++)
        {
            for (int x = (int)mapCollisionPossibilities.topLeft.X; x <= mapCollisionPossibilities.bottomRight.X; x++)
            {
                if (x >= 0 && y >= 0 && y < tiles.Count && x < tiles[y].Count)
                {
                    if (tileManager.tileTypes[tiles[y][x]].collideable == true)
                    {
                        return true;
                    }
                }
            }
        }

        return false;
    }

And this is the code from the player class:

        if (!tEngine.collision(position + new Vector2(0, 1), new Vector2(32, 64)))
        {
            position.Y += 1;
        }

I add “Vector2(0, 1)” because I want to see if there’s a collision a pixel further down; so that he falls until he hits something. It’s very basic right now but it’s only to test the collision engine, which isn’t working.

enter image description here

There’s a picture of the error. You can see the player is a pixel too high.

In the picture, “X:” is the top-left block coordinate on X axis, “X2:” is the bottom-right block coordinate on the X axis, and same with “Y:” and “Y2: except Y axis. They’re read from the mapSection directly.

If anyone can notice why this is happening, it would be massively appreciated.

Thanks.

If you cannot understand any section of the code just post in the comments and I’ll be happy to explain, or if you think I’ve been a bit too unspecific in some area of this post.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:14:56+00:00Added an answer on May 31, 2026 at 1:14 pm

    EDIT: For the tile coordinates issue, your toMapMinMax code should be more like this:

    EDIT2: have subtracted (1, 1) from bottomRight, since it is a size we are adding.

    public mapSection toMapMinMax(Vector2 position, Vector2 size)
    {
        Vector2 topLeft = position;
        Vector2 bottomRight = position + size - new Vector2(1, 1);
    
        topLeft.X = (float)Math.Floor(topLeft.X / tileManager.def_ts);
        topLeft.Y = (float)Math.Floor(topLeft.Y / tileManager.def_ts);
        bottomRight.X = (float)Math.Floor(bottomRight.X / tileManager.def_ts);
        bottomRight.Y = (float)Math.Floor(bottomRight.Y / tileManager.def_ts);
    
        return new mapSection(topLeft, bottomRight);
    }
    

    Also, I was wrong in my above comment; you do want <= signs in your two for loops, because most of the time you will be checking 6 tiles.

    for the off-by-one-pixel issue:

    In order for you to see the character off by some amount of pixels, the draw code and the collision code must be different. If they were identical, for example if they were both off by 15 pixels (you collide 15 pixels too early, but you are also drawing 15 pixels ahead), you wouldn’t see any change.

    The 1 pixel gap indicates a 1 pixel difference between the draw coordinate calculation and the collision coordinate calculation. This 1 pixel difference is most likely caused by differences in rounding, probably that you are calling Math.Floor in the collision code, but are not rounding the coordinates in the draw code. (I would guess you are probably just passing the position Vector2 straight to the SpriteBatch.Draw method).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.