public class BetterCharacterControl extends AbstractPhysicsControl implements PhysicsTickListener, JmeCloneable
A rigid body with cylinder collision shape is used and its velocity is set continuously. A ray test is used to test whether the character is on the ground.
The character keeps their own local coordinate system which adapts based on the gravity working on the character so they will always stand upright.
Motion in the local X-Z plane is damped.
This class is shared between JBullet and Native Bullet.
Modifier and Type | Field and Description |
---|---|
protected boolean |
ducked |
protected float |
duckedFactor
relative height when ducked (1=full height)
|
protected float |
height |
protected boolean |
jump |
protected Vector3f |
jumpForce |
protected Vector3f |
localForward
Local absolute z-forward direction, derived from gravity and UNIT_Z,
updated continuously when gravity changes.
|
protected Quaternion |
localForwardRotation
Local z-forward quaternion for the "local absolute" z-forward direction.
|
protected Vector3f |
localLeft
Local left direction, derived from up and forward.
|
protected Vector3f |
localUp
local up direction, derived from gravity
|
protected Vector3f |
location
spatial location, corresponds to RigidBody location.
|
protected static java.util.logging.Logger |
logger |
protected float |
mass
mass of this character (>0)
|
protected boolean |
onGround |
protected float |
physicsDamping
X-Z motion damping factor (0→no damping, 1=no external forces,
default=0.9)
|
protected float |
radius |
protected PhysicsRigidBody |
rigidBody |
protected Vector3f |
rotatedViewDirection |
protected Quaternion |
rotation
spatial rotation, a Z-forward rotation based on the view direction and
local X-Z plane.
|
protected Vector3f |
scale |
protected Vector3f |
velocity |
protected Vector3f |
viewDirection
a Z-forward vector based on the view direction and the local X-Z plane.
|
protected Vector3f |
walkDirection |
protected boolean |
wantToUnDuck |
added, applyLocal, enabled, space, spatial
Modifier | Constructor and Description |
---|---|
protected |
BetterCharacterControl()
No-argument constructor needed by SavableClassUtil.
|
|
BetterCharacterControl(float radius,
float height,
float mass)
Instantiate an enabled control with the specified properties.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addPhysics(PhysicsSpace space)
Add all managed physics objects to the specified space.
|
protected void |
calculateNewForward(Quaternion rotation,
Vector3f direction,
Vector3f worldUpVector)
This method works similar to Camera.lookAt but where lookAt sets the
priority on the direction, this method sets the priority on the up vector
so that the result direction vector and rotation is guaranteed to be
perpendicular to the up vector.
|
protected boolean |
checkCanUnDuck()
This checks if the character can go from ducked to unducked state by
doing a ray test.
|
protected void |
checkOnGround()
Test whether the character is on the ground, by means of a ray test.
|
protected void |
createSpatialData(Spatial spat)
Create spatial-dependent data.
|
float |
getDuckedFactor()
Read the height multiplier for ducking.
|
protected float |
getFinalHeight()
Calculate the character's scaled height.
|
protected float |
getFinalRadius()
Calculate the character's scaled radius.
|
Vector3f |
getGravity()
Copy the character's gravity vector.
|
Vector3f |
getGravity(Vector3f store)
Copy the character's gravity vector.
|
Vector3f |
getJumpForce()
Access the jump force.
|
float |
getPhysicsDamping()
Read how much motion in the local X-Z plane is damped.
|
protected CollisionShape |
getShape()
Create a collision shape based on the scale parameter.
|
Vector3f |
getVelocity()
Access the character's linear velocity in physics-space coordinates.
|
Vector3f |
getViewDirection()
Access the view direction.
|
Vector3f |
getWalkDirection()
Read the walk velocity.
|
boolean |
isDucked()
Check if the character is ducking, either due to user input or due to
unducking being impossible at the moment (obstacle above).
|
boolean |
isOnGround()
Test whether the character is supported.
|
java.lang.Object |
jmeClone()
Create a shallow clone for the JME cloner.
|
void |
jump()
Makes the character jump with the set jump force.
|
void |
physicsTick(PhysicsSpace space,
float tpf)
Callback from Bullet, invoked just after the physics has been stepped.
|
void |
prePhysicsTick(PhysicsSpace space,
float tpf)
Callback from Bullet, invoked just before the physics is stepped.
|
void |
read(JmeImporter im)
De-serialize this control, for example when loading from a J3O file.
|
protected void |
removePhysics(PhysicsSpace space)
Remove all managed physics objects from the specified space.
|
protected void |
removeSpatialData(Spatial spat)
Destroy spatial-dependent data.
|
void |
render(RenderManager rm,
ViewPort vp)
Render this control.
|
void |
resetForward(Vector3f vec)
Realign the local forward vector to given direction vector, if null is
supplied Vector3f.UNIT_Z is used.
|
void |
setDucked(boolean enabled)
Toggle character ducking.
|
void |
setDuckedFactor(float factor)
Alter the height multiplier for ducking.
|
void |
setGravity(Vector3f gravity)
Alter the gravity acting on this character.
|
protected void |
setHeightPercent(float percent)
Alter the height of collision shape.
|
void |
setJumpForce(Vector3f jumpForce)
Alter the jump force.
|
void |
setPhysicsDamping(float physicsDamping)
Alter how much motion in the local X-Z plane is damped.
|
protected void |
setPhysicsLocation(Vector3f vec)
Translate the character to the specified location.
|
protected void |
setPhysicsRotation(Quaternion quat)
Rotate the physics object to the specified orientation.
|
void |
setViewDirection(Vector3f vec)
Alter the character's view direction.
|
void |
setWalkDirection(Vector3f vec)
Alter the character's the walk direction.
|
void |
update(float tpf)
Update this control.
|
protected void |
updateLocalCoordinateSystem()
Updates the local coordinate system from the localForward and localUp
vectors, adapts localForward, sets localForwardRotation quaternion to
local Z-forward rotation.
|
protected void |
updateLocalViewDirection()
Updates the local X-Z view direction and the corresponding rotation
quaternion for the spatial.
|
void |
warp(Vector3f vec)
Move the character somewhere.
|
void |
write(JmeExporter ex)
Serialize this control, for example when saving to a J3O file.
|
applyPhysicsTransform, cloneFields, cloneForSpatial, getPhysicsSpace, getSpatial, getSpatialRotation, getSpatialTranslation, isApplyPhysicsLocal, isEnabled, setApplyPhysicsLocal, setEnabled, setPhysicsSpace, setSpatial
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
cloneFields
protected static final java.util.logging.Logger logger
protected PhysicsRigidBody rigidBody
protected float radius
protected float height
protected float mass
protected float duckedFactor
protected final Vector3f localUp
protected final Vector3f localForward
protected final Vector3f localLeft
protected final Quaternion localForwardRotation
protected final Vector3f viewDirection
protected final Vector3f location
protected final Quaternion rotation
rotatedViewDirection
protected final Vector3f rotatedViewDirection
protected final Vector3f walkDirection
protected final Vector3f jumpForce
protected float physicsDamping
protected final Vector3f scale
protected final Vector3f velocity
protected boolean jump
protected boolean onGround
protected boolean ducked
protected boolean wantToUnDuck
protected BetterCharacterControl()
public BetterCharacterControl(float radius, float height, float mass)
The final height when ducking must be larger than 2x radius. The jumpForce will be set to an upward force of 5x mass.
radius
- the radius of the character's collision shape (>0)height
- the height of the character's collision shape
(>2*radius)mass
- the character's mass (≥0)public void update(float tpf)
update
in interface Control
update
in class AbstractPhysicsControl
tpf
- the time interval between frames (in seconds, ≥0)public void render(RenderManager rm, ViewPort vp)
render
in interface Control
render
in class AbstractPhysicsControl
rm
- the render manager (not null)vp
- the view port to render (not null)public void prePhysicsTick(PhysicsSpace space, float tpf)
prePhysicsTick
in interface PhysicsTickListener
space
- the space that is about to be stepped (not null)tpf
- the time per physics step (in seconds, ≥0)public void physicsTick(PhysicsSpace space, float tpf)
physicsTick
in interface PhysicsTickListener
space
- the space that was just stepped (not null)tpf
- the time per physics step (in seconds, ≥0)public void warp(Vector3f vec)
vec
- the desired character location (not null)public void jump()
public void setJumpForce(Vector3f jumpForce)
jumpForce
- the desired jump force (not null, unaffected,
default=5*mass in +Y direction)public Vector3f getJumpForce()
public boolean isOnGround()
public void setDucked(boolean enabled)
enabled
- true→duck, false→unduckpublic boolean isDucked()
public void setDuckedFactor(float factor)
factor
- the factor by which the height should be multiplied when
ducking (≥0, ≤1)public float getDuckedFactor()
public void setWalkDirection(Vector3f vec)
vec
- The movement direction and speed in m/spublic Vector3f getWalkDirection()
public void setViewDirection(Vector3f vec)
vec
- a direction vector (not null, unaffected)public Vector3f getViewDirection()
public void resetForward(Vector3f vec)
vec
- the desired forward vector (perpendicular to the gravity
vector, may be null, default=0,0,1)public Vector3f getVelocity()
public void setGravity(Vector3f gravity)
gravity
- an acceleration vector (not null, unaffected)public Vector3f getGravity()
public Vector3f getGravity(Vector3f store)
store
- storage for the result (modified if not null)public void setPhysicsDamping(float physicsDamping)
physicsDamping
- the desired damping factor (0→no damping, 1=no
external forces, default=0.9)public float getPhysicsDamping()
protected void setHeightPercent(float percent)
percent
- the desired height, as a percentage of the full heightprotected void checkOnGround()
protected boolean checkCanUnDuck()
protected CollisionShape getShape()
protected float getFinalHeight()
protected float getFinalRadius()
protected void updateLocalCoordinateSystem()
protected void updateLocalViewDirection()
protected final void calculateNewForward(Quaternion rotation, Vector3f direction, Vector3f worldUpVector)
rotation
- The rotation to set the result on or null to create a new
Quaternion, this will be set to the new "z-forward" rotation if not nulldirection
- The direction to base the new look direction on, will be
set to the new directionworldUpVector
- The up vector to use, the result direction will be
perpendicular to thisprotected void setPhysicsLocation(Vector3f vec)
setPhysicsLocation
in class AbstractPhysicsControl
vec
- desired location (not null, unaffected)protected void setPhysicsRotation(Quaternion quat)
We don't set the actual physics rotation but the view rotation here. It might actually be altered by the calculateNewForward method.
setPhysicsRotation
in class AbstractPhysicsControl
quat
- desired orientation (not null, unaffected)protected void addPhysics(PhysicsSpace space)
addPhysics
in class AbstractPhysicsControl
space
- which physics space to add to (not null)protected void removePhysics(PhysicsSpace space)
removePhysics
in class AbstractPhysicsControl
space
- which physics space to remove from (not null)protected void createSpatialData(Spatial spat)
createSpatialData
in class AbstractPhysicsControl
spat
- the controlled spatial (not null, alias created)protected void removeSpatialData(Spatial spat)
removeSpatialData
in class AbstractPhysicsControl
spat
- the previously controlled spatial (not null)public java.lang.Object jmeClone()
jmeClone
in interface JmeCloneable
public void write(JmeExporter ex) throws java.io.IOException
write
in interface Savable
write
in class AbstractPhysicsControl
ex
- exporter (not null)java.io.IOException
- from exporterpublic void read(JmeImporter im) throws java.io.IOException
read
in interface Savable
read
in class AbstractPhysicsControl
im
- importer (not null)java.io.IOException
- from importer