Attach two objects (chain? rope?..) in pymunk

Official forum for the Chipmunk2D Physics Library.
Angel
Posts: 17
Joined: Thu Feb 09, 2012 6:21 am
Contact:

Attach two objects (chain? rope?..) in pymunk

Post by Angel »

Hi there,

In my game, I have two objects moving around (only passive movement due to crashes and pushing forces of other events) of a specific type.

When those two objects collide I want them to "get married" so that they are attached to each other. How do I do this? Whenever I try to use joints (PinJoint, SlideJoint,...) the forces become crazy and the two objects display a very unpredictable movement. I just want them to be chained together with a chain of lenght 0. Also, where do I do it? On a collision callback?

The only thing that works to me is to use a String to join them. But the String is extremelly flexible and does not fit to my goals.

I am using pymunk.

Any help would be very much appreciated.
Thanks!!
Angel.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Attach two objects (chain? rope?..) in pymunk

Post by slembcke »

If you set the joint up so that it forces the shapes to overlap then the collision and the joint will fight and jitter a lot. You can set the shapes to be in a collision group so that they won't collide with each other though.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Angel
Posts: 17
Joined: Thu Feb 09, 2012 6:21 am
Contact:

Re: Attach two objects (chain? rope?..) in pymunk

Post by Angel »

I pivotjoin them using as the position the point where they got in touch (collision). At first the two shapes start "traveling" together. But very soon, they start making unpredictive movements and they spin around like they were mad.

Is it then that they are trying to avoid a collision while forced to overlap? I wish they would stay as in the first 2 seconds after their joint.

I would like them to collide though so wouln't want to group them.

Angel!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Attach two objects (chain? rope?..) in pymunk

Post by slembcke »

The shapes are overlapping slightly when the collision callback happens, so adding a pivot joint at that point would be bad. Both the joint and the collision will end up applying successively larger impulses as they try to outdo the other.

You either need to set up the joint to allow them to move apart slightly first or you could set a max force on the pivot joint. The first solution is still preferable though.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Angel
Posts: 17
Joined: Thu Feb 09, 2012 6:21 am
Contact:

Re: Attach two objects (chain? rope?..) in pymunk

Post by Angel »

OK! That sounds good.
So you mean that if the collision is at point P, I should look for a point Q which is nearby P but is not inside any of the two shapes I want to attach. Is that right?
Thanks,
A
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Attach two objects (chain? rope?..) in pymunk

Post by slembcke »

So there are two constructors for a pivot joint. One where you give it two local anchors, and the more common one where you give it an absolute coordinate figures out the two local anchors for you. When you use the form where you give it the two anchors you need to make sure that the anchors line up initially or it will cause the joint to pop back together.

In addition to the contact point for a collision, you can also get the collision normal and overlap distance of the two shapes. After converting the contact point relative to each body using cpBodyWorld2Local() you can offset it using the normal and distance of the contact point to push it out of the shapes. These two collision points won't quite line up any more, but that's okay because it's only going to be by as much as the collision will push them apart anyway.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Angel
Posts: 17
Joined: Thu Feb 09, 2012 6:21 am
Contact:

Re: Attach two objects (chain? rope?..) in pymunk

Post by Angel »

Very many thanks for the reply.

Just to clarify (I am a bit confused): so I need to calculate the points that are close to the collision point but off the shapes (or just at the edge of the shape?) so that I can use them for the pivot joint. Is that right?

As far as I see, pymunk cannot give you the normal.
Should I calculate both points then? Or just one could do?

Sorry for insisting and many thanks again,
A!!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Attach two objects (chain? rope?..) in pymunk

Post by slembcke »

Hrm. I thought I remember Viblio asking how to best expose the rest of the collision data to pymunk a couple years ago.

If you can't get the collision normal/depth you are going to have to figure out some other way of doing it or use the max force hack.
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: Attach two objects (chain? rope?..) in pymunk

Post by viblo »

All relevant collision info is available from pymunk (I hope!). Each collision callback will give you an Arbiter, and each arbiter has a contact array of Contact objects with normal, distance and position for each contact.

For an example take a look at the draw_collsion() collsion handler in this example: http://code.google.com/p/pymunk/source/ ... riction.py (uses the position and distance properties of a contact)
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
Angel
Posts: 17
Joined: Thu Feb 09, 2012 6:21 am
Contact:

Re: Attach two objects (chain? rope?..) in pymunk

Post by Angel »

Hi Viblio,

Sorry I say that. I was obviously incorrect! I only looked at the Arbiter class and hoped to see it there, but the Contact class has all the info needed.

Will do that then.

Thanks very much for your comments.
And thanks for Chi/Py-munk

A!
Post Reply

Who is online

Users browsing this forum: No registered users and 35 guests