Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixes polygon with radius drawing. Issue #76
  • Loading branch information
viblo committed Apr 16, 2019
1 parent 51942bb commit 5eb4c0c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 53 deletions.
Binary file modified docs/src/_static/examples/pygame_util_demo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/src/_static/examples/pyglet_util_demo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 31 additions & 31 deletions examples/shapes_for_draw_demos.py
Expand Up @@ -51,7 +51,7 @@ def fill_space(space, custom_color=(255,255,0,255)):
b = pymunk.Body(body_type=pymunk.Body.STATIC)
b.position = (120,500)
t = pymunk.Transform(ty=-100)
s = pymunk.Poly(b, [(0, -25),(30, 25),(-30, 25)], t, radius=3)
s = pymunk.Poly(b, [(0, -25),(30, 25),(-30, 25)], t, radius=1)
space.add(s)

b = pymunk.Body(body_type=pymunk.Body.STATIC)
Expand Down Expand Up @@ -159,56 +159,49 @@ def fill_space(space, custom_color=(255,255,0,255)):

b = pymunk.Body(1,1)
b.position = (460,500)
s = pymunk.Poly(b, [(0, -25),(30, 25),(-30, 25)], pymunk.Transform(ty=-100), radius=3)
s = pymunk.Poly(b, [(0, -25),(30, 25),(-30, 25)],
pymunk.Transform(ty=-100), radius=5)
space.add(s)

b = pymunk.Body(1,1)
b.position = (400,430)
s = pymunk.Poly(b, [(0, -50), (50, 0), (30, 50),(-30, 50),(-50, 0)], pymunk.Transform(ty=-100))
s = pymunk.Poly(b, [(0, -50), (50, 0), (30, 50),(-30, 50),(-50, 0)],
pymunk.Transform(ty=-100))
space.add(s)

###Constraints

# PinJoints
captions.append(((560,660), "Pin Joints"))
a = pymunk.Body(1,1)
a.position = (550, 600)
sa = pymunk.Circle(a, 20)
b = pymunk.Body(1,1)
b.position = (650, 620)
sb = pymunk.Circle(b, 20)
j = pymunk.PinJoint(a, b)
space.add(sa, sb, a, b, j)

captions.append(((560,660), "Pin Joint"))
a = pymunk.Body(1,1)
a.position = (550,550)
a.position = (550,600)
sa = pymunk.Circle(a, 20)
b = pymunk.Body(1,1)
b.position = (650,570)
b.position = (650,620)
sb = pymunk.Circle(b, 20)
j = pymunk.PinJoint(a, b, anchor_a=(0,20), anchor_b=(0,-20))
j = pymunk.PinJoint(a, b, anchor_a=(0,0), anchor_b=(0,-20))
space.add(sa, sb, a, b, j)

# SlideJoints
captions.append(((560,490), "Slide Joint"))
captions.append(((560,560), "Slide Joint"))
a = pymunk.Body(1,1)
a.position = (550,430)
a.position = (550,500)
sa = pymunk.Circle(a, 20)
b = pymunk.Body(1,1)
b.position = (650,450)
b.position = (650,520)
sb = pymunk.Circle(b, 20)
j = pymunk.SlideJoint(a, b, anchor_a=(0,20), anchor_b=(0,-20), min=10, max=30)
space.add(sa, sb, a, b, j)

# PivotJoints
captions.append(((560,390), "Pivot Joint"))
captions.append(((560,460), "Pivot Joint"))
a = pymunk.Body(1,1)
a.position = (550,330)
a.position = (550,400)
sa = pymunk.Circle(a, 20)
b = pymunk.Body(1,1)
b.position = (650,350)
b.position = (650,420)
sb = pymunk.Circle(b, 20)
j = pymunk.PivotJoint(a, b, (600,320))
j = pymunk.PivotJoint(a, b, (600,390))
space.add(sa, sb, a, b, j)


Expand All @@ -220,7 +213,7 @@ def fill_space(space, custom_color=(255,255,0,255)):
b = pymunk.Body(1,1)
b.position = (850,620)
sb = pymunk.Circle(b, 20)
j = pymunk.GrooveJoint(a, b, (790,610), (790,620), (840,620))
j = pymunk.GrooveJoint(a, b, (40,40), (40,-40), (-60,0))
space.add(sa, sb, a, b, j)

# DampedSpring
Expand Down Expand Up @@ -277,28 +270,35 @@ def fill_space(space, custom_color=(255,255,0,255)):
### Other

# Objects in custom color
captions.append(((150,150), "Custom Color (static & dynamic)"))
captions.append(
((150,150), "Custom Color (static, kinematic & dynamic)"))
b = pymunk.Body(body_type=pymunk.Body.STATIC)
b.position = (200, 200)
s = pymunk.Circle(b, 40)
s = pymunk.Circle(b, 30)
s.color = custom_color
space.add(s)

b = pymunk.Body(body_type=pymunk.Body.KINEMATIC)
b.position = (300, 200)
s = pymunk.Circle(b, 30)
s.color = custom_color
space.add(s)

b = pymunk.Body(1, 1)
b.position = (300, 200)
s = pymunk.Circle(b, 40)
b.position = (400, 200)
s = pymunk.Circle(b, 30)
s.color = custom_color
space.add(s)

# Collision
captions.append(((450,150), "Collisions"))
captions.append(((550,150), "Collisions"))
b = pymunk.Body(body_type=pymunk.Body.STATIC)
b.position = (470, 200)
b.position = (570, 200)
s = pymunk.Circle(b, 40)
space.add(s)

b = pymunk.Body(1, 1)
b.position = (500, 250)
b.position = (590, 250)
s = pymunk.Circle(b, 40)
space.add(s)

