Blender car game script

Filed in Python by on August 14, 2011 0 Comments

This is a script for a cars basic dynamic  system in the blender game engine

# import PhysicsConstraints
import PhysicsConstraints

# get list of objects in scene
objList = GameLogic.getCurrentScene().getObjectList()

# get vehicle named Car_Red
car = objList[“OBCar_Red”]

# get tires
tire_FD = objList[“OBTireFD”]
tire_FP = objList[“OBTireFP”]
tire_RD = objList[“OBTireRD”]
tire_RP = objList[“OBTireRP”]

# keep the vehicle active, otherwise simulation stops:
car.applyImpulse([ 0.0, 0.0, 0.0], [ 0.0, 0.0, 0.0])

# get physics ID
car_PhysicsID = car.getPhysicsId()

# create a vehicle constraint
vehicle_Constraint = PhysicsConstraints.createConstraint(car_PhysicsID, 0, 11)

# get the constraint ID and save it as an car variable
car.constraint_ID = vehicle_Constraint.getConstraintId()

# get the vehicle constraint ID
vehicle = PhysicsConstraints.getVehicleConstraint(car.constraint_ID)

#############################  Create car frame and suspension

# tire position from car object center
tirePos_FD = [ -2.0,   2.0, 0.0]
tirePos_FP = [  2.0,   2.0, 0.0]
tirePos_RD = [ -2.0,  -4.0, 0.0]
tirePos_RP = [  2.0,  -4.0, 0.0]

# suspension angle from car object center
suspension_Angle = [ 0.0, 0.0, -1.0]

# tire axis attached to axle
tireAxis = [ -1.0, 0.0, 0.0]

# suspension height
suspensionFront = 0.4
suspensionBack = 0.3

# tire radius
tireRadius = 0.75

# tire has steering?
tireSteer_Front = True
tireSteer_Rear = False

#############################  Add the tires to the car

# Add front driver tire
vehicle.addWheel( tire_FD, tirePos_FD, suspension_Angle, tireAxis,
suspensionFront,  tireRadius, tireSteer_Front )

# Add front passenger tire
vehicle.addWheel( tire_FP, tirePos_FP, suspension_Angle, tireAxis,
suspensionFront,  tireRadius, tireSteer_Front )

# Add rear driver tire
vehicle.addWheel( tire_RD, tirePos_RD, suspension_Angle, tireAxis,
suspensionBack,  tireRadius, tireSteer_Rear )

# Add rear passenger tire
vehicle.addWheel( tire_RP, tirePos_RP, suspension_Angle, tireAxis,
suspensionBack,  tireRadius, tireSteer_Rear )

################################# Car variables  Can be changed at anytime.  Anyplace.

grip = 30.0
compression = 4.0
damping = 5.0
stiffness = 8.0
roll = 0.1

# tire friction7
vehicle.setTyreFriction(grip, 0)
vehicle.setTyreFriction(grip, 1)
vehicle.setTyreFriction(grip, 2)
vehicle.setTyreFriction(grip, 3)

# suspension compression
vehicle.setSuspensionCompression(compression, 0)
vehicle.setSuspensionCompression(compression, 1)
vehicle.setSuspensionCompression(compression, 2)
vehicle.setSuspensionCompression(compression, 3)

# suspension damping
vehicle.setSuspensionDamping(damping, 0)
vehicle.setSuspensionDamping(damping, 1)
vehicle.setSuspensionDamping(damping, 2)
vehicle.setSuspensionDamping(damping, 3)

# suspension stiffness
vehicle.setSuspensionStiffness(stiffness, 0)
vehicle.setSuspensionStiffness(stiffness, 1)
vehicle.setSuspensionStiffness(stiffness, 2)
vehicle.setSuspensionStiffness(stiffness, 3)

# roll influence
vehicle.setRollInfluence( roll, 0)
vehicle.setRollInfluence( roll, 1)
vehicle.setRollInfluence( roll, 2)
vehicle.setRollInfluence( roll, 3)

About the Author ()

Hello, my name is Brennen. I create 3d. Videos. Music, tutorials, animations, and sometimes silly videos. I sell 3d models on three different websites: Turbosquid, 3d Export and 3D02 ►Business Inquiries: brennens3d@gmail.com**