-
Notifications
You must be signed in to change notification settings - Fork 1
/
20_BowlAndEggs.py
195 lines (150 loc) · 5.88 KB
/
20_BowlAndEggs.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
#from panda3d.core import load_prc_file_data
#load_prc_file_data('', 'load-display tinydisplay')
import sys
import random
from direct.showbase.ShowBase import ShowBase
from direct.showbase.InputStateGlobal import inputState
from panda3d.core import AmbientLight
from panda3d.core import DirectionalLight
from panda3d.core import LVector3
from panda3d.core import LVector4
from panda3d.core import LPoint3
from panda3d.core import TransformState
from panda3d.core import BitMask32
from panda3d.bullet import BulletWorld
from panda3d.bullet import BulletRigidBodyNode
from panda3d.bullet import BulletDebugNode
from panda3d.bullet import BulletPlaneShape
from panda3d.bullet import BulletConvexHullShape
from panda3d.bullet import BulletTriangleMesh
from panda3d.bullet import BulletTriangleMeshShape
from panda3d.bullet import ZUp
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(0, -10, 5)
base.cam.look_at(0, 0, 0.2)
# Light
alight = AmbientLight('ambientLight')
alight.set_color(LVector4(0.5, 0.5, 0.5, 1))
alightNP = render.attach_new_node(alight)
dlight = DirectionalLight('directionalLight')
dlight.set_direction(LVector3(1, 1, -1))
dlight.set_color(LVector4(0.7, 0.7, 0.7, 1))
dlightNP = render.attach_new_node(dlight)
render.clear_light()
render.set_light(alightNP)
render.set_light(dlightNP)
# Input
self.accept('escape', self.do_exit)
self.accept('r', self.do_reset)
self.accept('f1', base.toggle_wireframe)
self.accept('f2', base.toggle_texture)
self.accept('f3', self.toggle_debug)
self.accept('f5', self.do_screenshot)
inputState.watchWithModifiers('up', 'w')
inputState.watchWithModifiers('left', 'a')
inputState.watchWithModifiers('down', 's')
inputState.watchWithModifiers('right', 'd')
# Task
taskMgr.add(self.update, 'updateWorld')
# Physics
self.setup()
# _____HANDLER_____
def do_exit(self):
self.cleanup()
sys.exit(1)
def do_reset(self):
self.cleanup()
self.setup()
def toggle_debug(self):
if self.debugNP.is_hidden():
self.debugNP.show()
else:
self.debugNP.hide()
def do_screenshot(self):
base.screenshot('Bullet')
# ____TASK___
def process_input(self, dt):
force = LVector3(0, 0, 0)
if inputState.isSet('up'): force.y = 1.0
if inputState.isSet('down'): force.y = -1.0
if inputState.isSet('left'): force.x = -1.0
if inputState.isSet('right'): force.x = 1.0
force *= 300.0
self.bowlNP.node().set_active(True)
self.bowlNP.node().apply_central_force(force)
def update(self, task):
dt = globalClock.get_dt()
self.process_input(dt)
self.world.do_physics(dt)
return task.cont
def cleanup(self):
self.world = None
self.worldNP.remove_node()
def setup(self):
self.worldNP = render.attach_new_node('World')
# World
self.debugNP = self.worldNP.attach_new_node(BulletDebugNode('Debug'))
self.debugNP.show()
self.debugNP.node().show_wireframe(True)
self.debugNP.node().show_constraints(True)
self.debugNP.node().show_bounding_boxes(False)
self.debugNP.node().show_normals(False)
self.world = BulletWorld()
self.world.set_gravity(LVector3(0, 0, -9.81))
self.world.set_debug_node(self.debugNP.node())
# Ground
shape = BulletPlaneShape(LVector3(0, 0, 1), 0)
body = BulletRigidBodyNode('Ground')
bodyNP = self.worldNP.attach_new_node(body)
bodyNP.node().add_shape(shape)
bodyNP.set_pos(0, 0, 0)
bodyNP.set_collide_mask(BitMask32.all_on())
self.world.attach(bodyNP.node())
# Bowl
visNP = loader.load_model('models/bowl.egg')
geom = (visNP.findAllMatches('**/+GeomNode')
.get_path(0).node().get_geom(0))
mesh = BulletTriangleMesh()
mesh.addGeom(geom)
shape = BulletTriangleMeshShape(mesh, dynamic=True)
body = BulletRigidBodyNode('Bowl')
bodyNP = self.worldNP.attach_new_node(body)
bodyNP.node().add_shape(shape)
bodyNP.node().set_mass(10.0)
bodyNP.set_pos(0, 0, 0)
bodyNP.set_collide_mask(BitMask32.all_on())
self.world.attach(bodyNP.node())
visNP.reparent_to(bodyNP)
self.bowlNP = bodyNP
self.bowlNP.set_scale(2)
# Eggs
self.eggNPs = []
for i in range(5):
x = random.gauss(0, 0.1)
y = random.gauss(0, 0.1)
z = random.gauss(0, 0.1) + 1
h = random.random() * 360
p = random.random() * 360
r = random.random() * 360
visNP = loader.load_model('models/egg.egg')
geom = (visNP.find_all_matches('**/+GeomNode')
.get_path(0).node().get_geom(0))
shape = BulletConvexHullShape()
shape.addGeom(geom)
body = BulletRigidBodyNode('Egg-%i' % i)
bodyNP = self.worldNP.attach_new_node(body)
bodyNP.node().set_mass(1.0)
bodyNP.node().add_shape(shape)
bodyNP.node().set_deactivation_enabled(False)
bodyNP.set_collide_mask(BitMask32.all_on())
bodyNP.set_pos_hpr(x, y, z, h, p, r)
#bodyNP.set_scale(1.5)
self.world.attach(bodyNP.node())
visNP.reparent_to(bodyNP)
self.eggNPs.append(bodyNP)
game = Game()
game.run()