From 3da928a4de888aa49ca3cdc914ccc2e34f7a15f2 Mon Sep 17 00:00:00 2001 From: fireclawthefox Date: Thu, 4 Aug 2016 22:55:39 +0200 Subject: [PATCH] changed axis state detection to new functions and exposed state classes --- samples/gamepad/steeringWheel.py | 37 ++++++++++++++++---------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/samples/gamepad/steeringWheel.py b/samples/gamepad/steeringWheel.py index 6353f24188..256c2e1eb3 100644 --- a/samples/gamepad/steeringWheel.py +++ b/samples/gamepad/steeringWheel.py @@ -55,12 +55,14 @@ class App(ShowBase): self.environment = loader.loadModel("environment") self.environment.reparentTo(render) + # save the center position of the wheel + # NOTE: here we assume, that the wheel is centered when the application get started. + # In real world applications, you should notice the user and give him enough time + # to center the wheel until you store the center position of the controler! self.wheelCenter = 0 wheels = base.devices.getDevices(InputDevice.DC_steering_wheel) if len(wheels) > 0: - for i in range(wheels[0].getNumControls()): - if wheels[0].getControlMap(i) == InputDevice.C_wheel: - self.wheelCenter = wheels[0].getControlState(i) + self.wheelCenter = wheels[0].findControl(InputDevice.C_wheel).state # disable pandas default mouse-camera controls so we can handle the camera # movements by ourself @@ -111,24 +113,21 @@ class App(ShowBase): self.currentMoveSpeed = 0 # we will use the first found wheel - for i in range(wheels[0].getNumControls()): + # Acclerate + accleration = wheels[0].findControl(InputDevice.C_accelerator).state * self.maxAccleration + if self.currentMoveSpeed > wheels[0].findControl(InputDevice.C_accelerator).state * self.maxSpeed: + self.currentMoveSpeed -= dt * self.deaccleration + self.currentMoveSpeed += dt * accleration - if wheels[0].getControlMap(i) == InputDevice.C_accelerator: - accleration = wheels[0].getControlState(i) * self.maxAccleration - self.currentMoveSpeed += dt * accleration + # Break + deacleration = wheels[0].findControl(InputDevice.C_brake).state * self.deaclerationBreak + self.currentMoveSpeed -= dt * deacleration + if self.currentMoveSpeed < 0: + self.currentMoveSpeed = 0 - if self.currentMoveSpeed > wheels[0].getControlState(i) * self.maxSpeed: - self.currentMoveSpeed -= dt * self.deaccleration - elif wheels[0].getControlMap(i) == InputDevice.C_brake: - deacleration = wheels[0].getControlState(i) * self.deaclerationBreak - self.currentMoveSpeed -= dt * deacleration - - elif self.currentMoveSpeed < 0: - self.currentMoveSpeed = 0 - - if wheels[0].getControlMap(i) == InputDevice.C_wheel: - rotation = self.wheelCenter - wheels[0].getControlState(i) - base.camera.setH(base.camera, 100 * dt * rotation) + # Steering + rotation = self.wheelCenter - wheels[0].findControl(InputDevice.C_wheel).state + base.camera.setH(base.camera, 100 * dt * rotation) # calculate movement base.camera.setY(base.camera, dt * self.currentMoveSpeed)