Page 1 of 1

[pymunk] Access segment shape radius/thickness?

Posted: Thu Apr 09, 2009 2:32 pm
by brynjarh
How do I access the radius/thickness of a segment shape?

I create it here:

Code: Select all

def createLine():
    radius = randrange(1,5)
    mass = 10.0
    body = pm.Body(mass, 10000)
    body.position = randrange(0,640), randrange(0,400)
    shape = pm.Segment(body, (randrange(-200,-10), 0), (randrange(10,200), 0.0), radius)
    space.add(body, shape)
Loop through space.shapes and try to access radius:

Code: Select all

if isinstance(shape, pm.Segment):
    print shape.radius
Which doesn't work of course.

Think a solution is presented here: http://www.slembcke.net/forums/viewtopi ... dius#p1223 , but I don't understand it: "cast your shape to (cpSegmentShape*) to get access to cpVect a, b, n and cpFloat r (thickness)", what's "casting a shape"?

Re: [pymunk] Access segment shape radius/thickness?

Posted: Thu Apr 09, 2009 3:45 pm
by slembcke
I don't think that pymunk exposes information like that. In the C API, the shape structs are opaque so you can't get that information either. You can if you cast the shapes to the more specific type, but this is not something that I recommend in general.

If you need to remember the radius later, store it somewhere yourself.

Re: [pymunk] Access segment shape radius/thickness?

Posted: Mon Apr 13, 2009 5:21 pm
by viblo
The Segment shape in pymunk doesnt expose a radius property (noone has asked for it before :)). I have added the property to the Segment class in trunk for pymunk => it will be included in the next release. Until then you can write a little code to do it yourself like in this example:

Code: Select all

>>> import pymunk
>>> b = pymunk.Body(10, 10)
>>> s = pymunk.Segment(b, (0,0), (10,0), 3)
>>> import ctypes
>>> import pymunk._chipmunk as cp
>>> ctypes.cast(s._shape, ctypes.POINTER(cp.cpSegmentShape)).contents.r
3.0

Re: [pymunk] Access segment shape radius/thickness?

Posted: Fri Apr 24, 2009 10:27 am
by brynjarh
viblo wrote:I have added the property to the Segment class in trunk for pymunk => it will be included in the next release.
Wow, thanks!