Search code examples
javaandroidlibgdx2dgame-physics

2D Game Physics: Hitting a platform actor won't stop


If in a game, the actor was jumping if he was to hit a platform. What would i need to set the velocity/position/gravity in order to make him stop on the platform? Any pointers on game physics would be great. I think it's something like this?

Vector2 position ?   ;
Vector2 gravity ?   ;

final int Jump velocity = 11;
Vector2 Gravity(0, -12); 

public void hitPlatform () {
            velocity.y = ?
            state = IDLE;
            stateTime = 0;
        }

This is the way i'm trying with velocity.y = 0; in the hitPlatform() and velocity adds gravity and position adds velocity in the update method but he just falls through the platform.


Solution

  • Just like apnorton said in the comments, "set your acceleration to 0 as well as your velocity" While the user is on the platform make sure the velocity does not change or you can just make the platform a solid and anything which touches it on the top will cancel its movement and set it's velocity to 0.

    Basically, just don't let the user go through the platform.

    Where you said "and velocity adds gravity and position adds velocity in the update method but he just falls through the platform", you could make a method called isOnGround() or isOnPlatform() and cancel the gravity and velocity stuff in your update method.