An introduction to character physics
A physics character is a physics object used to simulate game characters (people) walking, jumping, and falling.
Jolt JNI provides 2 implementations of physics characters:
-
Character
, which incorporates a dynamic rigid body, and -
CharacterVirtual
, which doesn’t require a body.
The standard physics characters make some simplifying assumptions:
-
The character remains upright even when jumping or falling.
-
Limits are placed on how steep a slope the character can climb.
When coding, be careful to distinguish the Jolt-JNI Character
class
(in the "com.github.stephengold.joltjni" package)
from the wrapper class with the same name in the "java.lang" package.
Creation
To create a physics character, you first configure a settings object, which is then passed to a constructor. Jolt JNI doesn’t keep track of characters; the app is responsible for that.
HelloCharacter is a Sport-Jolt app
that demonstrates the creation of a Character
,
followed by automated jumps.
Things to notice about the app:
-
The settings object requires a shape. In this sample app, a capsule is used.
-
To be effective, the character must be added its physics system by invoking
Character.addToPhysicsSystem()
. -
The character’s desired motion is specified by invoking
setLinearVelocity()
before each simulation step. -
The character must be explicitly updated by invoking
postSimulation()
after each simulation step. -
The
isSupported()
method tests whether the character is supported by a solid surface (as opposed to jumping or falling). -
In Sport-Jolt, characters that are jumping or falling are shown in pink, while supported characters are shown in brown.
HelloCharacterVirtual is a Sport-Jolt app
that demonstrates the creation of a CharacterVirtual
,
followed by automated jumps.
Things to notice:
-
Unlike a
Character
, aCharacterVirtual
never gets added to any physics system. -
The character must be explicitly updated by invoking
extendedUpdate()
after each simulation step.
Walking
HelloWalk demonstrates keyboard-controlled motion of a physics character. Things to notice while running the app:
-
Drag with the left mouse button to rotate the camera.
-
Press Space bar to jump.
-
Press W to walk in the camera’s forward direction.
Configuration
Body properties
CharacterSettings
allows you configure
important properties of the character’s rigid body, including:
-
friction ratio,
-
gravity factor,
-
object layer, and
-
mass.