[pymunk] Access segment shape radius/thickness?

Official forum for the Chipmunk2D Physics Library.
Post Reply
brynjarh
Posts: 4
Joined: Mon Mar 02, 2009 12:27 pm
Contact:

[pymunk] Access segment shape radius/thickness?

Post 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"?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

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

Post 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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
viblo
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm
Contact:

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

Post 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
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
brynjarh
Posts: 4
Joined: Mon Mar 02, 2009 12:27 pm
Contact:

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

Post 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!
Post Reply

Who is online

Users browsing this forum: No registered users and 28 guests