Class PhysicsVehicle

All Implemented Interfaces:
com.jme3.export.Savable, com.jme3.util.clone.JmeCloneable, Cloneable, Comparable<NativePhysicsObject>
Direct Known Subclasses:
VehicleControl

public class PhysicsVehicle extends PhysicsRigidBody
A rigid body for simplified vehicle simulation based on Bullet's btRaycastVehicle.

The wheels of a PhysicsVehicle aren't collision objects, so the vehicle's ignore list doesn't affect them.

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 rigid body, the chassis. The collision detection of the wheels is approximated by ray casts, and the tire friction is a basic anisotropic friction model.

  • Field Details

    • logger3

      public static final Logger logger3
      message logger for this class
  • Constructor Details

    • PhysicsVehicle

      protected PhysicsVehicle()
      No-argument constructor needed by SavableClassUtil.
    • PhysicsVehicle

      public PhysicsVehicle(CollisionShape shape)
      Instantiate a responsive vehicle with the specified CollisionShape and mass=1.
      Parameters:
      shape - the desired shape of the chassis (not null, alias created)
    • PhysicsVehicle

      public PhysicsVehicle(CollisionShape shape, float mass)
      Instantiate a responsive vehicle with the specified CollisionShape and mass.
      Parameters:
      shape - the desired shape of the chassis (not null, alias created)
      mass - the desired mass of the chassis (>0, default=1)
  • Method Details

    • accelerate

      public void accelerate(float force)
      Apply the specified engine force to each wheel. Works continuously. The vehicle must be added to a PhysicsSpace.
      Parameters:
      force - the desired amount of force (may be negative)
    • accelerate

      public void accelerate(int wheelIndex, float force)
      Apply the specified engine force to the indexed wheel. Works continuously. The vehicle must be added to a PhysicsSpace.
      Parameters:
      wheelIndex - the index of the wheel to apply the force to (≥0, <count)
      force - the desired amount of force (may be negative)
    • addWheel

      public VehicleWheel addWheel(com.jme3.scene.Spatial subtree, com.jme3.math.Vector3f connectionPoint, com.jme3.math.Vector3f direction, com.jme3.math.Vector3f axle, float suspensionRestLength, float wheelRadius, boolean isFrontWheel)
      Add a wheel to this vehicle.
      Parameters:
      subtree - the scene-graph subtree for visualization (alias created) or null for none
      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 wheel
      Returns:
      a new VehicleWheel for access (not null)
    • addWheel

      public VehicleWheel addWheel(com.jme3.math.Vector3f connectionPoint, com.jme3.math.Vector3f direction, com.jme3.math.Vector3f axle, float suspensionRestLength, float wheelRadius, boolean isFrontWheel)
      For compatibility with the jme3-jbullet library.
      Parameters:
      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 wheel
      Returns:
      a new VehicleWheel for access (not null)
    • applyWheelTransforms

      public void applyWheelTransforms()
      Used internally.
    • brake

      public void brake(float impulse)
      Apply the specified brake impulse to all wheels. Works continuously. The vehicle must be added to a PhysicsSpace.
      Parameters:
      impulse - the desired impulse
    • brake

      public void brake(int wheelIndex, float impulse)
      Apply the specified brake impulse to the indexed wheel. Works continuously. The vehicle must be added to a PhysicsSpace.
      Parameters:
      wheelIndex - the index of the wheel to apply the impulse to (≥0, <count)
      impulse - the desired impulse
    • castRay

      public float castRay(int wheelIndex)
      Compute depth for the indexed wheel by raycasting. The vehicle must be added to a PhysicsSpace.
      Parameters:
      wheelIndex - the index of the wheel to raycast (≥0, <count)
      Returns:
      the depth value, or -1 if the raycast finds no result
    • createVehicle

      public void createVehicle(PhysicsSpace space)
      Used internally, creates the controller when the vehicle is added to a PhysicsSpace.
      Parameters:
      space - the PhysicsSpace to use, or null for none
    • forwardAxisIndex

      public int forwardAxisIndex()
      Determine the index of this vehicle's forward axis. The vehicle must be added to a PhysicsSpace.
      Returns:
      the index of the local axis: 0→X, 1→Y, 2→Z, -1→custom axes
    • getController

      public VehicleController getController()
      Access this vehicle's controller.
      Returns:
      the pre-existing instance, or null if never added to a PhysicsSpace
    • getCurrentVehicleSpeedKmHour

      public float getCurrentVehicleSpeedKmHour()
      Determine this vehicle's forward velocity as of the previous simulation step. The vehicle must be added to a PhysicsSpace. Note that the units are kilometers per hour ONLY if the physics-space unit is exactly one meter.
      Returns:
      the forward component of the linear velocity (in physics-space units per 3.6 seconds, may be negative)
    • getForwardVector

      public com.jme3.math.Vector3f getForwardVector(com.jme3.math.Vector3f storeResult)
      Determine this vehicle's forward direction. The vehicle must be added to a PhysicsSpace.
      Parameters:
      storeResult - storage for the result (modified if not null)
      Returns:
      a direction vector (in physics-space coordinates, either storeResult or a new vector, not null)
    • getFrictionSlip

      public float getFrictionSlip()
      Determine the initial friction for new wheels.
      Returns:
      the coefficient of friction between tire and ground (0.8→realistic car, 10000→kart racer)
    • getMaxSuspensionForce

      public float getMaxSuspensionForce()
      Determine the initial maximum suspension force for new wheels.
      Returns:
      the maximum force per wheel
    • getMaxSuspensionTravelCm

      public float getMaxSuspensionTravelCm()
      Determine the initial maximum suspension travel distance for new wheels. Note that the units are centimeters ONLY if the physics-space unit is exactly one meter.
      Returns:
      the maximum amount a suspension can be compressed or expanded, relative to its rest length (in hundredths of a physics-space unit)
    • getNumWheels

      public int getNumWheels()
      Count the number of wheels on this vehicle.
      Returns:
      count (≥0)
    • getSuspensionCompression

      public float getSuspensionCompression()
      Determine the initial damping (when the suspension is compressed) for new wheels.
      Returns:
      the damping coefficient
    • getSuspensionDamping

      public float getSuspensionDamping()
      Determine the initial damping (when the suspension is expanded) for new wheels.
      Returns:
      the damping coefficient
    • getSuspensionStiffness

      public float getSuspensionStiffness()
      Determine the initial suspension stiffness for new wheels.
      Returns:
      the stiffness constant (10→off-road buggy, 50→sports car, 200→Formula-1 race car)
    • getTuning

      public VehicleTuning getTuning()
      Access the tuning parameters applied when a wheel is created.
      Returns:
      the pre-existing instance (not null)
    • getVehicleId

      public long getVehicleId()
      Used internally.
      Returns:
      the unique identifier (not zero)
    • getWheel

      public VehicleWheel getWheel(int wheelIndex)
      Access the indexed wheel of this vehicle.
      Parameters:
      wheelIndex - the index of the wheel to access (≥0, <count)
      Returns:
      the pre-existing instance
    • removeWheel

      public void removeWheel(int wheelIndex)
      Remove a wheel. TODO test this---with joints!
      Parameters:
      wheelIndex - the index of the wheel to remove (≥0, <count)
    • resetSuspension

      public void resetSuspension()
      Reset this vehicle's suspension. The vehicle must be added to a PhysicsSpace.
    • rightAxisIndex

      public int rightAxisIndex()
      Determine the index of this vehicle's right-side axis. The vehicle must be added to a PhysicsSpace.
      Returns:
      the index of the local axis: 0→X, 1→Y, 2→Z, -1→custom axes
    • setFrictionSlip

      public void setFrictionSlip(float frictionSlip)
      Alter the initial friction for new wheels. Effective only before adding wheels. After adding a wheel, use setFrictionSlip(int, float).

      For better handling, increase the friction.

      Parameters:
      frictionSlip - the desired coefficient of friction between tire and ground (0.8→realistic car, 10000→kart racer, default=10.5)
    • setFrictionSlip

      public void setFrictionSlip(int wheelIndex, float frictionSlip)
      Alter the friction of the indexed wheel.

      For better handling, increase the friction.

      Parameters:
      wheelIndex - the index of the wheel to modify (≥0, <count)
      frictionSlip - the desired coefficient of friction between tire and ground (0.8→realistic car, 10000→kart racer, default=10.5)
    • setMaxSuspensionForce

      public void setMaxSuspensionForce(float maxSuspensionForce)
      Alter the initial maximum suspension force for new wheels. Effective only before adding wheels. After adding a wheel, use setMaxSuspensionForce(int, float).

      If the suspension cannot handle the vehicle's weight, increase this limit.

      Parameters:
      maxSuspensionForce - the desired maximum force per wheel (default=6000)
    • setMaxSuspensionForce

      public void setMaxSuspensionForce(int wheelIndex, float maxSuspensionForce)
      Alter the maximum suspension force for the specified wheel.

      If the suspension cannot handle the vehicle's weight, increase this limit.

      Parameters:
      wheelIndex - the index of the wheel to modify (≥0, <count)
      maxSuspensionForce - the desired maximum force per wheel (default=6000)
    • setMaxSuspensionTravelCm

      public void setMaxSuspensionTravelCm(float maxSuspensionTravelCm)
      Alter the initial maximum suspension travel distance for new wheels. Effective only before adding wheels. After adding a wheel, use setMaxSuspensionTravelCm(int, float). Note that the units are centimeters ONLY if the physics-space unit is exactly one meter.
      Parameters:
      maxSuspensionTravelCm - the desired maximum amount a suspension can be compressed or expanded, relative to its rest length (in hundredths of a physics-space unit, default=500)
    • setMaxSuspensionTravelCm

      public void setMaxSuspensionTravelCm(int wheelIndex, float maxSuspensionTravelCm)
      Alter the maximum suspension travel distance for the indexed wheel. Note that the units are centimeters ONLY if the physics-space unit is exactly one meter.
      Parameters:
      wheelIndex - the index of the wheel to modify (≥0, <count)
      maxSuspensionTravelCm - the desired maximum amount the suspension can be compressed or expanded, relative to its rest length (in hundredths of a physics-space unit, default=500)
    • setRollInfluence

      public void setRollInfluence(int wheelIndex, float rollInfluence)
      Alter the roll influence of the indexed wheel.

      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 tires 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.

      Parameters:
      wheelIndex - the index of the wheel to modify (≥0, <count)
      rollInfluence - the desired roll-influence factor (0→no roll torque, 1→realistic behavior, default=1)
    • setSuspensionCompression

      public void setSuspensionCompression(float coefficient)
      Alter the initial damping (when the suspension is compressed) for new wheels. Effective only before adding wheels. After adding a wheel, use 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

      Parameters:
      coefficient - the desired damping coefficient (default=0.83)
    • setSuspensionCompression

      public void setSuspensionCompression(int wheelIndex, float coefficient)
      Alter the damping (when the suspension is compressed) for the indexed wheel.

      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

      Parameters:
      wheelIndex - the index of the wheel to modify (≥0, <count)
      coefficient - the desired damping coefficient (default=0.83)
    • setSuspensionDamping

      public void setSuspensionDamping(float coefficient)
      Alter the initial damping (when the suspension is expanded) for new wheels. Effective only before adding wheels. After adding a wheel, use 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

      Parameters:
      coefficient - the desired damping coefficient (default=0.88)
    • setSuspensionDamping

      public void setSuspensionDamping(int wheelIndex, float coefficient)
      Alter the damping (when the suspension is expanded) for the indexed wheel.

      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

      Parameters:
      wheelIndex - the index of the wheel to modify (≥0, <count)
      coefficient - the desired damping coefficient (default=0.88)
    • setSuspensionStiffness

      public void setSuspensionStiffness(float coefficient)
      Alter the initial suspension stiffness for new wheels. Effective only before adding wheels. After adding a wheel, use setSuspensionStiffness(int, float).
      Parameters:
      coefficient - the desired stiffness coefficient (10→off-road buggy, 50→sports car, 200→Formula-1 race car, default=5.88)
    • setSuspensionStiffness

      public void setSuspensionStiffness(int wheelIndex, float coefficient)
      Alter the suspension stiffness of the indexed wheel.
      Parameters:
      wheelIndex - the index of the wheel to modify (≥0, <count)
      coefficient - the desired stiffness coefficient (10→off-road buggy, 50→sports car, 200→Formula-1 race car, default=5.88)
    • steer

      public void steer(float angle)
      Alter the steering angle of all front wheels. The vehicle must be added to a PhysicsSpace.
      Parameters:
      angle - the desired angle (in radians, 0=straight, positive=left)
    • steer

      public void steer(int wheelIndex, float angle)
      Alter the steering angle of the indexed wheel. The vehicle must be added to a PhysicsSpace.
      Parameters:
      wheelIndex - the index of the wheel to steer (≥0, <count)
      angle - the desired angle (in radians, 0=straight, positive=left)
    • upAxisIndex

      public int upAxisIndex()
      Determine the index of this vehicle's up axis. The vehicle must be added to a PhysicsSpace.
      Returns:
      the index of the local axis: 0→X, 1→Y, 2→Z, -1→custom axes
    • updateWheels

      public void updateWheels()
      Used internally.
    • cloneFields

      public void cloneFields(com.jme3.util.clone.Cloner cloner, Object original)
      Callback from Cloner to convert this shallow-cloned body into a deep-cloned one, using the specified Cloner and original to resolve copied fields.
      Specified by:
      cloneFields in interface com.jme3.util.clone.JmeCloneable
      Overrides:
      cloneFields in class PhysicsRigidBody
      Parameters:
      cloner - the Cloner that's cloning this vehicle (not null)
      original - the instance from which this vehicle was shallow-cloned
    • read

      public void read(com.jme3.export.JmeImporter importer) throws IOException
      De-serialize this vehicle from the specified importer, for example when loading from a J3O file.
      Specified by:
      read in interface com.jme3.export.Savable
      Overrides:
      read in class PhysicsRigidBody
      Parameters:
      importer - (not null)
      Throws:
      IOException - from the importer
    • postRebuild

      protected void postRebuild()
      Invoked during a rebuild after the native object is created.

      For use by subclasses.

      Overrides:
      postRebuild in class PhysicsRigidBody
    • write

      public void write(com.jme3.export.JmeExporter exporter) throws IOException
      Serialize this vehicle to the specified exporter, for example when saving to a J3O file.
      Specified by:
      write in interface com.jme3.export.Savable
      Overrides:
      write in class PhysicsRigidBody
      Parameters:
      exporter - (not null)
      Throws:
      IOException - from the exporter