I'm trying to make a platformer game without tiles. I've made a collision handler to handle collision between Player and Stage(platform)and a collision checker, here's the code:
public void handleCollision(GameObject OtherObject)
{
if(this.CollidesWith(OtherObject))
{
if(OtherObject is Stage)
{
float rightEdgeDistance = OtherObject.BoundRect.X - (Position.X + this.BoundRect.Width);
float leftEdgeDistance = OtherObject.BoundRect.X + OtherObject.BoundRect.Width - Position.X;
float TopEdgeDistance = OtherObject.BoundRect.Y - (Position.Y + this.BoundRect.Height);
float BottomEdgeDistance = OtherObject.BoundRect.Y + OtherObject.BoundRect.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 = OtherObject.BoundRect.X + OtherObject.BoundRect.Width;
Velocity.X = 0;
}
else if (smallerDistance == Math.Abs(rightEdgeDistance))
{
Position.X = OtherObject.BoundRect.X - this.BoundRect.Width;
Velocity.X = 0;
}
else if (smallerDistance == Math.Abs(BottomEdgeDistance))
{
Position.Y = OtherObject.BoundRect.Y + OtherObject.BoundRect.Height;
Velocity.Y = 0;
}
else if (smallerDistance == Math.Abs(TopEdgeDistance))
{
Position.Y = OtherObject.BoundRect.Y - this.BoundRect.Height;
Velocity.Y = 0;
}
}
}
}
My collision checker:
public bool CollidesWith(GameObject OtherObject)
{
return BoundRect.Intersects(OtherObject.BoundRect);
}
Here's my inputHandler method for the Player class:
public void inputHandler(GameTime gameTime)
{
Velocity = Vector2.Zero;
if (Keyboard.GetState().IsKeyDown(Keys.W) )
{
Velocity.Y -= moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
else if (Keyboard.GetState().IsKeyDown(Keys.S) )
{
Velocity.Y += moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
else
Velocity.Y = 0;
if (Keyboard.GetState().IsKeyDown(Keys.A) )
{
Velocity.X -= moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
else if (Keyboard.GetState().IsKeyDown(Keys.D) )
{
Velocity.X += moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
else
Velocity.X = 0;
}
And here's my code for the Update method in Player:
public void Update(GameTime gameTime,List<Stage> lstOtherObject)
{
inputHandler(gameTime);
foreach (GameObject OtherObject in lstOtherObject) { handleCollision(OtherObject); }
Position += Velocity;
}
This works, if I don't continue to press the keys in the bounded direction. If I do, it will stutter in and out of the platform bounds.
Like this:
It goes a bit into the grey platform
and then it goes out of the grey platform to the intended place.
Can someone help? I want to learn, but I cant seems to figure out whats wrong with this.I tried many things, but still I can't achieve what I want, that is for the player to stop when it reaches the bounds of the platform and never going into it.Appreciate if someone can bring a clear solution and explanation to this. Thanks.