Lots of questions about joints...

Official forum for the Chipmunk2D Physics Library.

Lots of questions about joints...

Postby hidas » Thu Jan 26, 2012 10:36 pm

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)
space.add(joint)


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? :|

-hidas
hidas
 
Posts: 4
Joined: Thu Jan 26, 2012 10:07 pm

Re: Lots of questions about joints...

Postby slembcke » Tue Jan 31, 2012 9:45 am

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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Lots of questions about joints...

Postby hidas » Tue Jan 31, 2012 10:20 am

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)
space.add(joint)

... it works!

Thanks! :)
hidas
 
Posts: 4
Joined: Thu Jan 26, 2012 10:07 pm

Re: Lots of questions about joints...

Postby viblo » Thu Feb 09, 2012 5:06 pm

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 :)
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
viblo
 
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm


Return to Chipmunk2D Physics

Who is online

Users browsing this forum: No registered users and 0 guests