An introduction to character physics
A physics character is a collision object used to simulate game characters (people) walking, jumping, and falling.
The standard physics character makes some simplifying assumptions:
-
The character’s shape must be convex.
-
The character remains upright even when jumping or falling: no sitting, squatting, ducking, or lying down.
-
Limits are placed on:
-
how steep a slope the character can climb,
-
how high a step the character can climb, and
-
how fast the character can fall.
-
Like a dynamic rigid body, a physics character has a collision shape, a location, and a gravity vector, along with velocities and damping parameters. It also has an option for contact response, which is enabled by default.
However, a physics character has no concept of mass, and its motion is controlled very differently from a dynamic body. Instead of applying forces or directly updating the character’s velocities, the app should specify when and how the character should walk and jump.
Creation
You can create a character by invoking the
PhysicsCharacter
constructor.
This allows you to specify its collision shape and step height.
HelloCharacter (also in Kotlin) is a SPORT app that demonstrates the creation of a character, followed by automated jumps. Things to notice about the app:
-
The constructor requires a convex shape. In this app, a capsule is used.
-
The
onGround()
method tests whether the character is supported by a solid surface (as opposed to jumping or falling). -
In SPORT, characters that are jumping or falling are shown in pink, while characters that are on ground are shown in brown.
Walking
A character’s walking motion is controlled by its walk direction vector.
During each simulation step, the horizontal component of the walk direction
gets added to the character’s location.
To stop the character from walking, invoke setWalkDirection(Vector3f.ZERO)
.
Despite its name, the walk direction need not be a unit vector. |
HelloWalk (also in Kotlin) demonstrates keyboard-controlled motion of a physics character. Things to notice while running the app:
-
Move the mouse to turn the camera.
-
Press Space bar to jump.
-
Press W to walk in the camera’s forward direction.
-
Some of the mountains have slopes too steep for the character to climb.
Configuration
Gravity and up direction
The up direction of a physics character is a unit vector that points in the direction opposite its gravity vector. By default, the up direction is (0,1,0) and the gravity vector is (0,-29.4,0).
A character’s default gravity is 3x stronger
than the default gravity of a PhysicsSpace .
|
Unlike the gravity of a rigid body, a character’s gravity is never overridden by any physics space. |
To alter a character’s gravity vector,
use character.setGravity(Vector3f)
.
Altering the gravity vector automatically updates the character’s up direction.
To alter the magnitude of a character’s gravity
(without changing its direction) use character.setGravity(float)
.
To alter a character’s up direction, use character.setUp(Vector3f)
.
Altering the up direction automatically updates the gravity vector.
Jump speed
If a character jumps in its up direction, its predicted rise time and jump height are determined by its gravity and jump speed. Roughly speaking:
float g = character.getGravity(null).length();
float v0 = character.getJumpSpeed();
float riseSeconds = v0 / g;
float jumpHeight = v0 * v0 / (2f * g);
The default jump speed is 10 psu per second.
To alter a character’s jump speed, use character.setJumpSpeed(float)
.
Fall speed
Fall speed limits the speed of a falling character. To be realistic, it should be larger than the character’s jump speed.
The default fall speed is 55 psu per second.
To alter a character’s fall speed, use character.setFallSpeed(float)
.
Step height
Step height limits how high a step the character can climb. To be realistic, it should be less than the character’s height.
A character’s initial step height is set by the constructor.
To alter it, use character.setStepHeight(float)
.