Hello!
I have written a data-oriented library for a game engine in python. It greatly reduces the time it takes to render objects by keeping an array of all entities and doing all the rotation and translation on contiguous c arrays with c functions rather than through iterating over python objects in a list. I have a previous project that uses pymunk that could benefit from this, and I have a question about the best way to integrate chipmunk.
Currently, to trigger the rendering I need to get all the updated positions and angles of bodies in the space through python iteration. Ie: `positions = numpy.array([ent.position for ent in entities])`. For hundreds of objects, this is hundreds of object attribute look ups in python and hundreds of python object to numpy dtype conversions. (obviously this isn't terribly slow).
Since the chipmunk bodies and shapes are c structs, it would be awesome to have an array of these structs where numpy could access the positions and angles based on offests in these structs. That is, I'd have a numpy array of chipmunk structs and I could access the positions using numpy slicing, Ie: `positions = bodies[:,'position']`.
My guess is that such a simple interface is not possible because the pymunk Body has a _body which is a ctypes pointer. Pymunk can't enforce all of the structs (specifically, _body.contents) being continuous in memory.
So, are there any thoughts on what would be the fastest way to get all of the positions into a contiguous C array in a predictable order? I'm sorry if this isn;t clear and I'd be happy to explain more if needed.
Thanks for reading,
Elliot
[pymunk] access an array of bodies with numpy
-
- Posts: 28
- Joined: Sun Nov 06, 2011 1:38 am
- Contact:
-
- Posts: 206
- Joined: Tue Aug 21, 2007 3:12 pm
- Contact:
Re: [pymunk] access an array of bodies with numpy
Hello!
I did some quick checks, and I think it might be difficult to get an array out of Chipmunk.
A Chipmunk cpSpace keeps the bodies in 3 arrays + a "sleeping list". You can see what it takes to iterate them in the cpSpaceEachBody function in cpSpace. If I understand that function properly sleeping bodies that become awake will change the ordering of the bodies, so it is not directly useful for your case.
Pymunk on the other hand have a list of Bodies in the Space.bodies. That is using a set as its storage, so it doesnt guarantee the item ordering.
So, in the end it seems like if you want a stable iteration of the bodies you have to do it yourself, just like you already done.
To optimize things I wonder if you could create a numpy array of pointers to cpBody objects (what's in Body._body). Then the ordering would be stable and you could do the numpy operations? But no, the actual data wouldnt be continuous in memory, only the pointers to data.
Another possibility would be to write a little bit of c code that creates an array each frame, and then you could interface with that with ctypes & numpy. Iterating all bodies once each frame should be very cheap if done from c code?
Regardless, if you come up with something it would be very interesting if you could post a followup here. At the moment Im working on upgrading pymunk to use the latest chipmunk so I dont have much time to do any experiments myself..
I did some quick checks, and I think it might be difficult to get an array out of Chipmunk.
A Chipmunk cpSpace keeps the bodies in 3 arrays + a "sleeping list". You can see what it takes to iterate them in the cpSpaceEachBody function in cpSpace. If I understand that function properly sleeping bodies that become awake will change the ordering of the bodies, so it is not directly useful for your case.
Pymunk on the other hand have a list of Bodies in the Space.bodies. That is using a set as its storage, so it doesnt guarantee the item ordering.
So, in the end it seems like if you want a stable iteration of the bodies you have to do it yourself, just like you already done.
To optimize things I wonder if you could create a numpy array of pointers to cpBody objects (what's in Body._body). Then the ordering would be stable and you could do the numpy operations? But no, the actual data wouldnt be continuous in memory, only the pointers to data.
Another possibility would be to write a little bit of c code that creates an array each frame, and then you could interface with that with ctypes & numpy. Iterating all bodies once each frame should be very cheap if done from c code?
Regardless, if you come up with something it would be very interesting if you could post a followup here. At the moment Im working on upgrading pymunk to use the latest chipmunk so I dont have much time to do any experiments myself..
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
-
- Posts: 1
- Joined: Thu Mar 10, 2016 2:29 am
- Contact:
Re: [pymunk] access an array of bodies with numpy
Elderly people should believe special care in monitoring their Grow XL lifestyle habits to ensure that they realize not become afflicted once vitriolic reflux. Left untreated mordant reflux can contribute to more immense result. A consistent condition will cause trenchant to be every time completion in the throat. http://jackedmuscleextremeadvice.com/grow-xl-male-enhancement/
Who is online
Users browsing this forum: No registered users and 8 guests