Package com.jme3.bullet.control
Class BetterCharacterControl
java.lang.Object
com.jme3.bullet.control.AbstractPhysicsControl
com.jme3.bullet.control.BetterCharacterControl
- All Implemented Interfaces:
PhysicsControl,PhysicsTickListener,Savable,Control,JmeCloneable,Cloneable
public class BetterCharacterControl
extends AbstractPhysicsControl
implements PhysicsTickListener, JmeCloneable
Intended to replace the CharacterControl class.
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 it will always stand upright.
Motion in the local X-Z plane is damped.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected floatrelative height when ducked (1=full height)protected floatprotected booleanprotected final Vector3fprotected final Vector3fLocal absolute z-forward direction, derived from gravity and UNIT_Z, updated continuously when gravity changes.protected final QuaternionLocal z-forward quaternion for the "local absolute" z-forward direction.protected final Vector3fLocal left direction, derived from up and forward.protected final Vector3flocal up direction, derived from gravityprotected final Vector3fspatial location, corresponds to RigidBody location.protected static final Loggerprotected floatmass of this character (>0)protected booleanprotected floatX-Z motion damping factor (0→no damping, 1=no external forces, default=0.9)protected floatprotected PhysicsRigidBodyprotected final Vector3fprotected final Quaternionspatial rotation, a Z-forward rotation based on the view direction and local X-Z plane.protected final Vector3fprotected final Vector3fprotected final Vector3fa Z-forward vector based on the view direction and the local X-Z plane.protected final Vector3fprotected booleanFields inherited from class com.jme3.bullet.control.AbstractPhysicsControl
added, applyLocal, enabled, space, spatial -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedNo-argument constructor needed by SavableClassUtil.BetterCharacterControl(float radius, float height, float mass) Instantiate an enabled control with the specified properties. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddPhysics(PhysicsSpace space) Add all managed physics objects to the specified space.protected final voidcalculateNewForward(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 booleanThis checks if the character can go from ducked to unducked state by doing a ray test.protected voidTest whether the character is on the ground, by means of a ray test.protected voidcreateSpatialData(Spatial spat) Create spatial-dependent data.floatRead the height multiplier for ducking.protected floatCalculate the character's scaled height.protected floatCalculate the character's scaled radius.Copy the character's gravity vector.getGravity(Vector3f store) Copy the character's gravity vector.Access the jump force.floatRead how much motion in the local X-Z plane is damped.protected CollisionShapegetShape()Create a collision shape based on the scale parameter.Access the character's linear velocity in physics-space coordinates.Access the view direction.Read the walk velocity.booleanisDucked()Check if the character is ducking, either due to user input or due to unducking being impossible at the moment (obstacle above).booleanTest whether the character is supported.jmeClone()Create a shallow clone for the JME cloner.voidjump()Makes the character jump with the set jump force.voidphysicsTick(PhysicsSpace space, float tpf) Callback from Bullet, invoked just after the physics has been stepped.voidprePhysicsTick(PhysicsSpace space, float tpf) Callback from Bullet, invoked just before the physics is stepped.voidread(JmeImporter im) De-serialize this control, for example when loading from a J3O file.protected voidremovePhysics(PhysicsSpace space) Remove all managed physics objects from the specified space.protected voidremoveSpatialData(Spatial spat) Destroy spatial-dependent data.voidrender(RenderManager rm, ViewPort vp) Render this control.voidresetForward(Vector3f vec) Realign the local forward vector to given direction vector, if null is supplied Vector3f.UNIT_Z is used.voidsetDucked(boolean enabled) Toggle character ducking.voidsetDuckedFactor(float factor) Alter the height multiplier for ducking.voidsetGravity(Vector3f gravity) Alter the gravity acting on this character.protected voidsetHeightPercent(float percent) Alter the height of collision shape.voidsetJumpForce(Vector3f jumpForce) Alter the jump force.voidsetPhysicsDamping(float physicsDamping) Alter how much motion in the local X-Z plane is damped.protected voidTranslate the character to the specified location.protected voidsetPhysicsRotation(Quaternion quat) Rotate the physics object to the specified orientation.voidsetViewDirection(Vector3f vec) Alter the character's view direction.voidsetWalkDirection(Vector3f vec) Alter the character's the walk direction.voidupdate(float tpf) Update this control.protected voidUpdates the local coordinate system from the localForward and localUp vectors, adapts localForward, sets localForwardRotation quaternion to local Z-forward rotation.protected voidUpdates the local X-Z view direction and the corresponding rotation quaternion for the spatial.voidMove the character somewhere.voidwrite(JmeExporter ex) Serialize this control, for example when saving to a J3O file.Methods inherited from class com.jme3.bullet.control.AbstractPhysicsControl
applyPhysicsTransform, cloneFields, cloneForSpatial, getPhysicsSpace, getSpatial, getSpatialRotation, getSpatialTranslation, isApplyPhysicsLocal, isEnabled, setApplyPhysicsLocal, setEnabled, setPhysicsSpace, setSpatialMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.jme3.util.clone.JmeCloneable
cloneFields
-
Field Details
-
logger
-
rigidBody
-
radius
protected float radius -
height
protected float height -
mass
protected float massmass of this character (>0) -
duckedFactor
protected float duckedFactorrelative height when ducked (1=full height) -
localUp
local up direction, derived from gravity -
localForward
Local absolute z-forward direction, derived from gravity and UNIT_Z, updated continuously when gravity changes. -
localLeft
Local left direction, derived from up and forward. -
localForwardRotation
Local z-forward quaternion for the "local absolute" z-forward direction. -
viewDirection
a Z-forward vector based on the view direction and the local X-Z plane. -
location
spatial location, corresponds to RigidBody location. -
rotation
spatial rotation, a Z-forward rotation based on the view direction and local X-Z plane.- See Also:
-
rotatedViewDirection
-
walkDirection
-
jumpForce
-
physicsDamping
protected float physicsDampingX-Z motion damping factor (0→no damping, 1=no external forces, default=0.9) -
scale
-
velocity
-
jump
protected boolean jump -
onGround
protected boolean onGround -
ducked
protected boolean ducked -
wantToUnDuck
protected boolean wantToUnDuck
-
-
Constructor Details
-
BetterCharacterControl
protected BetterCharacterControl()No-argument constructor needed by SavableClassUtil. Do not invoke directly! -
BetterCharacterControl
public BetterCharacterControl(float radius, float height, float mass) Instantiate an enabled control with the specified properties.The final height when ducking must be larger than 2x radius. The jumpForce will be set to an upward force of 5x mass.
- Parameters:
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)
-
-
Method Details
-
update
public void update(float tpf) Update this control. Invoked once per frame during the logical-state update, provided the control is added to a scene graph. Do not invoke directly from user code.- Specified by:
updatein interfaceControl- Overrides:
updatein classAbstractPhysicsControl- Parameters:
tpf- the time interval between frames (in seconds, ≥0)
-
render
Render this control. Invoked once per view port per frame, provided the control is added to a scene. Should be invoked only by a subclass or by the RenderManager.- Specified by:
renderin interfaceControl- Overrides:
renderin classAbstractPhysicsControl- Parameters:
rm- the render manager (not null)vp- the view port to render (not null)
-
prePhysicsTick
Callback from Bullet, invoked just before the physics is stepped.- Specified by:
prePhysicsTickin interfacePhysicsTickListener- Parameters:
space- the space that is about to be stepped (not null)tpf- the time per physics step (in seconds, ≥0)
-
physicsTick
Callback from Bullet, invoked just after the physics has been stepped.- Specified by:
physicsTickin interfacePhysicsTickListener- Parameters:
space- the space that was just stepped (not null)tpf- the time per physics step (in seconds, ≥0)
-
warp
Move the character somewhere. Note the character also warps to the location of the spatial when the control is added.- Parameters:
vec- the desired character location (not null)
-
jump
public void jump()Makes the character jump with the set jump force. -
setJumpForce
Alter the jump force. The jump force is local to the character's coordinate system, which normally is always z-forward (in world coordinates, parent coordinates when set to applyLocalPhysics)- Parameters:
jumpForce- the desired jump force (not null, unaffected, default=5*mass in +Y direction)
-
getJumpForce
Access the jump force.- Returns:
- the pre-existing vector (not null)
-
isOnGround
public boolean isOnGround()Test whether the character is supported. Uses a ray test from the center of the character and might return false even if the character is not falling yet.- Returns:
- true if on the ground, otherwise false
-
setDucked
public void setDucked(boolean enabled) Toggle character ducking. When ducked the characters capsule collision shape height will be multiplied by duckedFactor to make the capsule smaller. When unducking, the character will check with a ray test if it can in fact unduck and only do so when it's possible. You can test the state using isDucked().- Parameters:
enabled- true→duck, false→unduck
-
isDucked
public boolean isDucked()Check if the character is ducking, either due to user input or due to unducking being impossible at the moment (obstacle above).- Returns:
- true if ducking, otherwise false
-
setDuckedFactor
public void setDuckedFactor(float factor) Alter the height multiplier for ducking.- Parameters:
factor- the factor by which the height should be multiplied when ducking (≥0, ≤1)
-
getDuckedFactor
public float getDuckedFactor()Read the height multiplier for ducking.- Returns:
- the factor (≥0, ≤1)
-
setWalkDirection
Alter the character's the walk direction. This parameter is framerate independent and the character will move continuously in the direction given by the vector with the speed given by the vector length in m/s.- Parameters:
vec- The movement direction and speed in m/s
-
getWalkDirection
Read the walk velocity. The length of the vector defines the speed.- Returns:
- the pre-existing vector (not null)
-
setViewDirection
Alter the character's view direction. Note this only defines the orientation in the local X-Z plane.- Parameters:
vec- a direction vector (not null, unaffected)
-
getViewDirection
Access the view direction. This need not agree with the spatial's forward direction.- Returns:
- the pre-existing vector (not null)
-
resetForward
Realign the local forward vector to given direction vector, if null is supplied Vector3f.UNIT_Z is used. The input vector must be perpendicular to gravity vector. This normally only needs to be invoked when the gravity direction changed continuously and the local forward vector is off due to drift. E.g. after walking around on a sphere "planet" for a while and then going back to a Y-up coordinate system the local Z-forward might not be 100% aligned with the Z axis.- Parameters:
vec- the desired forward vector (perpendicular to the gravity vector, may be null, default=0,0,1)
-
getVelocity
Access the character's linear velocity in physics-space coordinates.- Returns:
- the pre-existing vector (not null)
-
setGravity
Alter the gravity acting on this character. Note that this also realigns the local coordinate system of the character so that continuous changes in gravity direction are possible while maintaining a sensible control over the character.- Parameters:
gravity- an acceleration vector (not null, unaffected)
-
getGravity
Copy the character's gravity vector.- Returns:
- a new acceleration vector (not null)
-
getGravity
Copy the character's gravity vector.- Parameters:
store- storage for the result (modified if not null)- Returns:
- an acceleration vector (either the provided storage or a new vector, not null)
-
setPhysicsDamping
public void setPhysicsDamping(float physicsDamping) Alter how much motion in the local X-Z plane is damped.- Parameters:
physicsDamping- the desired damping factor (0→no damping, 1=no external forces, default=0.9)
-
getPhysicsDamping
public float getPhysicsDamping()Read how much motion in the local X-Z plane is damped.- Returns:
- the damping factor (0→no damping, 1=no external forces)
-
setHeightPercent
protected void setHeightPercent(float percent) Alter the height of collision shape.- Parameters:
percent- the desired height, as a percentage of the full height
-
checkOnGround
protected void checkOnGround()Test whether the character is on the ground, by means of a ray test. -
checkCanUnDuck
protected boolean checkCanUnDuck()This checks if the character can go from ducked to unducked state by doing a ray test.- Returns:
- true if able to unduck, otherwise false
-
getShape
Create a collision shape based on the scale parameter. The new shape is a compound shape containing an offset capsule.- Returns:
- a new compound shape (not null)
-
getFinalHeight
protected float getFinalHeight()Calculate the character's scaled height.- Returns:
- the height
-
getFinalRadius
protected float getFinalRadius()Calculate the character's scaled radius.- Returns:
- the radius
-
updateLocalCoordinateSystem
protected void updateLocalCoordinateSystem()Updates the local coordinate system from the localForward and localUp vectors, adapts localForward, sets localForwardRotation quaternion to local Z-forward rotation. -
updateLocalViewDirection
protected void updateLocalViewDirection()Updates the local X-Z view direction and the corresponding rotation quaternion for the spatial. -
calculateNewForward
protected final 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.- Parameters:
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 this
-
setPhysicsLocation
Translate the character to the specified location.- Specified by:
setPhysicsLocationin classAbstractPhysicsControl- Parameters:
vec- desired location (not null, unaffected)
-
setPhysicsRotation
Rotate the physics object to the specified orientation.We don't set the actual physics rotation but the view rotation here. It might actually be altered by the calculateNewForward method.
- Specified by:
setPhysicsRotationin classAbstractPhysicsControl- Parameters:
quat- desired orientation (not null, unaffected)
-
addPhysics
Add all managed physics objects to the specified space.- Specified by:
addPhysicsin classAbstractPhysicsControl- Parameters:
space- which physics space to add to (not null)
-
removePhysics
Remove all managed physics objects from the specified space.- Specified by:
removePhysicsin classAbstractPhysicsControl- Parameters:
space- which physics space to remove from (not null)
-
createSpatialData
Create spatial-dependent data. Invoked when this control is added to a spatial.- Specified by:
createSpatialDatain classAbstractPhysicsControl- Parameters:
spat- the controlled spatial (not null, alias created)
-
removeSpatialData
Destroy spatial-dependent data. Invoked when this control is removed from a spatial.- Specified by:
removeSpatialDatain classAbstractPhysicsControl- Parameters:
spat- the previously controlled spatial (not null)
-
jmeClone
Create a shallow clone for the JME cloner.- Specified by:
jmeClonein interfaceJmeCloneable- Returns:
- a new control (not null)
-
write
Serialize this control, for example when saving to a J3O file.- Specified by:
writein interfaceSavable- Overrides:
writein classAbstractPhysicsControl- Parameters:
ex- exporter (not null)- Throws:
IOException- from exporter
-
read
De-serialize this control, for example when loading from a J3O file.- Specified by:
readin interfaceSavable- Overrides:
readin classAbstractPhysicsControl- Parameters:
im- importer (not null)- Throws:
IOException- from importer
-