[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Example of pymunk using images



Hi!

It is really not very hard to do when you understand the basic
principle used (same as in almost all physics engines). What is
displayed on screen does not have to be the same as what is used
inside pymunk, pymunk doesnt care how you draw the things it
simulates.

You will have 2 forms representing your object, one used for physics
and one used to display on screen. For example, lets say you have an
image of a head that you want to use. Then you create a pymunk Shape
that match the head as close as possible, if its a round head then
maybe a Circle will do, otherwise maybe you can do a Poly-shape. Then
pymunk with act on the Circle (or whatever shape it is). However, when
you want to draw it, you dont draw the Circle shape, instead you take
the image and draw it on the same position as the Circles body, with
the same rotation (and usually some offset to make the image line up
with the shape).

I should have added an example to pymunk a long time ago, because I
think its something many new to pymunk struggle with. However, that is
no more a problem, I created a small example just for you and checked
it in to the current trunk :)

Take a look here
http://code.google.com/p/pymunk/source/browse/trunk/examples/using_sprites.py
to run the example you also need the pymunk logo image that I used for
example, located here:
http://code.google.com/p/pymunk/source/browse/trunk/examples/pymunk_logo_googlecode.png

As you can see I used a simple triangle to simulate the form of the
snake and boxes, but if you have something else another shape might be
better, or you can even combine more than one shape on the same body
if your image is complicated (remember that Poly-shapes must be
convex)

I hope that clear things up!

/Victor

On Mon, Jun 28, 2010 at 3:40 AM, Dan Ross <dan@xxxxxxxxxxxxxxxxxx> wrote:
> Hi all-
>
> I'm thinking of experimenting with pymunk, but I'd like to use images,
> rather than shapes.
>
> Is there documentation or an example or two anyone could point me to?
>
> Thanks,
>
> Dan
>