Car physics not work

Hello again, I’m having trouble with a simple car project. The script is error-free, but the car doesn’t work as expected. The “” command has no effect either. I’d like to thank anyone who can help me.

from bge import constraints as ct
from mathutils import Vector as v
import GameLogic

own = GameLogic.getCurrentController().owner
id = own.getPhysicsId()

wh_0 = GameLogic.getCurrentScene().objects['Roda_0']
wh_1 = GameLogic.getCurrentScene().objects['Roda_1']
wh_2 = GameLogic.getCurrentScene().objects['Roda_2']
wh_3 = GameLogic.getCurrentScene().objects['Roda_3']

wh_0.removeParent()
wh_1.removeParent()
wh_2.removeParent()
wh_3.removeParent()

if not "init" in own:
    own['vehicle'] = ct.createVehicle(id)
    own['ct_vehicle'] = own['vehicle'].getConstraintId() 
    own['init'] = "Done"
    
    
    
wh_0_pos = [3.4,-6.3,-2.0]
wh_1_pos = [-3.4,-6.3,-2.0]
wh_2_pos = [3.4,6.0,-2.0]
wh_3_pos = [-3.4,6.0,-2.0]

wh_0_dw = ([0.0, 0.0, -1.0])
wh_1_dw = ([0.0, 0.0, -1.0])
wh_2_dw = ([0.0, 0.0, -1.0])
wh_3_dw = ([0.0, 0.0, -1.0])

wh_0_ax = ([-1.0, 0.0, 0.0])
wh_1_ax = ([-1.0, 0.0, 0.0])
wh_2_ax = ([-1.0, 0.0, 0.0])
wh_3_ax = ([-1.0, 0.0, 0.0])

wh_0_su = 0.0
wh_1_su = 0.0
wh_2_su = 0.0
wh_3_su = 0.0

wh_0_rad = 1.0
wh_1_rad = 1.0
wh_2_rad = 1.0
wh_3_rad = 1.0

wh_0_ste = True
wh_1_ste = True
wh_2_ste = False
wh_3_ste = False

own['vehicle'].addWheel( wh_0 , wh_0_pos , wh_0_dw , wh_0_ax , wh_0_su , wh_0_rad , wh_0_ste ) 
own['vehicle'].addWheel( wh_1 , wh_1_pos , wh_1_dw , wh_1_ax , wh_1_su , wh_1_rad , wh_1_ste ) 
own['vehicle'].addWheel( wh_2 , wh_2_pos , wh_2_dw , wh_2_ax , wh_2_su , wh_2_rad , wh_2_ste ) 
own['vehicle'].addWheel( wh_3 , wh_3_pos , wh_3_dw , wh_3_ax , wh_3_su , wh_3_rad , wh_3_ste ) 

stiffness = 1.0
own['vehicle'].setSuspensionStiffness(stiffness, 0)
own['vehicle'].setSuspensionStiffness(stiffness, 1)
own['vehicle'].setSuspensionStiffness(stiffness, 2)
own['vehicle'].setSuspensionStiffness(stiffness, 3)

'''
damping = 0.0
own['vehicle'].setSuspensionDamping(damping, 0)
own['vehicle'].setSuspensionDamping(damping, 1)
own['vehicle'].setSuspensionDamping(damping, 2)
own['vehicle'].setSuspensionDamping(damping, 3)

friction = 0.0
own['vehicle'].setTyreFriction(friction, 0)
own['vehicle'].setTyreFriction(friction, 1)
own['vehicle'].setTyreFriction(friction, 2)
own['vehicle'].setTyreFriction(friction, 3)

rollInfluece = 0.0
own['vehicle'].setRollInfluence(rollInfluece, 0)
own['vehicle'].setRollInfluence(rollInfluece, 1)
own['vehicle'].setRollInfluence(rollInfluece, 2)
own['vehicle'].setRollInfluence(rollInfluece, 3)

compression = 0.0
own['vehicle'].setSuspensionCompression(compression, 0)
own['vehicle'].setSuspensionCompression(compression, 1)
own['vehicle'].setSuspensionCompression(compression, 2)
own['vehicle'].setSuspensionCompression(compression, 3)'''

First off, GameLogic is actually the logic module of the bge package(just as constraints is a module of the bge package).

so you could change the GameLogic import to:
from bge import logic
and change all the GameLogic. to logic. but this really isn’t the main problem.

I am very surprised there were no errors because there is no such thing as constraints.createVehicle(id) method (unless there is more code to this script that isn’t represented here?). There is, however, a
constraints.createConstraint(physics_id1, physics_id2, constraint_type, …)
which is what is needed.

You need to start off with 4 basic things for a vehicle:

  1. Vehicle physics ID - which is used to create the ---->
  2. Vehicle constraint - that allows you to get the ---->
  3. Vehicle constraint ID - which you need to finally get the ---->
  4. Vehicle - This is the thing you want.

In code similar to yours, this process may look like:

vehicle_phy_ID = own.getPhysicsId()
vehicle_ct = ct.createConstraint(vehicle_phy_ID, 0, ct.VEHICLE_CONSTRAINT) 
vehicle_ct_ID = vehicle_ct.getConstraintId() 
own['vehicle'] = ct.getVehicleConstraint(vehicle_ct_ID)

All of which you can do in the ‘init’ section

After that your vehicle should be working and ready to set up the suspension and controls.

1 Like