PirateHearts

Jumping physics pseudocode

Feb 9th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. // Member variables
  2. float JumpUpVelocity;
  3. float GravDownAcceleration;
  4. vector3 Position;
  5. vector3 Velocity;
  6. vector3 Acceleration;
  7.  
  8. void CalcParameters(float InFootSpeed, float InJumpHeight, float InJumpWidthToPeak)
  9. {
  10. JumpUpVelocity = 2.0f * InJumpHeight * InFootSpeed / InJumpWidthToPeak;
  11. GravDownAcceleration = -2.0f * InJumpHeight * InFootSpeed * InFootSpeed / (InJumpWidthToPeak * InJumpWidthToPeak);
  12.  
  13. // Assumes +Z is up, swizzle as necessary.
  14. Acceleration = vector3(0, 0, -GravDownAcceleration);
  15. };
  16.  
  17. void Jump()
  18. {
  19. // TODO: Make sure we're standing on the ground unless we want to allow air jumping.
  20.  
  21. // Assumes +Z is up, swizzle as necessary.
  22. // Can't change X or Y unless we deliberately want to alter lateral movement when jumping.
  23. Velocity.z = JumpUpVelocity;
  24. }
  25.  
  26. void Update(float DeltaTime)
  27. {
  28. // Velocity Verlet integration given the simple case of constant acceleration.
  29. // This should run once per frame.
  30. Position = Position + ((Velocity * DeltaTime) + (Acceleration * DeltaTime * DeltaTime * 0.5f));
  31. Velocity = Velocity + (Acceleration * DeltaTime);
  32. }
Add Comment
Please, Sign In to add comment