[pymunk] Retrieve force from Arbiter when no gravity

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

[pymunk] Retrieve force from Arbiter when no gravity

Post by Angel »

Hi,

I have a few circles in a space with no gravity. Once in a while I create a new one. When the new one collides with existing circles, they move apart. I need the force (or pressure) in those bodies during that "moving apart" steps.

I can set a collision_handler and then read the arbiter, but the impulse value is always a zero vector (algthough the bodies move). I am missing something but I cannot figure out what it is.

Could you help me calculating the force that the engine is applying to those bodies in order to move them apart?

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

Re: [pymunk] Retrieve force from Arbiter when no gravity

Post by slembcke »

Chipmunk solves the velocities of objects that collide using forces (impulses actually, but the distinction is not that important here). Overlap is handled differently. It happens either because a discrete simulation can never be perfect (overlap is usually small), or because you created a new object that overlaps an existing one (overlap can be quite large). Chipmunk solves overlapping shapes by moving them apart by a certain percentage each frame. This correction is more or less applied directly to the position and does not affect the regular velocity or forces. If it did, overlapping objects would go flying apart which is usually undesirable.
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: [pymunk] Retrieve force from Arbiter when no gravity

Post by Angel »

Oh! I see...

How is then handled cumulative overlapps? If for instance a circle shape is subject to three overlaps at different points and also the overlapping shapes are in the same situation. Is there like a "overlap counter and normal vector" or something? Are the normal of all contacts added and the shape moves according to this new unified normal?

What I want is to figure out which shapes are subject to more "pressure". Somehow equivalent to the overlap steps to carry out (?) in each.

Many thanks for the clarification.
Cheers!

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

Re: [pymunk] Retrieve force from Arbiter when no gravity

Post by slembcke »

This correction is more or less applied directly to the position and does not affect the regular velocity or forces.
How it works is that the solver uses bias velocities, an idea that came from Box2D. To make the position change a certain amount over a certain time step, that's a velocity, and there is already a powerful mechanism for solving velocity constraints. It basically just duplicates the regular velocity solver and then throws the bias velocities away after updating the position.

Solver:
https://github.com/slembcke/Chipmunk2D/ ... ter.c#L458

Body update:
https://github.com/slembcke/Chipmunk2D/ ... ody.c#L512

So, what you want is something like cpArbiterTotalImpulse(). Instead of making a vector sum of the plain impulses, you want to make a scalar sum of bias impulses (cpContact.jBias).

https://github.com/slembcke/Chipmunk2D/ ... ter.c#L142
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: [pymunk] Retrieve force from Arbiter when no gravity

Post by Angel »

So, what you want is something like cpArbiterTotalImpulse(). Instead of making a vector sum of the plain impulses, you want to make a scalar sum of bias impulses (cpContact.jBias).
Exactly. But the arbiter.total_impulse is always Vec2d(0.0,0.0), although the bodies move correctly. So I guess that velocities are stored somewhere I cannot access (?). I may be getting everything wrong, but I can only read the normal vectors out of the arbiter in each colision callback.

I wonder how to get into that solver to read impulse values.

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

Re: [pymunk] Retrieve force from Arbiter when no gravity

Post by slembcke »

Oooh. Pymunk.

The bias impulses are not exposed by the public API. You will have to write some C code to get at jBias field.

As an alternative, does Pymunk have something that wraps cpArbiterGetContactPointSet()? You could sum up the overlap distances to get something a similar value.
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: [pymunk] Retrieve force from Arbiter when no gravity

Post by Angel »

Yes, I can do that! I can get the overlap distances, so will do that instead. Meanwhile I'll try to get the impulse properly.

I hope that Pymunk's developer reads this post.

Many thanks,
A!
viblo
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm
Contact:

Re: [pymunk] Retrieve force from Arbiter when no gravity

Post by viblo »

Can you make a small example? And please specify which version of pymunk you are using.

I have a test in the tests folder that should check that total_impulse works, at https://github.com/viblo/pymunk/blob/ma ... ter.py#L27 Maybe you can compare your code and see whats different?
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: [pymunk] Retrieve force from Arbiter when no gravity

Post by Angel »

Hmmm...

That test returns OK to me (version 4.0). Please try the example "contact_and_no_flipy.py" from the examples folder. If you print the arbiter.total_impulse inside the collision callback:

Code: Select all

def draw_collision(space, arb, surface):
    print arb.total_impulse
it will return valid values. BUT, if you remove the gravity:

Code: Select all

space.gravity = (0.0, 0.0)
then the values for the arbiter.total_impulse are always zero. It looks like the values for the total_impulse are only OK if the bodies have been impulsed before (either gravity or a direct apply_impulse). On the other hand, if the bodies just appear, when some of them overlap they resolve the problem but the total_impulse of the arbiter is always zero.

Maybe that is the way it should be. I'm a bit confused...

Many thanks for all the clarifications!
Angel
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: [pymunk] Retrieve force from Arbiter when no gravity

Post by slembcke »

Err, that's the way it's supposed to be. Maybe my explanation above wasn't very clear. The values you are looking for are available if you use the private APIs in C, but those aren't guaranteed to be stable. The bias impulses (the ones that solve overlap) aren't stored or used in the same way as the regular impulses. Since they are sort of an implementation detail, they aren't exposed by the public API.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests