Skip to content

Commit cf63a6c

Browse files
Added another example showing usage of wheel devices
1 parent 670efb9 commit cf63a6c

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

samples/gamepad/steeringWheel.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/usr/bin/env python
2+
'''
3+
Demonstrate usage of steering wheels
4+
5+
In this sample you can use a wheel type device to control the camera and
6+
show some messages on screen. You can acclerate forward using the
7+
accleration pedal and slow down using the break pedal.
8+
'''
9+
10+
from direct.showbase.ShowBase import ShowBase
11+
from panda3d.core import TextNode, InputDevice, loadPrcFileData, Vec3
12+
from direct.gui.OnscreenText import OnscreenText
13+
14+
loadPrcFileData("", "notify-level-device debug")
15+
16+
class App(ShowBase):
17+
def __init__(self):
18+
ShowBase.__init__(self)
19+
# print all events sent through the messenger
20+
self.messenger.toggleVerbose()
21+
22+
self.lblWarning = OnscreenText(
23+
text = "No devices found",
24+
fg=(1,0,0,1),
25+
scale = .25)
26+
self.lblWarning.hide()
27+
28+
self.lblAction = OnscreenText(
29+
text = "Action",
30+
fg=(1,1,1,1),
31+
scale = .15)
32+
self.lblAction.hide()
33+
34+
self.checkDevices()
35+
36+
self.currentMoveSpeed = 0.0
37+
self.maxAccleration = 28.0
38+
self.deaccleration = 10.0
39+
self.deaclerationBreak = 37.0
40+
self.maxSpeed = 80.0
41+
42+
# Accept device dis-/connection events
43+
# NOTE: catching the events here will overwrite the accept in showbase, hence
44+
# we need to forward the event in the functions we set here!
45+
self.accept("connect-device", self.connect)
46+
self.accept("disconnect-device", self.disconnect)
47+
48+
self.accept("escape", exit)
49+
self.accept("flight_stick0-start", exit)
50+
51+
# Accept button events of the first connected steering wheel
52+
self.accept("steering_wheel0-action_a", self.doAction, extraArgs=[True, "Action"])
53+
self.accept("steering_wheel0-action_a-up", self.doAction, extraArgs=[False, "Release"])
54+
55+
self.environment = loader.loadModel("environment")
56+
self.environment.reparentTo(render)
57+
58+
self.wheelCenter = 0
59+
wheels = base.devices.getDevices(InputDevice.DC_steering_wheel)
60+
if len(wheels) > 0:
61+
for i in range(wheels[0].getNumControls()):
62+
if wheels[0].getControlMap(i) == InputDevice.C_wheel:
63+
self.wheelCenter = wheels[0].getControlState(i)
64+
65+
# disable pandas default mouse-camera controls so we can handle the camera
66+
# movements by ourself
67+
self.disableMouse()
68+
base.camera.setZ(2)
69+
70+
self.taskMgr.add(self.moveTask, "movement update task")
71+
72+
def connect(self, device):
73+
# we need to forward the event to the connectDevice function of showbase
74+
self.connectDevice(device)
75+
# Now we can check for ourself
76+
self.checkDevices()
77+
78+
def disconnect(self, device):
79+
# we need to forward the event to the disconnectDevice function of showbase
80+
self.disconnectDevice(device)
81+
# Now we can check for ourself
82+
self.checkDevices()
83+
84+
def checkDevices(self):
85+
# check if we have wheel devices connected
86+
if self.devices.get_devices(InputDevice.DC_steering_wheel):
87+
# we have at least one steering wheel device
88+
self.lblWarning.hide()
89+
else:
90+
# no devices connected
91+
self.lblWarning.show()
92+
93+
def doAction(self, showText, text):
94+
if showText and self.lblAction.isHidden():
95+
self.lblAction.show()
96+
else:
97+
self.lblAction.hide()
98+
99+
def moveTask(self, task):
100+
dt = globalClock.getDt()
101+
movementVec = Vec3()
102+
103+
wheels = base.devices.getDevices(InputDevice.DC_steering_wheel)
104+
if len(wheels) == 0:
105+
# savety check
106+
return task.cont
107+
108+
if self.currentMoveSpeed > 0:
109+
self.currentMoveSpeed -= dt * self.deaccleration
110+
if self.currentMoveSpeed < 0:
111+
self.currentMoveSpeed = 0
112+
113+
# we will use the first found wheel
114+
for i in range(wheels[0].getNumControls()):
115+
116+
if wheels[0].getControlMap(i) == InputDevice.C_accelerator:
117+
accleration = wheels[0].getControlState(i) * self.maxAccleration
118+
self.currentMoveSpeed += dt * accleration
119+
120+
if self.currentMoveSpeed > wheels[0].getControlState(i) * self.maxSpeed:
121+
self.currentMoveSpeed -= dt * self.deaccleration
122+
elif wheels[0].getControlMap(i) == InputDevice.C_brake:
123+
deacleration = wheels[0].getControlState(i) * self.deaclerationBreak
124+
self.currentMoveSpeed -= dt * deacleration
125+
126+
elif self.currentMoveSpeed < 0:
127+
self.currentMoveSpeed = 0
128+
129+
if wheels[0].getControlMap(i) == InputDevice.C_wheel:
130+
rotation = self.wheelCenter - wheels[0].getControlState(i)
131+
base.camera.setH(base.camera, 100 * dt * rotation)
132+
133+
# calculate movement
134+
base.camera.setY(base.camera, dt * self.currentMoveSpeed)
135+
136+
return task.cont
137+
138+
app = App()
139+
app.run()

0 commit comments

Comments
 (0)