-
Notifications
You must be signed in to change notification settings - Fork 1
/
ball.py
55 lines (45 loc) · 1.46 KB
/
ball.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
from nodebox.graphics import *
from math import sin, cos
import Box2D as physics
class Ball(object):
def __init__(self, world, x0, y0, theta0):
# constants
self.radius = 0.5
# coordinates for drawing
self.theta = theta0
self.x = x0
self.y = y0
# first define a body
bodyDef = physics.b2BodyDef()
bodyDef.position = (self.x, self.y)
# create body in our world
self.body = world.CreateBody(bodyDef)
# then define the attached shape
# (it's a circle)
shapeDef = physics.b2CircleDef()
shapeDef.radius = self.radius
shapeDef.density = 3
# create the shape
self.shape = self.body.CreateShape(shapeDef)
# attach a mass
self.body.SetMassFromShapes()
# fixtures
self.shape.friction = 1.0
self.shape.restitution = 0.1
# initial angular velocity
self.body.angularVelocity = -30.0
def update(self):
# drawing pos equals body pos
self.x = self.body.position.x
self.y = self.body.position.y
self.theta = self.body.angle
def draw(self, system):
x = system.get_screen_x(self.x)
y = system.get_screen_y(self.y)
r = system.get_screen_dim(self.radius)
nostroke()
fill(0.0, 0.5, 0.75, 0.5)
ellipse(x, y, r*2, r*2)
fill(0)
stroke(0)
line(x, y, x+cos(self.theta)*r, y+sin(self.theta)*r)