[pymunk] access an array of bodies with numpy
Posted: Sun Jan 31, 2016 7:59 pm
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
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