-1

I have some questions for correctly implementing a system that add gravity to my player while still detecting and correcting collision with the floor. I understand the basic math for the AABB and adding gravity vector to the players velocity vector then adding it to the position. For some reason when I try the player stops for the first few frames then eventually the velocity builds up to be more than the tile width and it goes through. I guess my real question is how to properly implement the gravity with the collision. Do I make gravity vector 0, 0 when it hits the floor or when I am just standing on the floor my collision detection will constantly move me out of the floor? I really don't know the flow of the logic with this situation.

Edit 1: I am trying to make the simple engine myself to gain experience with programming. I kind of solved the one problem, I now can stop on the floor. My current logic is: add gravity to velocity, check for collision, adjust the velocity by the offset of the collision, if any, add velocity to the position then set velocity to 0.

m_velocity += m_gravity;

glm::vec2 mtd = collideWithObject(objectVector);

m_velocity -= mtd;

m_position += m_velocity;

m_velocity.x = 0.0f;
m_velocity.y = 0.0f;

It works but I am not sure if that is the flow, I guess I just want to know what is the most common way to implement this.

Dewie102
  • 1
  • 3

2 Answers2

0

For some reason when I try the player stops for the first few frames then eventually the velocity builds up to be more than the tile width and it goes through.

If you are applying physics in increments - such as once every frame - you must account for the infinite increments that exist between each of those points.

For a simple "plane collision", like your floor, don't test if your current AABB collides with the floor. Create the smallest AABB that includes your current AABB and the last AABB. If that box collides, you've collided with the plane.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
0

as @MorphingDragon mentioned, you should consider Box2D, many games use Box2D.

For your question "Do I make gravity vector 0, 0 when it hits the floor or when I am just standing on the floor my collision detection will constantly move me out of the floor? I really don't know the flow of the logic with this situation.". I just assume you know Verlet integration, for calculate your new position, you don't need to change your gravity, [as you know, gravity is everywhere on earth, or you want to do a game in space, ;)] you just need to handle the collision detection logic as "when collision happens, just make the collision point satisfy my constraints", and your constraint should be "all point should not be placed under the floor, otherwise just move it and make it sit on the floor".

The collection detection and constraint solver theory should be very common in modern physical engine and easy to understand and implement.

Hope this help.

Thanks An

Yan An
  • 459
  • 2
  • 10