Class PhysicsVehicle

All Implemented Interfaces:
Savable
Direct Known Subclasses:
VehicleControl

public class PhysicsVehicle extends PhysicsRigidBody

PhysicsVehicleNode - Special PhysicsNode that implements vehicle functions

From bullet manual:
For most 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

    • vehicle

      protected com.bulletphysics.dynamics.vehicle.RaycastVehicle vehicle
    • tuning

      protected com.bulletphysics.dynamics.vehicle.VehicleTuning tuning
    • rayCaster

      protected com.bulletphysics.dynamics.vehicle.VehicleRaycaster rayCaster
    • wheels

      protected ArrayList<VehicleWheel> wheels
    • physicsSpace

      protected PhysicsSpace physicsSpace
  • Constructor Details

    • PhysicsVehicle

      protected PhysicsVehicle()
    • PhysicsVehicle

      public PhysicsVehicle(CollisionShape shape)
    • PhysicsVehicle

      public PhysicsVehicle(CollisionShape shape, float mass)
  • Method Details

    • updateWheels

      public void updateWheels()
      used internally
    • applyWheelTransforms

      public void applyWheelTransforms()
      used internally
    • postRebuild

      protected void postRebuild()
      Overrides:
      postRebuild in class PhysicsRigidBody
    • createVehicle

      public void createVehicle(PhysicsSpace space)
      Used internally, creates the actual vehicle constraint when vehicle is added to physics space
      Parameters:
      space - the PhysicsSpace to use (alias created) or null for none
    • addWheel

      public VehicleWheel addWheel(Vector3f connectionPoint, Vector3f direction, Vector3f axle, float suspensionRestLength, float wheelRadius, boolean isFrontWheel)
      Add a wheel to this vehicle
      Parameters:
      connectionPoint - The starting point of the ray, where the suspension connects to the chassis (chassis space)
      direction - the direction of the wheel (should be -Y / 0,-1,0 for a normal car)
      axle - The axis of the wheel, pointing right in vehicle direction (should be -X / -1,0,0 for a normal car)
      suspensionRestLength - The current length of the suspension (metres)
      wheelRadius - the wheel radius
      isFrontWheel - sets if this wheel is a front wheel (steering)
      Returns:
      the PhysicsVehicleWheel object to get/set infos on the wheel
    • addWheel

      public VehicleWheel addWheel(Spatial spat, Vector3f connectionPoint, Vector3f direction, Vector3f axle, float suspensionRestLength, float wheelRadius, boolean isFrontWheel)
      Add a wheel to this vehicle
      Parameters:
      spat - the wheel Geometry
      connectionPoint - The starting point of the ray, where the suspension connects to the chassis (chassis space)
      direction - the direction of the wheel (should be -Y / 0,-1,0 for a normal car)
      axle - The axis of the wheel, pointing right in vehicle direction (should be -X / -1,0,0 for a normal car)
      suspensionRestLength - The current length of the suspension (metres)
      wheelRadius - the wheel radius
      isFrontWheel - sets if this wheel is a front wheel (steering)
      Returns:
      the PhysicsVehicleWheel object to get/set infos on the wheel
    • removeWheel

      public void removeWheel(int wheel)
      This rebuilds the vehicle as there is no way in bullet to remove a wheel.
      Parameters:
      wheel - the index of the wheel to remove (≥0, <count)
    • getWheel

      public VehicleWheel getWheel(int wheel)
      You can get access to the single wheels via this method.
      Parameters:
      wheel - the wheel index
      Returns:
      the WheelInfo of the selected wheel
    • getNumWheels

      public int getNumWheels()
    • getFrictionSlip

      public float getFrictionSlip()
      Returns:
      the frictionSlip
    • setFrictionSlip

      public void setFrictionSlip(float frictionSlip)
      Use before adding wheels, this is the default used when adding wheels. After adding the wheel, use direct wheel access.
      The coefficient of friction between the tyre and the ground. Should be about 0.8 for realistic cars, but can be increased for better handling. Set large (10000.0) for kart racers.
      Parameters:
      frictionSlip - the frictionSlip to set
    • setFrictionSlip

      public void setFrictionSlip(int wheel, float frictionSlip)
      The coefficient of friction between the tyre and the ground. Should be about 0.8 for realistic cars, but can be increased for better handling. Set large (10000.0) for kart racers.
      Parameters:
      wheel - the index of the wheel to modify (≥0, <count)
      frictionSlip - the desired coefficient of friction between tyre and ground (0.8→realistic car, 10000→kart racer, default=10.5)
    • setRollInfluence

      public void setRollInfluence(int wheel, float rollInfluence)
      Reduces the rolling torque applied from the wheels that cause the vehicle to roll over. This is a bit of a hack, but it's quite effective. 0.0 = no roll, 1.0 = physical behaviour. If m_frictionSlip is too high, you'll need to reduce this to stop the vehicle rolling over. You should also try lowering the vehicle's centre of mass
      Parameters:
      wheel - the index of the wheel to modify (≥0, <count)
      rollInfluence - the desired roll-influence factor (0→no roll torque, 1→realistic behavior, default=1)
    • getMaxSuspensionTravelCm

      public float getMaxSuspensionTravelCm()
      Returns:
      the maxSuspensionTravelCm
    • setMaxSuspensionTravelCm

      public void setMaxSuspensionTravelCm(float maxSuspensionTravelCm)
      Use before adding wheels, this is the default used when adding wheels. After adding the wheel, use direct wheel access.
      The maximum distance the suspension can be compressed (centimetres)
      Parameters:
      maxSuspensionTravelCm - the maxSuspensionTravelCm to set
    • setMaxSuspensionTravelCm

      public void setMaxSuspensionTravelCm(int wheel, float maxSuspensionTravelCm)
      The maximum distance the suspension can be compressed (centimetres)
      Parameters:
      wheel - the index of the wheel to modify (≥0, <count)
      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)
    • getMaxSuspensionForce

      public float getMaxSuspensionForce()
    • setMaxSuspensionForce

      public void setMaxSuspensionForce(float maxSuspensionForce)
      This value caps the maximum suspension force, raise this above the default 6000 if your suspension cannot handle the weight of your vehicle.
      Parameters:
      maxSuspensionForce - the desired maximum force per wheel (default=6000)
    • setMaxSuspensionForce

      public void setMaxSuspensionForce(int wheel, float maxSuspensionForce)
      This value caps the maximum suspension force, raise this above the default 6000 if your suspension cannot handle the weight of your vehicle.
      Parameters:
      wheel - the index of the wheel to modify (≥0, <count)
      maxSuspensionForce - the desired maximum force per wheel (default=6000)
    • getSuspensionCompression

      public float getSuspensionCompression()
      Returns:
      the suspensionCompression
    • setSuspensionCompression

      public void setSuspensionCompression(float suspensionCompression)
      Use before adding wheels, this is the default used when adding wheels. After adding the wheel, use direct wheel access.
      The damping coefficient for when the suspension is compressed. Set to k * 2.0 * FastMath.sqrt(m_suspensionStiffness) so k is proportional to critical damping.
      k = 0.0 undamped/bouncy, k = 1.0 critical damping
      0.1 to 0.3 are good values
      Parameters:
      suspensionCompression - the suspensionCompression to set
    • setSuspensionCompression

      public void setSuspensionCompression(int wheel, float suspensionCompression)
      The damping coefficient for when the suspension is compressed. Set to k * 2.0 * FastMath.sqrt(m_suspensionStiffness) so k is proportional to critical damping.
      k = 0.0 undamped/bouncy, k = 1.0 critical damping
      0.1 to 0.3 are good values
      Parameters:
      wheel - the index of the wheel to modify (≥0, <count)
      suspensionCompression - the desired damping coefficient (default=4.4)
    • getSuspensionDamping

      public float getSuspensionDamping()
      Returns:
      the suspensionDamping
    • setSuspensionDamping

      public void setSuspensionDamping(float suspensionDamping)
      Use before adding wheels, this is the default used when adding wheels. After adding the wheel, use direct wheel access.
      The damping coefficient for when the suspension is expanding. See the comments for setSuspensionCompression for how to set k.
      Parameters:
      suspensionDamping - the suspensionDamping to set
    • setSuspensionDamping

      public void setSuspensionDamping(int wheel, float suspensionDamping)
      The damping coefficient for when the suspension is expanding. See the comments for setSuspensionCompression for how to set k.
      Parameters:
      wheel - the index of the wheel to modify (≥0, <count)
      suspensionDamping - the desired damping coefficient (default=2.3)
    • getSuspensionStiffness

      public float getSuspensionStiffness()
      Returns:
      the suspensionStiffness
    • setSuspensionStiffness

      public void setSuspensionStiffness(float suspensionStiffness)
      Use before adding wheels, this is the default used when adding wheels. After adding the wheel, use direct wheel access.
      The stiffness constant for the suspension. 10.0 - Off-road buggy, 50.0 - Sports car, 200.0 - F1 Car
      Parameters:
      suspensionStiffness - the desired stiffness coefficient (10→off-road buggy, 50→sports car, 200→Formula-1 race car, default=5.88)
    • setSuspensionStiffness

      public void setSuspensionStiffness(int wheel, float suspensionStiffness)
      The stiffness constant for the suspension. 10.0 - Off-road buggy, 50.0 - Sports car, 200.0 - F1 Car
      Parameters:
      wheel - the index of the wheel to modify (≥0, <count)
      suspensionStiffness - the desired stiffness coefficient (10→off-road buggy, 50→sports car, 200→Formula-1 race car, default=5.88)
    • resetSuspension

      public void resetSuspension()
      Reset the suspension
    • accelerate

      public void accelerate(float force)
      Apply the given engine force to all wheels, works continuously
      Parameters:
      force - the force
    • accelerate

      public void accelerate(int wheel, float force)
      Apply the given engine force, works continuously
      Parameters:
      wheel - the wheel to apply the force on
      force - the force
    • steer

      public void steer(float value)
      Set the given steering value to all front wheels (0 = forward)
      Parameters:
      value - the steering angle of the front wheels (Pi = 360deg)
    • steer

      public void steer(int wheel, float value)
      Set the given steering value to the given wheel (0 = forward)
      Parameters:
      wheel - the wheel to set the steering on
      value - the steering angle of the front wheels (Pi = 360deg)
    • brake

      public void brake(float force)
      Apply the given brake force to all wheels, works continuously
      Parameters:
      force - the force
    • brake

      public void brake(int wheel, float force)
      Apply the given brake force, works continuously
      Parameters:
      wheel - the wheel to apply the force on
      force - the force
    • getCurrentVehicleSpeedKmHour

      public float getCurrentVehicleSpeedKmHour()
      Get the current speed of the vehicle in km/h
      Returns:
      the speed
    • getForwardVector

      public Vector3f getForwardVector(Vector3f vector)
      Get the current forward vector of the vehicle in world coordinates
      Parameters:
      vector - The object to write the forward vector values to. Passing null will cause a new Vector3f to be created.
      Returns:
      The forward vector
    • getVehicleId

      public com.bulletphysics.dynamics.vehicle.RaycastVehicle getVehicleId()
      used internally
      Returns:
      the pre-existing instance
    • destroy

      public void destroy()
      Description copied from class: PhysicsRigidBody
      destroys this PhysicsRigidBody and removes it from memory
      Overrides:
      destroy in class PhysicsRigidBody
    • read

      public void read(JmeImporter im) throws IOException
      Description copied from class: PhysicsRigidBody
      De-serialize this body, for example when loading from a J3O file.
      Specified by:
      read in interface Savable
      Overrides:
      read in class PhysicsRigidBody
      Parameters:
      im - importer (not null)
      Throws:
      IOException - from importer
    • write

      public void write(JmeExporter ex) throws IOException
      Description copied from class: PhysicsRigidBody
      Serialize this body, for example when saving to a J3O file.
      Specified by:
      write in interface Savable
      Overrides:
      write in class PhysicsRigidBody
      Parameters:
      ex - exporter (not null)
      Throws:
      IOException - from exporter