Changing an active object to a static object

Discuss new features and future development.
Post Reply
probablyCorey
Posts: 1
Joined: Thu Oct 30, 2008 5:58 pm
Contact:

Changing an active object to a static object

Post by probablyCorey »

I've got several shapes that I would like to change from active to static objects (and vice versa) during runtime.
Is this possible with chipmunk? I've tried replacing the shapes body with a static body, but this seemed to cause crazy results.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Changing an active object to a static object

Post by slembcke »

If you want to make an object static, set it's body's velocity and rotational velocity to zero, set it's mass and moment of inertia to infinity and remove the body from the space. You must remove the body to prevent it from falling under the effects of gravity.

To make it active again, you to the opposite. Restore it's mass and moment of inertia back to what they were before and add it to the space.

You don't really need to worry about static vs. active shapes unless you have a lot of shapes. Static shapes are just an optimization. Their collision properties are precalculated when they are added instead of every frame. If you do add and remove shapes from the static hash, you will want to call cpSpaceReshashStatic() every once in a while to remove all the old empty pointers to removed objects.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
JoeStrout
Posts: 7
Joined: Fri Nov 28, 2008 11:46 pm
Contact:

Re: Changing an active object to a static object

Post by JoeStrout »

slembcke wrote:If you want to make an object static, set it's body's velocity and rotational velocity to zero, set it's mass and moment of inertia to infinity and remove the body from the space. You must remove the body to prevent it from falling under the effects of gravity.
I'm confused by this. If an object isn't part of the space, then how would it be participating in the simulation at all?

I need to understand this more deeply, because in my own project, I have some static objects that are falling due to gravity, and others that are not (I want them to all not fall, of course). All are created with an infinite mass and moment of inertia, and all are added to the space. Clearly something is different between the two cases, and I'll track down what it is if I have to rip the code apart line by line... but it'd be helpful to have a better idea of what to look for, and exactly how one should create a static object.

(To be clear, I need something that interacts with other objects with regard to collisions, but does not move in any way.)

Thanks,
- Joe
JoeStrout
Posts: 7
Joined: Fri Nov 28, 2008 11:46 pm
Contact:

Re: Changing an active object to a static object

Post by JoeStrout »

I figured it out: for a static object, you add the shape to the space, but not the body.

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

Re: Changing an active object to a static object

Post by slembcke »

I suppose the documentation doesn't quite cover what Chipmunk does when things are added to a space.

When a body is added to the space, it's position and velocity will be updated.
When a shape is added to the space, it causes the body that it's connected to to collide with other body's shapes.

The reason why you don't add a static body to a space is twofold. The first is that it will end up accelerating due to gravity. (gravity is not modeled as a force) The second is that if an infinite mass object collides with another infinite mass object (another body intended to be static for instance) it will cause it to generate infinitely large forces to resolve the collision which would be very bad.

The reason why it's OK to leave the static body out of the space is that any object colliding with it won't be able to push it because of its infinite mass. Also, because it never moves, it doesn't need it's position or velocity updated.

A neat side effect is that if do give a static body a velocity, it won't move, but objects colliding with it will think it is. For instance, you could make a bit of an earthquake by giving the static body a small random velocity every frame.
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 5 guests