stampson
unread,Sep 1, 2008, 5:06:53 PM9/1/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to pyglet-users
I've been working with pyglet and pymunk with pyglet sprites, and
discovered that the major hit to framerate is the updating of the
sprite's x, y, and rotation. When using pymunk, it handles the
calculations of bodies's locations and rotations, and the pyglet app
is responsible for representation. Synchronization of the positions
of the sprites must occur regularly. After subclassing
pyglet.sprite.Sprite to streamline updates, I saw significant increase
in my framerate. I took advantage of the fact that updating the x, y,
and rotation simultaneously eliminates multiple calls to
pyglet.sprite.Sprite._update_position, and the fact that pymunk uses
radians for rotation and so does pyglet internally.
If anybody else finds it useful:
class MunkSprite(pyglet.sprite.Sprite):
'''
set sprite's x, y, and rotation (in radians)
'''
def set_position_and_rotation(self, x, y, rotation):
self._x = x
self._y = y
self._rotation = rotation
self._update_position()
def _update_position(self):
img = self._texture
if not self._visible:
self._vertex_list.vertices[:] = [0, 0, 0, 0, 0, 0, 0, 0]
elif self._rotation:
x1 = -img.anchor_x * self._scale
y1 = -img.anchor_y * self._scale
x2 = x1 + img.width * self._scale
y2 = y1 + img.height * self._scale
x = self._x
y = self._y
cr = cos(self._rotation)
sr = sin(self._rotation)
ax = int(x1 * cr - y1 * sr + x)
ay = int(x1 * sr + y1 * cr + y)
bx = int(x2 * cr - y1 * sr + x)
by = int(x2 * sr + y1 * cr + y)
cx = int(x2 * cr - y2 * sr + x)
cy = int(x2 * sr + y2 * cr + y)
dx = int(x1 * cr - y2 * sr + x)
dy = int(x1 * sr + y2 * cr + y)
self._vertex_list.vertices[:] = [ax, ay, bx, by, cx, cy,
dx, dy]
elif self._scale != 1.0:
x1 = int(self._x - img.anchor_x * self._scale)
y1 = int(self._y - img.anchor_y * self._scale)
x2 = int(x1 + img.width * self._scale)
y2 = int(y1 + img.height * self._scale)
self._vertex_list.vertices[:] = [x1, y1, x2, y1, x2, y2,
x1, y2]
else:
x1 = int(self._x - img.anchor_x)
y1 = int(self._y - img.anchor_y)
x2 = x1 + img.width
y2 = y1 + img.height
self._vertex_list.vertices[:] = [x1, y1, x2, y1, x2, y2,
x1, y2]