2 Replies - 939 Views - Last Post: 18 June 2012 - 11:04 AM

#1 CosminNegoita  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-May 12

2D Platform game collision acting weird

Posted 07 May 2012 - 08:37 PM

First of all, I have to say that I'm new around here and this is my first question regarding game development. I've started using XNA almost 2 weeks ago, and as a first project, I started working on a 2D platformer.

Everything I've done till now kinda works as intended, but with some little bugs which I can't think of a solution to fix them.

First, let me show you this block of code where I apply my gravity and a little function for collision detection:

// Apply gravity
if (isInAir == true)
{
    position.Y += velocity.Y * curTime;
    velocity.Y += gravity;
}
else
{
    position.Y = tilePosY - texture.Height;
    velocity.Y = 0f;
}
if (position.Y > 400) position.Y = -16;

// Check for tile collision
CheckCollisions();



Obviously, these are located in a Player class, in the Update method. Now, follows the CheckCollisions() function which is separate just to keep the code in the Update cleaner:

public void CheckCollisions()
{
    foreach (Tile t in Game1.tiles)
    {
        if (recBot.Intersects(t.rectangle))
        {
            colBot = true;
            tilePosY = t.position.Y;
        }
        if (recRight.Intersects(t.rectangle))
        {
            colRight = true;
            tilePosX = t.position.X - texture.Width;
        }
        if (recLeft.Intersects(t.rectangle))
        {
            colLeft = true;
            tilePosX = t.position.X + t.texture.Width;
        }
    }
}



The Update() method looks something like this, with some little modifications to shorten it:

public void Update(GameTime gameTime)
{
    float curTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
    position += velocity * curTime;

    // The movement
    PlayerMovement();

    //Create the rectangles - left and right rectangles removed
    recBot = new Rectangle((int)position.X, (int)position.Y + texture.Height, texture.Width, 4);

    // Set the collisions to false
    CollisionFalse();

    // Apply gravity - the first code block I showed you
  
    // Check for tile collision
    CheckCollisions();

    // Check if the character is in air based on collision
    CheckInAir();
}



Now the problem is, when I fall from a platform to the bottom-most ground, the sprite gets stuck and it's looking like it's going up and down 1 px in an infinite loop. You can download the source and run a quick test to see what I mean. It seems that the velocity on Y axis is too high, but dunno how to fix it to really stop when collision is detected.

Download the source code (my server, direct download)

Looking forward to see some tips from experts!

Is This A Good Question/Topic? 0
  • +

Replies To: 2D Platform game collision acting weird

#2 DanielLeone  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 177
  • Joined: 04-February 12

Re: 2D Platform game collision acting weird

Posted 08 May 2012 - 05:08 AM

Do you mean when when you fall from the sky to the ground continuously, and then the sprite goes sort on feint, and won't stop falling, even if you move it to the side?

If that is the case, then it's because your velocity is constantly increasing, because the elapsed time is as well. Therefor the sprite is eventually moving so fast that is 'skips' touching the ground completely.

Say your time is like 100, and your velocity is like 50, just as an example. Then your position is going to increment something like 5000 pixels. That is going to skip the ground tiles because if you move 5000 pixels, it only checks the collision at that point, and then move back to -16 on the the Y.

That probably made absolutely no sense, but I gave it a go ;).

That may not be your problem, so you may have to explain it a bit more.

Hope I helped somewhat,
Daniel,

This post has been edited by DanielLeone: 08 May 2012 - 05:15 AM

Was This Post Helpful? 1
  • +
  • -

#3 TheBroodian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-December 11

Re: 2D Platform game collision acting weird

Posted 18 June 2012 - 11:04 AM

After reading your problem a bit, As Daniel mentioned, you may be experiencing tunneling (when your character is traveling so quickly that he passes right through solid objects without the game ever checking for collision), but that wouldn't exactly seem to be the case, because this code...

if (position.Y > 400) position.Y = -16;


... would make it seem like if your character were to pass through the floor he'd eventually end up somewhere above screen level.

My next thought is that somehow your bottom-most "ground" (as you call it) probably isn't returning a proper "tilePosY" value. Which may not place your character in a proper position above the ground, which may cause him to behave the way he's behaving. That's my best guess.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1