-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemolib.py
92 lines (71 loc) · 2.46 KB
/
demolib.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
import time
import pygame.display
from OpenGL.GL import (
GL_TRIANGLE_STRIP, GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT,
glPushMatrix, glPopMatrix, glColor, glClear,
glBegin, glEnd, glTranslate, glVertex)
from OpenGL.GLU import gluNewQuadric, gluSphere
from bullet.bullet import (
Vector3, Transform, BoxShape, SphereShape, DefaultMotionState, RigidBody)
class Ground:
def __init__(self):
self.boxHalfExtents = Vector3(20, 2, 20)
groundShape = BoxShape(self.boxHalfExtents)
groundTransform = Transform()
groundTransform.setIdentity()
groundTransform.setOrigin(Vector3(0, -4, 0))
groundMotion = DefaultMotionState()
groundMotion.setWorldTransform(groundTransform)
self.body = RigidBody(groundMotion, groundShape)
self.body.setRestitution(0.5)
self.motion = groundMotion
def render(self):
x, y, z = (
self.boxHalfExtents.x, self.boxHalfExtents.y, self.boxHalfExtents.z)
o = self.motion.getWorldTransform().getOrigin()
glColor(0, 0, 255)
glTranslate(o.x, o.y, o.z)
glBegin(GL_TRIANGLE_STRIP)
glVertex(-x, y, -z)
glVertex(x, y, -z)
glVertex(-x, y, z)
glVertex(x, y, z)
glEnd()
class Ball:
def __init__(self, position, color, radius=2):
self.radius = radius
ballShape = SphereShape(self.radius)
ballTransform = Transform()
ballTransform.setIdentity()
ballTransform.setOrigin(position)
ballMotion = DefaultMotionState()
ballMotion.setWorldTransform(ballTransform)
self.body = RigidBody(ballMotion, ballShape, 2.0)
self.body.setRestitution(0.9)
self.motion = ballMotion
self.quad = gluNewQuadric()
self.color = color
def render(self):
o = self.motion.getWorldTransform().getOrigin()
glColor(*self.color)
glTranslate(o.x, o.y, o.z)
gluSphere(self.quad, self.radius, 25, 25)
def step(world):
timeStep = fixedTimeStep = 1.0 / 60.0
world.stepSimulation(timeStep, 1, fixedTimeStep)
now = time.time()
delay = now % timeStep
time.sleep(delay)
def render(objects):
glPushMatrix()
for o in objects:
glPushMatrix()
o.render()
glPopMatrix()
glPopMatrix()
pygame.display.flip()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
def simulate(world, objects):
while True:
step(world)
render(objects)