Page 1 of 1

Lots of questions about joints...

Posted: Thu Jan 26, 2012 10:36 pm
by hidas
I don't want to appear to forthright in my first post here, but I'm having lots of trouble getting some of the joints to work. I tried searching first but couldn't find anything that looked like it was related.
I'm using Python 2.7.2+ on Linux Mint 12 with Pymunk 2.1.0

Mainly I'm having trouble with Springs and Pivot joints. Here's some code I wrote:

Code: Select all

BALL = createBall(150, 150, mass=30, radius=50)
BALL2 = createBall(250, 250, mass=5, radius=10)
joint = pymunk.DampedSpring(BALL.body, BALL2.body, BALL.body.position, BALL2.body.position, 70, 50, 50)
The two balls jitter around for a few milliseconds, flip positions, and explode apart. I tried modifying the rest length, stiffness, and damping, but it just made it worse. I can move them around, and the small ball wiggles a little bit but doesn't move closer like it should. When the large ball gets really far away (like +400 or so) it starts shooting into the air and falling back down again repeatedly. If I change the small ball's mass to 20 they don't explode but still don't act like they are attached.

On Pivot joints, it just acts... weird. I switched joint=pymunk.DampedSpring with joint=pymunk.PivotJoint (removing the extraneous numbers, of course), and it basically does the same thing as the Damped Spring. BUT if I specify a pivot point like pymunk.Vec2d(200,200) instead of two anchors, it sort of behaves/

Can someone show me what I'm doing wrong? :|


Re: Lots of questions about joints...

Posted: Tue Jan 31, 2012 9:45 am
by slembcke
Ah! Sorry about the late reply on this. It came up right before we started the Global Game Jam, and I ended up forgetting about it.

I'm pretty sure it's because you are using the positions of the objects as their anchor points. 99.9% certain that you meant to use (0, 0) as the anchor point. That attaches the spring to the centers of the balls. The anchor points are relative to the position/rotation of the body. So by using the ball's position is a very bad thing! The farther the ball is away from the origin of space, the farther from the center of the ball the spring will be attached and will cause it to rotate around in crazy ways.

There is only one joint that can be optionally initialized with absolute coordinates, cpPivotJoint. It allows you to specify a single absolute point as the anchor point which is then converted to relative coordinates for setting the anchor points on the two bodies.

Re: Lots of questions about joints...

Posted: Tue Jan 31, 2012 10:20 am
by hidas
AHA! Thanks for the reply!
So I'm using world coordinates instead of body relative coordinates.
Trying again:

Code: Select all

BALL = createBall(150, 150, 30, 50)
BALL2 = createBall(250, 250, 20, 10)
anchor1 = pymunk.Vec2d(0, 0) #body relative, right?
anchor2 = pymunk.Vec2d(0, 0)
joint = pymunk.DampedSpring(BALL.body, BALL2.body, anchor2, \
                            anchor2, 70, 50, 50)
... it works!

Thanks! :)

Re: Lots of questions about joints...

Posted: Thu Feb 09, 2012 5:06 pm
by viblo
Small comment on your code: You do not need to explicitly create Vec2ds as anchor points, its perfectly fine to just send in a zero-tuple.

Code: Select all

joint = pymunk.DampedSpring(BALL.body, BALL2.body, (0,0), (0,0), 70, 50, 50)
I will try to update the API docs of pymunk to write out if positions should be in body local or world coordinates. The truth is I havent been 100% sure myself in all cases and took the easy way out and left it to the user to figure it out :)