I am getting this problem sometimes. When my vehicle stops, it tends to keep going slightly, like the wheels arent’t carrying almost any mass. This can probably be fixed with some tweaking. My main problem is that if the vehivle stops completely, ten it is as if it froze, meaning that I can’t move anymore, and the wheels turn very sluggishly. I don’t know what else to say about it, but here is the .blend file: PS: Does anyone know how to change the braking to, instead of “space”, an automatic brake when neither going forward or reversing? Here is my code:
from bge import logic, constraintsimport math
cont = logic.getCurrentController()
scene = logic.getCurrentScene()
own = cont.owner
chassis = own
w = cont.sensors["w"]
s = cont.sensors["s"]
a = cont.sensors["a"]
d = cont.sensors["d"]
space = cont.sensors["space"]
vehicleID = own.getPhysicsId()
smallWheels = ["Wheel_L_Front", "Wheel_R_Front", "Wheel_L_Rear", "Wheel_R_Rear"]
if not "init" in own:
vehicle = constraints.createVehicle(vehicleID)
#save wheel list
own["Wheels"] = []
for wheel in chassis.childrenRecursive:
if "wheel" in wheel:
#define parameters
wheelPos = chassis.worldOrientation.inverted() * (wheel.worldPosition - chassis.worldPosition)
downDir = [0.0, 0.0, -1.0]
axleDir = [-1.0, 0.0, 0.0]
#create the wheel
wheel.removeParent()
vehicle.addWheel(wheel, wheelPos, downDir, axleDir, 0.25, 1.19, False)
own["Wheels"].append(wheel)
#save vehicle
own["Vehicle"] = vehicle
for wheel in range(vehicle.getNumWheels()):
vehicle.setTyreFriction(10.0, wheel)
vehicle.setSuspensionDamping(1.5, wheel)
own["init"] = True
###########MOVEMENT#############
def main():
brakes = Brakes(own["Vehicle"])
Power(own["Vehicle"], brakes)
diameterSmall = 1.0
diameterBig = 2.0
for wheel in smallWheels:
if "Wheel_L" in wheel: #left side
rotSmall(scene.objects[wheel], diameterSmall, diameterBig, Average("L"))
if "Wheel_R" in wheel: #right side
rotSmall(scene.objects[wheel], diameterSmall, diameterBig, Average("R"))
def Brakes(vehicle):
BRAKE_POWER = 20.0
braking = False
brake = 0.0
#brakes
if not w.positive and not s.positive and not a.positive and not d.positive:
brake = BRAKE_POWER/2
braking = True
elif space.positive:
brake = BRAKE_POWER
braking = True
#no brakes
else:
brake = 0.0
braking = False
for wheel in range(vehicle.getNumWheels()):
vehicle.applyBraking(brake, wheel)
return braking
def Power(vehicle, braking):
GAS_POWER = 500.0
REVERSE_POWER = 300.0
#is braking
if braking == True:
power_L = 0.0
power_R = 0.0
#gas
elif w.positive:
#forward and left
if a.positive:
power_L = 0.0
power_R = -GAS_POWER * 2
#forward and right
elif d.positive:
power_L = -GAS_POWER * 2
power_R = 0.0
#straight forward
else:
power_L = power_R = -GAS_POWER
#reverse
elif s.positive:
#reverse and left
if a.positive:
power_L = 0.0
power_R = REVERSE_POWER * 2
#reverse and right
elif d.positive:
power_L = REVERSE_POWER * 2
power_R = 0.0
#straight reverse
else:
power_L = power_R = REVERSE_POWER
#left
elif a.positive:
power_L = 1000.0
power_R = -GAS_POWER * 2
#right
elif d.positive:
power_L = -GAS_POWER * 2
power_R = 1000.0
#no power
else:
power_L = power_R = 0.0
for wheel in range(vehicle.getNumWheels()):
if "Wheel_L" in own["Wheels"][wheel].name:
vehicle.applyEngineForce(power_L, wheel)
if "Wheel_R" in own["Wheels"][wheel].name:
vehicle.applyEngineForce(power_R, wheel)
def Average(side):
xList = []
for wheel in own["Wheels"]:
if "Wheel_" + side in wheel.name:
xList.append(wheel.localOrientation.to_euler()[0]) #append x rotation of wheel
average = sum(xList) / float(len(xList))
return average
def rotSmall(wheel, smallD, bigD, bigR):
smallR = (bigD * bigR)/smallD
rot = wheel.localOrientation.to_euler()
rot[0] = smallR
wheel.localOrientation = rot.to_matrix()
main()
Quite long, yes, if anyone has any ideas as to optimizing or wants to point out anything, feel free to do so.
Attachments
VehicleWrapperTest.blend (2.04 MB)