/
19_BallInMaze.py
196 lines (154 loc) · 6.61 KB
/
19_BallInMaze.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#from panda3d.core import load_prc_file_data
#load_prc_file_data('', 'load-display tinydisplay')
import sys
from direct.showbase.ShowBase import ShowBase
from direct.showbase.InputStateGlobal import inputState
from direct.interval.MetaInterval import Sequence
from direct.interval.MetaInterval import Parallel
from direct.interval.LerpInterval import LerpFunc
from direct.interval.FunctionInterval import Func
from direct.interval.FunctionInterval import Wait
from panda3d.core import AmbientLight
from panda3d.core import DirectionalLight
from panda3d.core import Material
from panda3d.core import LVector3
from panda3d.core import LVector4
from panda3d.core import LPoint3
from panda3d.bullet import BulletWorld
from panda3d.bullet import BulletRigidBodyNode
from panda3d.bullet import BulletGhostNode
from panda3d.bullet import BulletDebugNode
from panda3d.bullet import BulletHelper
class Game(ShowBase):
def __init__(self):
ShowBase.__init__(self)
base.set_background_color(0.1, 0.1, 0.8, 1)
base.set_frame_rate_meter(True)
base.cam.set_pos_hpr(0, 0, 25, 0, -90, 0)
base.disable_mouse()
# Input
self.accept('escape', self.exitGame)
self.accept('f1', base.toggle_wireframe)
self.accept('f2', base.toggle_texture)
self.accept('f3', self.toggle_debug)
self.accept('f5', self.do_screenshot)
# Setup scene 1: World
self.debugNP = render.attach_new_node(BulletDebugNode('Debug'))
self.debugNP.node().show_wireframe(True)
self.debugNP.node().show_constraints(True)
self.debugNP.node().show_bounding_boxes(True)
self.debugNP.node().show_normals(True)
self.debugNP.show()
self.world = BulletWorld()
self.world.set_gravity(LVector3(0, 0, -9.81))
self.world.set_debug_node(self.debugNP.node())
# Setup scene 2: Ball
#visNP = loader.load_model('models/ball.egg')
visNP = loader.load_model('samples/ball-in-maze/models/ball.egg.pz')
visNP.clear_model_nodes()
bodyNPs = BulletHelper.from_collision_solids(visNP, True)
self.ballNP = bodyNPs[0]
self.ballNP.reparent_to(render)
self.ballNP.node().set_mass(1.0)
self.ballNP.set_pos(4, -4, 1)
self.ballNP.node().set_deactivation_enabled(False)
visNP.reparent_to(self.ballNP)
# Setup scene 3: Maze
visNP = loader.load_model('models/maze.egg')
#visNP = loader.load_model('samples/ball-in-maze/models/maze.egg.pz')
visNP.clear_model_nodes()
visNP.reparent_to(render)
self.holes = []
self.maze = []
self.mazeNP = visNP
bodyNPs = BulletHelper.from_collision_solids(visNP, True);
for bodyNP in bodyNPs:
bodyNP.reparent_to(render)
if isinstance(bodyNP.node(), BulletRigidBodyNode):
bodyNP.node().set_mass(0.0)
bodyNP.node().set_kinematic(True)
self.maze.append(bodyNP)
elif isinstance(bodyNP.node(), BulletGhostNode):
self.holes.append(bodyNP)
# Lighting and material for the ball
ambientLight = AmbientLight('ambientLight')
ambientLight.set_color(LVector4(0.55, 0.55, 0.55, 1))
directionalLight = DirectionalLight('directionalLight')
directionalLight.set_direction(LVector3(0, 0, -1))
directionalLight.set_color(LVector4(0.375, 0.375, 0.375, 1))
directionalLight.set_specular_color(LVector4(1, 1, 1, 1))
self.ballNP.set_light(render.attach_new_node(ambientLight))
self.ballNP.set_light(render.attach_new_node(directionalLight))
m = Material()
m.set_specular(LVector4(1,1,1,1))
m.set_shininess(96)
self.ballNP.set_material(m, 1)
# Startup
self.start_game()
def exitGame(self):
sys.exit()
def toggle_debug(self):
if self.debugNP.is_hidden():
self.debugNP.show()
else:
self.debugNP.hide()
def do_screenshot(self):
base.screenshot('Bullet')
def start_game(self):
self.ballNP.set_pos(4, -4, 1)
self.ballNP.node().set_linear_velocity(LVector3(0, 0, 0))
self.ballNP.node().set_angular_velocity(LVector3(0, 0, 0))
# Mouse
p = base.win.get_properties()
base.win.move_pointer(0, int(p.get_x_size()/2), int(p.get_y_size()/2))
# Add bodies and ghosts
self.world.attach(self.ballNP.node())
for bodyNP in self.maze:
self.world.attach(bodyNP.node())
for ghostNP in self.holes:
self.world.attach(ghostNP.node())
# Simulation task
taskMgr.add(self.update_game, 'updateGame')
def stop_game(self):
# Remove bodies and ghosts
self.world.remove(self.ballNP.node())
for bodyNP in self.maze:
self.world.remove(bodyNP.node())
for ghostNP in self.holes:
self.world.remove(ghostNP.node())
# Simulation task
taskMgr.remove('updateGame')
def update_game(self, task):
dt = globalClock.get_dt()
# Get mouse position and tilt maze
if base.mouseWatcherNode.hasMouse():
mpos = base.mouseWatcherNode.get_mouse()
hpr = LVector3(0, mpos.y * -10, mpos.x * 10)
# Maze visual node
self.mazeNP.set_hpr(hpr)
# Maze body nodes
for bodyNP in self.maze:
bodyNP.set_hpr(hpr)
# Update simulation
self.world.do_physics(dt)
# Check if ball is touching a hole
for holeNP in self.holes:
if holeNP.node().get_num_overlapping_nodes() > 2:
if self.ballNP.node() in holeNP.node().get_overlapping_nodes():
self.lose_game(holeNP)
return task.cont
def lose_game(self, holeNP):
toPos = holeNP.node().get_shape_pos(0)
self.stop_game()
Sequence(
Parallel(
LerpFunc(self.ballNP.set_x, fromData = self.ballNP.get_x(),
toData = toPos.get_x(), duration = .1),
LerpFunc(self.ballNP.set_y, fromData = self.ballNP.get_y(),
toData = toPos.get_y(), duration = .1),
LerpFunc(self.ballNP.set_z, fromData = self.ballNP.get_z(),
toData = self.ballNP.get_z() - .9, duration = .2)),
Wait(1),
Func(self.start_game)).start()
game = Game()
game.run()