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 6378869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:03:49+00:00 2026-05-25T02:03:49+00:00

I’ve snatched this piece of tile collision code from another game, but in the

  • 0

I’ve snatched this piece of tile collision code from another game, but in the spirit of understanding and not just stealing, I’d like to get some help understanding what exactly it does. Specifically what the function of x1, y1, x2, y2 are and why it picks start and end one tile outside the actual bounding box of the object.

...
    ...
        Int32 Tile.Size = 16;
    ...
...

...
    private Vector2 CheckTileCollision(Vector2 velocity)
    {
        // Find all the tiles we intersect with, including a layer outside.
        Int32 startx = (Int32)((Single)this.Left / Tile.Size) - 1;
        Int32 endx = (Int32)((Single)this.Right / Tile.Size) + 1;
        Int32 starty = (Int32)((Single)this.Top / Tile.Size) - 1;
        Int32 endy = (Int32)((Single)this.Bottom / Tile.Size) + 1;

        // The following are array positions, not world positions.
        Int32 x1 = -1; // Only set when there's a horizontal collision
        Int32 y1 = -1; // Only set when there's a horizontal collision
        Int32 x2 = -1; // Only set when there's a vertical collision
        Int32 y2 = -1; // Only set when there's a vertical collision

        Vector2 newVelocity = velocity;
        Vector2 nextPosition = this.position + velocity;

        for (Int32 x = startx; x <= endx; x += 1)
        {
            for (Int32 y = starty; y <= endy; y += 1)
            {
                if (realm.TileAt(x, y).IsSolid) // We only collide with solid tiles.
                {
                    // The world coordinates of a tile.
                    Vector2 tilePosition = new Vector2(x * Tile.Size, y * Tile.Size);

                    // Check if we intersect the tile.
                    if (nextPosition.X + this.Width > tilePosition.X &&
                        nextPosition.X < tilePosition.X + Tile.Size &&
                        nextPosition.Y + this.Height > tilePosition.Y &&
                        nextPosition.Y < tilePosition.Y + Tile.Size)
                    {
                        realm.Tiles[x, y].Debug = true;
                        if (this.Bottom <= tilePosition.Y) // if the bottom is above or touching the tile top
                        {
                            x2 = x; // x2 is set to current tile array position, not world position
                            y2 = y; // y2 is set to current tile array position, not world position

                            if (x2 != x1)
                                newVelocity.Y = tilePosition.Y - (position.Y + this.Height);
                        }
                        else if (this.Right <= tilePosition.X) // if the right side is to the left or touching the tile left
                        {
                            x1 = x; // x1 is set to current tile array position, not world position
                            y1 = y; // y1 is set to current tile array position, not world position

                            if (y1 != y2) 
                                newVelocity.X = tilePosition.X - (position.X + this.Width);
                            if (x2 == x1)
                                newVelocity.Y = this.Velocity.Y;
                        }
                        else if (this.Left >= tilePosition.X + Tile.Size)// if the left side is to the right or touching the tile right
                        {
                            x1 = x; // x1 is set to current tile array position, not world position
                            y1 = y; // y1 is set to current tile array position, not world position

                            if (y1 != y2)
                                newVelocity.X = (tilePosition.X + Tile.Size) - position.X;
                            if (x2 == x1)
                                newVelocity.Y = this.Velocity.Y;
                        }
                        else if (this.Top >= tilePosition.Y + Tile.Size) // if the top is below or touching the tile bottom
                        {
                            x2 = x; // x2 is set to current tile array position, not world position
                            y2 = y; // y2 is set to current tile array position, not world position

                            newVelocity.Y = (tilePosition.Y + Tile.Size) - position.Y;
                            if (y2 == y1)
                                newVelocity.X = this.Velocity.X;
                        }
                    }
                }
            }
        }
    return newVelocity;
}
...

EDIT

Added a few more comments to the code, and fixed up the ifs.

  • 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-25T02:03:50+00:00Added an answer on May 25, 2026 at 2:03 am

    It picks start and end tiles outside of the bounding box of the object because the object could be moving, and has a velocity.

    x1, y1, x2, y2 are the outside boundaries of the object, after it has resolved collisions.

    The function returns a new velocity of the object that will prevent the object from colliding.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
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
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,

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.