Hello,
So I have two boxes, a blue box and a red box. The blue represents a player and the red box represent a wall. What I'm trying to do is, when the blue collide with the red box, the blue box should stop moving. This is working for the most part when the blue box collide with the red box from the left side. However its not working from the right side. I don't know how to fix it. Here is a video showing the problem.
and here is my code in the update function
if (PlayerRectangle.Intersects(BoxRectangle))
{
if (Position.X + 32 >= BoxRectangle.X )
Position.X = BoxRectangle.X - 32;
else if (Position.X <= BoxRectangle.X + BoxRectangle.Width)
Position.X = BoxRectangle.X + BoxRectangle.Width;
}
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.W))
Position.Y -= (float)(Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
if (keyboardState.IsKeyDown(Keys.S))
Position.Y += (float)(Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
if (keyboardState.IsKeyDown(Keys.D))
Position.X += (float)(Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
if (keyboardState.IsKeyDown(Keys.A))
Position.X -= (float)(Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
PlayerRectangle = new Rectangle((int)Position.X, (int)Position.Y, (int)32, (int)32);
--------------------------------------------------------------------------------------------------------------------------------
[Update 22/8/2014]
Ok so I updated my code and I used this method to detect collision. But I still can not get rid of the stutter problem where the player goes into the wall if the user holds the keyboard key. Also there is another problem where if the player collide from the upper or bottom side of wall and then the user presses the right or left key, the player will jump in the opposite direction instead of sliding to the right or left.
here is the code:-
Vector2 Position;
Rectangle PlayerRectangle, BoxRectangle;
float Speed = 0.25f;
enum Direction { Up, Right, Down, Left };
Direction direction;
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Up))
{
Position.Y -= (float)(Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
direction = Direction.Up;
}
if (keyboardState.IsKeyDown(Keys.Down))
{
Position.Y += (float)(Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
direction = Direction.Down;
}
if (keyboardState.IsKeyDown(Keys.Right))
{
Position.X += (float)(Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
direction = Direction.Right;
}
if (keyboardState.IsKeyDown(Keys.Left))
{
Position.X -= (float)(Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
direction = Direction.Left;
}
if (PlayerRectangle.Intersects(BoxRectangle))
{
if (direction == Direction.Right)
Position.X = BoxRectangle.Left - PlayerRectangle.Width;
else if (direction == Direction.Left)
Position.X = BoxRectangle.Right;
if (direction == Direction.Down)
Position.Y = BoxRectangle.Top - PlayerRectangle.Height;
else if (direction == Direction.Up)
Position.Y = BoxRectangle.Bottom;
}
PlayerRectangle = new Rectangle((int)Position.X, (int)Position.Y, (int)32, (int)32);
base.Update(gameTime);
}
and here is the video
--------------------------------------------------------------------------------------------------------------------------------
[Update 23/8/2014]
finally I fixed all my issues, I just wanted to share my final code.
Vector2 Velocity, Position;
Rectangle PlayerRectangle, BoxRectangle;
float Speed = 0.01f;
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Up))
Velocity.Y -= (float)(Speed);
else if (keyboardState.IsKeyDown(Keys.Down))
Velocity.Y += (float)(Speed);
else
Velocity.Y = 0;
if (keyboardState.IsKeyDown(Keys.Right))
Velocity.X += (float)(Speed );
else if (keyboardState.IsKeyDown(Keys.Left))
Velocity.X -= (float)(Speed);
else
Velocity.X = 0;
if (PlayerRectangle.Intersects(BoxRectangle))
{
float rightEdgeDistance = BoxRectangle.X - (Position.X + PlayerRectangle.Width);
float leftEdgeDistance = BoxRectangle.X + BoxRectangle.Width - Position.X;
float TopEdgeDistance = BoxRectangle.Y - (Position.Y + PlayerRectangle.Height);
float BottomEdgeDistance = BoxRectangle.Y + BoxRectangle.Height - Position.Y;
float Left_Right_SmallerDistance = Math.Min(Math.Abs(rightEdgeDistance), Math.Abs(leftEdgeDistance));
float Top_Bottom_SmallerDistance = Math.Min(Math.Abs(TopEdgeDistance), Math.Abs(BottomEdgeDistance));
float smallerDistance = Math.Min(Math.Abs(Left_Right_SmallerDistance), Math.Abs(Top_Bottom_SmallerDistance));
if (smallerDistance == Math.Abs(leftEdgeDistance))
{
Position.X = BoxRectangle.X + BoxRectangle.Width;
Velocity.X = 0;
}
else if (smallerDistance == Math.Abs(rightEdgeDistance))
{
Position.X = BoxRectangle.X - PlayerRectangle.Width;
Velocity.X = 0;
}
else if (smallerDistance == Math.Abs(BottomEdgeDistance))
{
Position.Y = BoxRectangle.Y + BoxRectangle.Height;
Velocity.Y = 0;
}
else if (smallerDistance == Math.Abs(TopEdgeDistance))
{
Position.Y = BoxRectangle.Y - PlayerRectangle.Height;
Velocity.Y = 0;
}
}
Position += new Vector2((float)(Velocity.X * gameTime.ElapsedGameTime.TotalMilliseconds), (float)(Velocity.Y * gameTime.ElapsedGameTime.TotalMilliseconds));
PlayerRectangle = new Rectangle((int)Position.X, (int)Position.Y, (int)32, (int)32);
base.Update(gameTime);
}