Expand Down
34 changes: 18 additions & 16 deletions pymunk/pygame_util.py
Expand Up @@ -113,7 +113,7 @@ def __init__(self, surface):
def draw_circle(self, pos, angle, radius, outline_color, fill_color):
p = to_pygame(pos, self.surface)

pygame.draw.circle(self.surface, fill_color, p, int(radius), 0)
pygame.draw.circle(self.surface, fill_color, p, _rndint(radius), 0)

circle_edge = pos + Vec2d(radius, 0).rotated(angle)
p2 = to_pygame(circle_edge, self.surface)
Expand All @@ -129,13 +129,10 @@ def draw_segment(self, a, b, color):
def draw_fat_segment(self, a, b, radius, outline_color, fill_color):
p1 = to_pygame(a, self.surface)
p2 = to_pygame(b, self.surface)
def rndint(x): return int(round(x))
r = int(max(1, radius*2))

r = _rndint(max(1, radius*2))
pygame.draw.lines(self.surface, fill_color, False, [p1,p2], r)
if r > 2:
#pygame.draw.circle(self.surface, fill_color, p1, int(radius), 0)
#pygame.draw.circle(self.surface, fill_color, p2, int(radius), 0)

delta = ( p2[0]-p1[0], p2[1]-p1[1] )
orthog = [ delta[1], -delta[0] ]
scale = radius / (orthog[0]*orthog[0] + orthog[1]*orthog[1])**0.5
Expand All @@ -147,23 +144,27 @@ def rndint(x): return int(round(x))
( p2[0]-orthog[0], p2[1]-orthog[1] )
]
pygame.draw.polygon(self.surface, fill_color, points)
pygame.draw.circle(self.surface, fill_color, (rndint(p1[0]),rndint(p1[1])), rndint(radius))
pygame.draw.circle(self.surface, fill_color, (rndint(p2[0]),rndint(p2[1])), rndint(radius))
pygame.draw.circle(self.surface, fill_color,
(_rndint(p1[0]),_rndint(p1[1])), _rndint(radius))
pygame.draw.circle(self.surface, fill_color,
(_rndint(p2[0]),_rndint(p2[1])), _rndint(radius))

def draw_polygon(self, verts, radius, outline_color, fill_color):
ps = [to_pygame(v, self.surface) for v in verts]
ps += [ps[0]]

pygame.draw.polygon(self.surface, fill_color, ps)

if radius < 1 and False:
pygame.draw.lines(self.surface, outline_color, False, ps)
else:
pygame.draw.lines(self.surface, outline_color, False, ps, int(radius*2))

if radius > 0:
for i in range(len(verts)):
a = verts[i]
b = verts[(i+1) % len(verts)]
self.draw_fat_segment(a, b, radius, outline_color,
outline_color)

def draw_dot(self, size, pos, color):
p = to_pygame(pos, self.surface)
pygame.draw.circle(self.surface, color, p, int(size), 0)
pygame.draw.circle(self.surface, color, p, _rndint(size), 0)


def get_mouse_pos(surface):
Expand All @@ -189,4 +190,5 @@ def from_pygame(p, surface):
"""
return to_pygame(p,surface)


def _rndint(x):
return int(round(x))
25 changes: 20 additions & 5 deletions pymunk/pyglet_util.py
Expand Up @@ -157,10 +157,10 @@ def draw_fat_segment(self, a, b, radius, outline_color, fill_color):
pv1 = a
pv2 = b
d = pv2 - pv1
a = -math.atan2(d.x, d.y)
atan = -math.atan2(d.x, d.y)
radius = max(radius, 1)
dx = radius * math.cos(a)
dy = radius * math.sin(a)
dx = radius * math.cos(atan)
dy = radius * math.sin(atan)

p1 = pv1 + Vec2d(dx,dy)
p2 = pv1 - Vec2d(dx,dy)
Expand All @@ -174,17 +174,22 @@ def draw_fat_segment(self, a, b, radius, outline_color, fill_color):
('v2f', vs),
('c4B', fill_color.as_int() * l))

self.draw_circle(a, 0, radius, fill_color, fill_color)
self.draw_circle(b, 0, radius, fill_color, fill_color)

def draw_polygon(self, verts, radius, outline_color, fill_color):
mode = pyglet.gl.GL_TRIANGLE_STRIP

l = len(verts)
mid = len(verts) // 2

if radius >= 3:
#print("POLY", verts)
pass
vs = []
for i in range(mid):
vs += [verts[i].x, verts[i].y]
vs += [verts[l-1-i].x, verts[l-1-i].y]

if l%2:
vs += [verts[mid].x, verts[mid].y]

Expand All @@ -194,6 +199,16 @@ def draw_polygon(self, verts, radius, outline_color, fill_color):
self.batch.add(l, mode, None,
('v2f', vs),
('c4B', fill_color.as_int()*l))

if radius > 0:
for i in range(len(verts)):
a = verts[i]
b = verts[(i+1) % len(verts)]
#print(a, b)
self.draw_fat_segment(a, b, radius, outline_color, outline_color)




def draw_dot(self, size, pos, color):
# todo: optimize this functions
Expand Down
2 changes: 1 addition & 1 deletion tools/generate_docs.py
Expand Up @@ -2,7 +2,7 @@
import os

def main():
os.system('''sphinx-build -E -b html ../docs/src ../docs/html''')
os.system('''python -m sphinx -E -b html ../docs/src ../docs/html''')

if __name__ == "__main__":
sys.exit(main())
Expand Down

0 comments on commit 5eb4c0c

Please sign in to comment.