Skip to content

Commit

Permalink
Fix drawing of polygon shapes with pyglet. Issue #109
Browse files Browse the repository at this point in the history
  • Loading branch information
viblo committed Mar 3, 2017
1 parent f80076b commit 1d00407
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 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.
1 change: 0 additions & 1 deletion examples/pyglet_util_demo.py
Expand Up @@ -3,7 +3,6 @@
See pygame_util_demo.py for a comparison to pygame.
"""

__version__ = "$Id:$"
__docformat__ = "reStructuredText"

import sys
Expand Down
6 changes: 3 additions & 3 deletions examples/shapes_for_draw_demos.py
Expand Up @@ -57,7 +57,7 @@ def fill_space(space, custom_color=(255,255,0,255)):
b = pymunk.Body(body_type=pymunk.Body.STATIC)
b.position = (50,430)
t = pymunk.Transform(ty=-100)
s = pymunk.Poly(b, [(0, -50), (50, 0), (30, 50),(-30, 50),(-50, 0)], t)
s = pymunk.Poly(b, [(0.0, -30.0), (19.0, -23.0), (30.0, -5.0), (26.0, 15.0), (10.0, 28.0), (-10.0, 28.0), (-26.0, 15.0), (-30.0, -5.0), (-19.0, -23.0)], t)
space.add(s)

### Kinematic
Expand Down Expand Up @@ -111,9 +111,9 @@ def fill_space(space, custom_color=(255,255,0,255)):
b = pymunk.Body(body_type=pymunk.Body.KINEMATIC)
b.position = (230,430)
t = pymunk.Transform(ty=-100)
s = pymunk.Poly(b, [(0, -50), (50, 0), (30, 50),(-30, 50),(-50, 0)], t)
s = pymunk.Poly(b, [(19.0, -50.0), (30.0, -5.0), (26.0, 15.0), (10.0, 38.0), (-10.0, 38.0), (-26.0, 15.0), (-30.0, -5.0), (-19.0, -50.0)], t)
space.add(s)

### Dynamic
captions.append(((390,680), "Dynamic Shapes"))

Expand Down
16 changes: 11 additions & 5 deletions pymunk/pyglet_util.py
Expand Up @@ -175,15 +175,21 @@ def draw_fat_segment(self, a, b, radius, outline_color, fill_color):
('c4B', fill_color.as_int() * l))

def draw_polygon(self, verts, radius, outline_color, fill_color):
ps = verts

mode = pyglet.gl.GL_TRIANGLE_STRIP
ps = [ps[1],ps[2], ps[0]] + ps[3:]

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

vs = []
for p in [ps[0]] + ps + [ps[-1]]:
vs += [p.x, p.y]
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]

vs = [vs[0], vs[1]] + vs + [vs[-2], vs[-1]]

l = len(vs)//2
self.batch.add(l, mode, None,
('v2f', vs),
Expand Down

0 comments on commit 1d00407

Please sign in to comment.