1

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.

BradleyIW
  • 1,338
  • 2
  • 20
  • 37
  • 1
    When you hit a platform, you want to set your *acceleration* to 0 as well as your velocity. Otherwise, you'll just momentarily freeze and fall right through. – apnorton Apr 19 '15 at 21:17
  • i havent got an accel vector defined anywhere else, would that need to be applied anywhere within the jump or just on the platform hit? – BradleyIW Apr 19 '15 at 21:19
  • you've just solved days of hassle for me, I can't believe it was so simple. I didn't change the gravity either. I added the acceleration and changed it for jump velocity. I get everything now from your comment alone. Thankyou so much. Ill accept your answer if you post one, you deserve the rep. – BradleyIW Apr 19 '15 at 22:43

2 Answers2

4

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.

Forseth11
  • 1,418
  • 1
  • 12
  • 21
  • sorted it by setting when isGrounded() gravity, velocity and accel on the y direction = 0; and back to normal when he isn't; also sorted collision detection from the answer in the comments before. + 1 for expansion on comments and clear explanation (also effort of course). – BradleyIW Apr 19 '15 at 22:41
1

When you hit a platform, you want to set your acceleration to 0 as well as your velocity. Otherwise, you'll just momentarily freeze and fall right through.

Comment converted to answer per OP request.

apnorton
  • 2,430
  • 1
  • 19
  • 34