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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:27:40+00:00 2026-05-19T13:27:40+00:00

So I got my collision detection working, but for some reason the player seems

  • 0

So I got my collision detection working, but for some reason the player seems to go intro the ground 1 frame then jump out of it in the next frame and then fall down as its supposed to.

Code I use for the collision detection

        void DoCollisions()
    {
        onGround = false;
        Position.Y += Velocity.Y;
        Vector2 tileCollision = GetTileCollision();
        if (tileCollision.X != -1 || tileCollision.Y != -1)
        {
            onGround = true;
            Vector2 collisionDepth = CollisionRectangle.DepthIntersection(
                new Rectangle(
                    tileCollision.X * World.tileEngine.TileWidth,
                    tileCollision.Y * World.tileEngine.TileHeight,
                    World.tileEngine.TileWidth,
                    World.tileEngine.TileHeight
                )
            );
            Position.Y -= Velocity.Y;
            Velocity.Y = 0;
            Position.Y += collisionDepth.Y;

        }

        Position.X += Velocity.X;
        tileCollision = GetTileCollision();
        if (tileCollision.X != -1 || tileCollision.Y != -1)
        {
            Vector2 collisionDepth = CollisionRectangle.DepthIntersection(
                new Rectangle(
                    tileCollision.X * World.tileEngine.TileWidth,
                    tileCollision.Y * World.tileEngine.TileHeight,
                    World.tileEngine.TileWidth,
                    World.tileEngine.TileHeight
                )
            );
            Position.X -= Velocity.X;
            Velocity.X = 0;
            Position.X += collisionDepth.X;

        }
    }

    Vector2 GetTileCollision()
    {
        int topLeftTileX = (int)(CollisionRectangle.TopLeft.X / World.tileEngine.TileWidth);
        int topLeftTileY = (int)(CollisionRectangle.TopLeft.Y / World.tileEngine.TileHeight);
        int BottomRightTileX = (int)(CollisionRectangle.DownRight.X / World.tileEngine.TileWidth);
        int BottomRightTileY = (int)(CollisionRectangle.DownRight.Y / World.tileEngine.TileHeight);

        for (int i = topLeftTileX; i <= BottomRightTileX; i++)
        {
            for (int j = topLeftTileY; j <= BottomRightTileY; j++)
            {
                if (World.tileEngine.TileIsSolid(i, j))
                {
                    return new Vector2(i,j);
                }
            }
        }

        return new Vector2(-1,-1);
    }
  • 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-19T13:27:41+00:00Added an answer on May 19, 2026 at 1:27 pm

    Using just the Y direction as an example, at first glance it seems that you are adjusting Y twice, once before the collision check, then when you get a collision you revert the Y and push the object out by collisionDepth

    You should probably not be reverting the Y, if the collisionDepth.Y indicates that you are intersecting the ground you should just push your object back by the depth amount, which would put you flush against the ground.

            if (tileCollision.X != -1 || tileCollision.Y != -1)
            {
                onGround = true;
                Vector2 collisionDepth = CollisionRectangle.DepthIntersection(
                    new Rectangle(
                        tileCollision.X * World.tileEngine.TileWidth,
                        tileCollision.Y * World.tileEngine.TileHeight,
                        World.tileEngine.TileWidth,
                        World.tileEngine.TileHeight
                    )
                );
                Velocity.Y = 0;
                // Depending on your coordinate system this should
                // be either + or - collisionDepth.Y
                Position.Y += collisionDepth.Y;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am stuck trying to figure out how to alter my collision detection to
I have working code below for collision of two circles, but now I have
I'm using Chipmunk physics and want to do collision detection to find out when
Got myself in a bit of a pickle here ... working on a CMS
Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)
for the past two weeks I've been working on coming up with a collision
I am learning JQuery, and I have checked out that JQUery has got a
Im trying out JBox2d. I´ve been through a couple of tutorials, and got it
I've been working on a simple 2D platformer engine. So far I've got a
Description: I've got a project which is nearly finished now, but I've noticed the

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.