I am trying to create a quick and dirty driving game for my 5 yr old son (while learning to be a better Panda3D programmer). Anyway, I have a bullet vehicle that moves fine with the keyboard. I am trying to add gamepad control. I used debug print statements to verify that I can receive input from the gamepad. The buttons work fine because they are the same as a keyboard keystroke. But the toggle stick logic is not working in my code. No error msg. Just does not respond.
# Chassis
shape = BulletBoxShape(Vec3(0.6, 1.4, 0.5))
ts = TransformState.makePos(Point3(0, 0, 0.5))
np = self.worldNP.attachNewNode(BulletRigidBodyNode('Vehicle'))
np.node().addShape(shape, ts)
np.setPos(80, 50, 1)
np.node().setMass(800.0)
np.node().setDeactivationEnabled(False)
self.world.attachRigidBody(np.node())
# Vehicle
self.vehicle = BulletVehicle(self.world, np.node())
self.vehicle.setCoordinateSystem(ZUp)
self.world.attachVehicle(self.vehicle)
self.yugoNP = loader.loadModel('models/yugo/yugo.egg')
self.yugoNP.reparentTo(np)
inputState.watchWithModifiers('forward', 'w')
inputState.watchWithModifiers('left', 'a')
inputState.watchWithModifiers('reverse', 's')
inputState.watchWithModifiers('right', 'd')
inputState.watchWithModifiers('turnLeft', 'q')
inputState.watchWithModifiers('turnRight', 'e')
inputState.watchWithModifiers('brakes', 'x')
inputState.watchWithModifiers('brakes', 'gamepad-rshoulder')
inputState.watchWithModifiers('brakes', 'gamepad-lshoulder')
#Accept button events of the first connected gamepad
self.accept("gamepad-back", self.doExit)
self.accept("gamepad-start", self.doReset)
def processInput(self, dt):
engineForce = 0.0
brakeForce = 0.0
if inputState.isSet('forward'):
engineForce = 1000.0
brakeForce = 0.0
if inputState.isSet('reverse'):
engineForce = -500.0
brakeForce = 0.0
if inputState.isSet('brakes'):
engineForce = 0.0
brakeForce = 100.0
if inputState.isSet('turnLeft'):
self.steering += dt * self.steeringIncrement
self.steering = min(self.steering, self.steeringClamp)
if inputState.isSet('turnRight'):
self.steering -= dt * self.steeringIncrement
self.steering = max(self.steering, -self.steeringClamp)
# Apply steering to front wheels
self.vehicle.setSteeringValue(self.steering, 0);
self.vehicle.setSteeringValue(self.steering, 1);
# Apply engine and brake to rear wheels
self.vehicle.applyEngineForce(engineForce, 2);
self.vehicle.applyEngineForce(engineForce, 3);
self.vehicle.setBrake(brakeForce, 2);
self.vehicle.setBrake(brakeForce, 3);
This is only snippets of relevent code and words fine as is.
The following was my attempt to drive the car. DOES NOT WORK.
The print statement does work.
left_x = self.gamepad.findAxis(InputDevice.Axis.left_x)
left_y = self.gamepad.findAxis(InputDevice.Axis.left_y)
right_x = self.gamepad.findAxis(InputDevice.Axis.right_x)
right_y = self.gamepad.findAxis(InputDevice.Axis.right_y)
#print(left_x.value, left_y.value, right_x.value, right_y.value)
engineForce = 0.0
if left_y.value > 0.0:
print("Left Toggle Forward")
engineForce = left_y.value * 1200
brakeForce = 0.0
self.vehicle.applyEngineForce(engineForce, 2);
self.vehicle.applyEngineForce(engineForce, 3);
self.vehicle.setBrake(brakeForce, 2);
self.vehicle.setBrake(brakeForce, 3);