public class PhysicsVehicle extends PhysicsRigidBody
From Bullet manual:
For arcade style vehicle simulations, it is recommended to use the simplified
Bullet vehicle model as provided in btRaycastVehicle. Instead of simulation
each wheel and chassis as separate rigid bodies, connected by constraints, it
uses a simplified model. This simplified model has many benefits, and is
widely used in commercial driving games.
The entire vehicle is represented as a single rigidbody, the chassis. The collision detection of the wheels is approximated by ray casts, and the tire friction is a basic anisotropic friction model.
Modifier and Type | Field and Description |
---|---|
protected PhysicsSpace |
physicsSpace
physics space where this vehicle is added, or null if none
|
protected long |
rayCasterId
Unique identifier of the ray caster.
|
protected VehicleTuning |
tuning
tuning parameters applied when a wheel is created
|
protected long |
vehicleId
Unique identifier of the btRaycastVehicle.
|
protected java.util.ArrayList<VehicleWheel> |
wheels
list of wheels
|
joints, kinematic, mass, motionState
COLLISION_GROUP_01, COLLISION_GROUP_02, COLLISION_GROUP_03, COLLISION_GROUP_04, COLLISION_GROUP_05, COLLISION_GROUP_06, COLLISION_GROUP_07, COLLISION_GROUP_08, COLLISION_GROUP_09, COLLISION_GROUP_10, COLLISION_GROUP_11, COLLISION_GROUP_12, COLLISION_GROUP_13, COLLISION_GROUP_14, COLLISION_GROUP_15, COLLISION_GROUP_16, COLLISION_GROUP_NONE, collisionGroup, collisionGroupsMask, collisionShape, objectId
Modifier | Constructor and Description |
---|---|
protected |
PhysicsVehicle()
No-argument constructor needed by SavableClassUtil.
|
|
PhysicsVehicle(CollisionShape shape)
Instantiate a vehicle with the specified collision shape and mass=1.
|
|
PhysicsVehicle(CollisionShape shape,
float mass)
Instantiate a vehicle with the specified collision shape and mass.
|
Modifier and Type | Method and Description |
---|---|
void |
accelerate(float force)
Apply the specified engine force to all wheels.
|
void |
accelerate(int wheel,
float force)
Apply the given engine force to the indexed wheel.
|
VehicleWheel |
addWheel(Spatial spat,
Vector3f connectionPoint,
Vector3f direction,
Vector3f axle,
float suspensionRestLength,
float wheelRadius,
boolean isFrontWheel)
Add a wheel to this vehicle
|
VehicleWheel |
addWheel(Vector3f connectionPoint,
Vector3f direction,
Vector3f axle,
float suspensionRestLength,
float wheelRadius,
boolean isFrontWheel)
Add a wheel to this vehicle.
|
void |
applyWheelTransforms()
used internally
|
void |
brake(float force)
Apply the given brake force to all wheels.
|
void |
brake(int wheel,
float force)
Apply the given brake force to the indexed wheel.
|
void |
createVehicle(PhysicsSpace space)
Used internally, creates the actual vehicle constraint when vehicle is
added to physics space.
|
protected void |
finalize()
Finalize this vehicle just before it is destroyed.
|
float |
getCurrentVehicleSpeedKmHour()
Read the vehicle's speed in km/h.
|
Vector3f |
getForwardVector(Vector3f vector)
Copy the vehicle's forward direction.
|
float |
getFrictionSlip()
Read the initial friction for new wheels.
|
float |
getMaxSuspensionForce()
Read the initial maximum suspension force for new wheels.
|
float |
getMaxSuspensionTravelCm()
Read the initial maximum suspension travel distance for new wheels.
|
int |
getNumWheels()
Read the number of wheels on this vehicle.
|
float |
getSuspensionCompression()
Read the initial damping (when the suspension is compressed) for new
wheels.
|
float |
getSuspensionDamping()
Read the initial damping (when the suspension is expanded) for new
wheels.
|
float |
getSuspensionStiffness()
Read the initial suspension stiffness for new wheels.
|
long |
getVehicleId()
used internally
|
VehicleWheel |
getWheel(int wheel)
Access the indexed wheel of this vehicle.
|
protected void |
postRebuild()
For use by subclasses.
|
void |
read(JmeImporter im)
De-serialize this vehicle, for example when loading from a J3O file.
|
void |
removeWheel(int wheel)
Remove a wheel.
|
void |
resetSuspension()
Reset this vehicle's suspension.
|
void |
setFrictionSlip(float frictionSlip)
Alter the initial friction for new wheels.
|
void |
setFrictionSlip(int wheel,
float frictionSlip)
Alter the friction of the indexed wheel.
|
void |
setMaxSuspensionForce(float maxSuspensionForce)
Alter the initial maximum suspension force for new wheels.
|
void |
setMaxSuspensionForce(int wheel,
float maxSuspensionForce)
Alter the maximum suspension force for the specified wheel.
|
void |
setMaxSuspensionTravelCm(float maxSuspensionTravelCm)
Alter the initial maximum suspension travel distance for new wheels.
|
void |
setMaxSuspensionTravelCm(int wheel,
float maxSuspensionTravelCm)
Alter the maximum suspension travel distance for the indexed wheel.
|
void |
setRollInfluence(int wheel,
float rollInfluence)
Alter the roll influence of the indexed wheel.
|
void |
setSuspensionCompression(float suspensionCompression)
Alter the initial damping (when the suspension is compressed) for new
wheels.
|
void |
setSuspensionCompression(int wheel,
float suspensionCompression)
Alter the damping (when the suspension is compressed) for the indexed
wheel.
|
void |
setSuspensionDamping(float suspensionDamping)
Alter the initial damping (when the suspension is expanded) for new
wheels.
|
void |
setSuspensionDamping(int wheel,
float suspensionDamping)
Alter the damping (when the suspension is expanded) for the indexed
wheel.
|
void |
setSuspensionStiffness(float suspensionStiffness)
Alter the initial suspension stiffness for new wheels.
|
void |
setSuspensionStiffness(int wheel,
float suspensionStiffness)
Alter the suspension stiffness of the indexed wheel.
|
void |
steer(float value)
Alter the steering angle of all front wheels.
|
void |
steer(int wheel,
float value)
Alter the steering angle of the indexed wheel.
|
void |
updateWheels()
used internally
|
void |
write(JmeExporter ex)
Serialize this vehicle, for example when saving to a J3O file.
|
activate, addJoint, applyCentralForce, applyForce, applyImpulse, applyTorque, applyTorqueImpulse, clearForces, getAngularDamping, getAngularFactor, getAngularFactor, getAngularSleepingThreshold, getAngularVelocity, getAngularVelocity, getCcdMotionThreshold, getCcdSquareMotionThreshold, getCcdSweptSphereRadius, getFriction, getGravity, getGravity, getInverseInertiaLocal, getJoints, getLinearDamping, getLinearFactor, getLinearSleepingThreshold, getLinearVelocity, getLinearVelocity, getMass, getMotionState, getPhysicsLocation, getPhysicsLocation, getPhysicsRotation, getPhysicsRotation, getPhysicsRotationMatrix, getPhysicsRotationMatrix, getRestitution, isActive, isInWorld, isKinematic, preRebuild, rebuildRigidBody, removeJoint, setAngularDamping, setAngularFactor, setAngularFactor, setAngularSleepingThreshold, setAngularVelocity, setCcdMotionThreshold, setCcdSweptSphereRadius, setCollisionShape, setContactResponse, setDamping, setFriction, setGravity, setInverseInertiaLocal, setKinematic, setLinearDamping, setLinearFactor, setLinearSleepingThreshold, setLinearVelocity, setMass, setPhysicsLocation, setPhysicsRotation, setPhysicsRotation, setRestitution, setSleepingThresholds
addCollideWithGroup, attachCollisionShape, finalizeNative, getCollideWithGroups, getCollisionFlags, getCollisionGroup, getCollisionShape, getDeactivationTime, getObjectId, getUserObject, initUserPointer, isContactResponse, removeCollideWithGroup, setCollideWithGroups, setCollisionFlags, setCollisionGroup, setUserObject
protected long vehicleId
protected long rayCasterId
protected VehicleTuning tuning
protected java.util.ArrayList<VehicleWheel> wheels
protected PhysicsSpace physicsSpace
protected PhysicsVehicle()
public PhysicsVehicle(CollisionShape shape)
shape
- the desired shape (not null, alias created)public PhysicsVehicle(CollisionShape shape, float mass)
shape
- the desired shape (not null, alias created)mass
- (>0)public void updateWheels()
public void applyWheelTransforms()
protected void postRebuild()
PhysicsRigidBody
postRebuild
in class PhysicsRigidBody
public void createVehicle(PhysicsSpace space)
space
- which physics spacepublic VehicleWheel addWheel(Vector3f connectionPoint, Vector3f direction, Vector3f axle, float suspensionRestLength, float wheelRadius, boolean isFrontWheel)
connectionPoint
- the location where the suspension connects to the
chassis (in chassis coordinates, not null, unaffected)direction
- the suspension direction (in chassis coordinates, not
null, unaffected, typically down/0,-1,0)axle
- the axis direction (in chassis coordinates, not null,
unaffected, typically -1,0,0)suspensionRestLength
- the rest length of the suspension (in
physics-space units)wheelRadius
- the wheel radius (in physics-space units, >0)isFrontWheel
- true→front (steering) wheel,
false→non-front wheelpublic VehicleWheel addWheel(Spatial spat, Vector3f connectionPoint, Vector3f direction, Vector3f axle, float suspensionRestLength, float wheelRadius, boolean isFrontWheel)
spat
- the associated spatial, or null if noneconnectionPoint
- the location where the suspension connects to the
chassis (in chassis coordinates, not null, unaffected)direction
- the suspension direction (in chassis coordinates, not
null, unaffected, typically down/0,-1,0)axle
- the axis direction (in chassis coordinates, not null,
unaffected, typically -1,0,0)suspensionRestLength
- the rest length of the suspension (in
physics-space units)wheelRadius
- the wheel radius (in physics-space units, >0)isFrontWheel
- true→front (steering) wheel,
false→non-front wheelpublic void removeWheel(int wheel)
wheel
- the index of the wheel to remove (≥0)public VehicleWheel getWheel(int wheel)
wheel
- the index of the wheel to access (≥0, <count)public int getNumWheels()
public float getFrictionSlip()
public void setFrictionSlip(float frictionSlip)
setFrictionSlip(int, float)
.
For better handling, increase the friction.
frictionSlip
- the desired coefficient of friction between tyre and
ground (0.8→realistic car, 10000→kart racer, default=10.5)public void setFrictionSlip(int wheel, float frictionSlip)
For better handling, increase the friction.
wheel
- the index of the wheel to modify (≥0)frictionSlip
- the desired coefficient of friction between tyre and
ground (0.8→realistic car, 10000→kart racer)public void setRollInfluence(int wheel, float rollInfluence)
The roll-influence factor reduces (or magnifies) any torque contributed by the wheel that would tend to cause the vehicle to roll over. This is a bit of a hack, but it's quite effective.
If the friction between the tyres and the ground is too high, you may reduce this factor to prevent the vehicle from rolling over. You should also try lowering the vehicle's center of mass.
wheel
- the index of the wheel to modify (≥0)rollInfluence
- the desired roll-influence factor (0→no roll
torque, 1→realistic behavior, default=1)public float getMaxSuspensionTravelCm()
public void setMaxSuspensionTravelCm(float maxSuspensionTravelCm)
setMaxSuspensionTravelCm(int, float)
.maxSuspensionTravelCm
- the desired maximum distance the suspension
can be compressed (in centimeters, default=500)public void setMaxSuspensionTravelCm(int wheel, float maxSuspensionTravelCm)
wheel
- the index of the wheel to modify (≥0)maxSuspensionTravelCm
- the desired maximum distance the suspension
can be compressed (in centimeters)public float getMaxSuspensionForce()
public void setMaxSuspensionForce(float maxSuspensionForce)
setMaxSuspensionForce(int, float)
.
If the suspension cannot handle the vehicle's weight, increase this limit.
maxSuspensionForce
- the desired maximum force per wheel
(default=6000)public void setMaxSuspensionForce(int wheel, float maxSuspensionForce)
If the suspension cannot handle the vehicle's weight, increase this limit.
wheel
- the index of the wheel to modify (≥0)maxSuspensionForce
- the desired maximum force per wheel
(default=6000)public float getSuspensionCompression()
public void setSuspensionCompression(float suspensionCompression)
setSuspensionCompression(int, float)
.
Set to k * 2 * FastMath.sqrt(m_suspensionStiffness) where k is the damping ratio:
k = 0.0 undamped and bouncy, k = 1.0 critical damping, k between 0.1 and 0.3 are good values
suspensionCompression
- the desired damping coefficient
(default=0.83)public void setSuspensionCompression(int wheel, float suspensionCompression)
Set to k * 2 * FastMath.sqrt(m_suspensionStiffness) where k is the damping ratio:
k = 0.0 undamped and bouncy, k = 1.0 critical damping, k between 0.1 and 0.3 are good values
wheel
- the index of the wheel to modify (≥0)suspensionCompression
- the desired damping coefficientpublic float getSuspensionDamping()
public void setSuspensionDamping(float suspensionDamping)
setSuspensionCompression(int, float)
.
Set to k * 2 * FastMath.sqrt(m_suspensionStiffness) where k is the damping ratio:
k = 0.0 undamped and bouncy, k = 1.0 critical damping, k between 0.1 and 0.3 are good values
suspensionDamping
- the desired damping coefficient (default=0.88)public void setSuspensionDamping(int wheel, float suspensionDamping)
Set to k * 2 * FastMath.sqrt(m_suspensionStiffness) where k is the damping ratio:
k = 0.0 undamped and bouncy, k = 1.0 critical damping, k between 0.1 and 0.3 are good values
wheel
- the index of the wheel to modify (≥0)suspensionDamping
- the desired damping coefficientpublic float getSuspensionStiffness()
public void setSuspensionStiffness(float suspensionStiffness)
setSuspensionStiffness(int, float)
.suspensionStiffness
- the desired stiffness coefficient
(10→off-road buggy, 50→sports car, 200→Formula-1 race car,
default=5.88)public void setSuspensionStiffness(int wheel, float suspensionStiffness)
wheel
- the index of the wheel to modify (≥0)suspensionStiffness
- the desired stiffness coefficient
(10→off-road buggy, 50→sports car, 200→Formula-1 race car,
default=5.88)public void resetSuspension()
public void accelerate(float force)
force
- the desired amount of forcepublic void accelerate(int wheel, float force)
wheel
- the index of the wheel to apply the force to (≥0)force
- the desired amount of forcepublic void steer(float value)
value
- the desired steering angle (in radians, 0=straight)public void steer(int wheel, float value)
wheel
- the index of the wheel to steer (≥0)value
- the desired steering angle (in radians, 0=straight)public void brake(float force)
force
- the desired amount of forcepublic void brake(int wheel, float force)
wheel
- the index of the wheel to apply the force to (≥0)force
- the desired amount of forcepublic float getCurrentVehicleSpeedKmHour()
public Vector3f getForwardVector(Vector3f vector)
vector
- storage for the result (modified if not null)public long getVehicleId()
public void read(JmeImporter im) throws java.io.IOException
read
in interface Savable
read
in class PhysicsRigidBody
im
- importer (not null)java.io.IOException
- from importerpublic void write(JmeExporter ex) throws java.io.IOException
write
in interface Savable
write
in class PhysicsRigidBody
ex
- exporter (not null)java.io.IOException
- from exporterprotected void finalize() throws java.lang.Throwable
finalize
in class PhysicsCollisionObject
java.lang.Throwable
- ignored by the garbage collector