Global Gravity Flip and Jumping

The next part of the physics is for the player to have an ability that can invert the world’s gravity along with jumping in the correct direction.

Gravity Invert === To start with a gravity direction int was put into the Game Scene’s header file, this value was then passed into every object in the scene that had a physics component. This value was used to determine the direction of the weight of the object which was used to apply gravity, moving the sprite down the screen unless it was in contact with something.

When the user presses q and has the gravity invert ability then a invertGravity function in the player’s physics component is called which takes the gravity direction by ref and sets it to itself * -1. This then inverts it and changes the gravity direction of everything in the scene.

 

Jumping

When the user presses space to jump a jump count float is set to 1 in the physics component. The y position of the player is then moved by the -gravity direction * jump count * jump force. Then whilst each frame of the game where the jump count > 0 it is reduced by delta time thus reducing the amount the player is moved by the jump. Eventually the gravity force still acting on the player will be stronger than the amount the player is jumping by and the player will fall to the ground.

Previous
Previous

Moving Enemies

Next
Next

Collision Detection