Jump to content
  • Advertisement
Sign in to follow this  
poligon

XNA collision handling problem

This topic is 2692 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all!
Sorry for my bad English :(
I tryed to make [color="#ff0000"]collision detection system, but I don't know how to [color="#ff0000"]stop my player if collision.
This is my code:




protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
Rectangle playerr = new Rectangle((int)playerv.X, (int)playerv.Y, player.Width, player.Height);
Rectangle playern = new Rectangle((int)playerpos.X, (int)playerpos.Y, player.Width, player.Height);
Rectangle obr = new Rectangle((int)obv.X, (int)obv.Y, ob.Width, ob.Height);

KeyboardState keyboard = Keyboard.GetState();

if (keyboard.IsKeyDown(Keys.S))
{
playerpos.Y = playerv.Y + 1;
}
if (keyboard.IsKeyDown(Keys.D))
{
playerpos.X = playerv.X + 1;
}

if (keyboard.IsKeyDown(Keys.A))
{
playerpos.X = playerv.X - 1;
}
if (keyboard.IsKeyDown(Keys.W))
{
playerpos.Y = playerv.Y - 1;
}

if (!playern.Intersects(obr))
{
playerv = playerpos;
}
base.Update(gameTime);
}



The problem:

if my player object is come from the Left side to "obr" : the player is cant move to UP, or Down, Just to right.

Share this post


Link to post
Share on other sites
Advertisement
if (!playern.Intersects(obr))
{
playerv = playerpos;
}


That code is checking the last frames movement instead of this frames you are not updating playerern.
Well you are, but you are updating it BEFORE you actual add the movement.

Based on what you are doing you would want to check playerpos against obr for collisions.

Try moveing it above the intersect like below:

Rectangle playern = new Rectangle((int)playerpos.X, (int)playerpos.Y, player.Width, player.Height);
if (!playern.Intersects(obr))
{
playerv = playerpos;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!