body and shapes

Official forum for the Chipmunk2D Physics Library.
Post Reply
pward84020
Posts: 2
Joined: Sat Sep 22, 2012 3:59 pm
Contact:

body and shapes

Post by pward84020 »

I am a bit confused about the api.

When you create a shape, it is automatically associated with a body ( you actually need to pass in the body to the shape create function, like cpShape * circleShape = cpCircleShapeNew( body, 10.0, cpvzero);

So, if the body has already been added to space... why do you need to add the shapes also ? Wouldn't they be automatically added since they are part of the body ?

cpBody *ballBody = cpBodyNew(100.0, INFINITY);
cpSpaceAddBody(space, ballBody);

cpShape *ballShape = cpCircleShapeNew(ballBody, 20.0, cpvzero);
cpSpaceAddShape(space, ballShape); // WHY IS THIS REQUIRED ?

- What would it mean if you create a body, add it to space, create shapes on it.. but never add the shapes ?

- When you remove / destroy the body , are the shapes also removed / destroyed ?

- Is it better to create the body, then create all the shapes on it, and then add it to space ?

- Do you only need to add the shape to space if the body is already in space ? ( ie, kinda an update on the bodys shape list )

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

Re: body and shapes

Post by slembcke »

Adding a body to a space means that body be simulated (i.e. it should have forces and gravity applied to it). Static bodies aren't added to the space because they never move and don't need to be simulated. Because they are infinite mass they can't be affected by collisions either. More information here including a description of rogue bodies: http://chipmunk-physics.net/release/Chi ... ougeStatic
pward84020 wrote:- When you remove / destroy the body , are the shapes also removed / destroyed ?
Adding a shape to a space means that the space should check for collisions with it. Adding a shape to a space effectively enables it, removing it disables it. You can add shapes to a space that are attached to a body that is not (rogue and static bodies). In this case, the shapes check for and apply collisions, but the bodies either never move (static bodies) or are manually updated (rogue bodies). Rogue bodies are generally only needed for very simple, infinite mass things like moving platforms or control bodies for jointing things too (like controlling things with a mouse).

A body that is added to the space but has no shapes is simply a body that can't collide with anything. This is useful if you only want it there to joint other objects to it or if you just want Chipmunk to update the trajectory of something for you.
pward84020 wrote:- When you remove / destroy the body , are the shapes also removed / destroyed ?
pward84020 wrote:- What would it mean if you create a body, add it to space, create shapes on it.. but never add the shapes ?
Memory in Chipmunk is not auto-magically managed for you at all. Spaces, bodies, shapes and constraints are all separate entities that can be connected in complex ways. As a C library without garbage collection, Chipmunk can't guess what you mean to do with them. If you want logic that removes them from the space all at the same time or frees them all at once, you should put that in the code for the game object that owns them.
pward84020 wrote:- Is it better to create the body, then create all the shapes on it, and then add it to space ?
You can create the shapes with a null body, then create the body, and then go back and set the body for all of the shapes. You can create them in any order you want as long as they are set up correctly when you try to add them to the space. It's not "better" per say, but it is more work. I'm not sure why you'd do that unless you made some sort of serialization scheme that needed to do it that way I guess. You *do* need to be careful about the order you destroy things in though. For instance, if you remove a body from the space, then free it, then try to remove it's shapes from the space you'll crash. The shapes will still be referencing the body you just destroyed, and will crash when trying to remove them from the space. In general, remove things then destroy them, or remove and destroy your shapes and constraints then remove and destroy the bodies.
pward84020 wrote:- Do you only need to add the shape to space if the body is already in space ? ( ie, kinda an update on the bodys shape list )
Again, shapes can be in a space even if their body isn't (static or rogue bodies again).
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
pward84020
Posts: 2
Joined: Sat Sep 22, 2012 3:59 pm
Contact:

Re: body and shapes

Post by pward84020 »

Thank you for your speedy and lengthy reply !!!!

I think I am getting a better understanding of things now.


TO RECAP:

I was assuming that since shapes are created with a body, that the body 'owned' them, and would free them when it was destroyed. Which is not the case. I did not realize you could create a shape with a NULL body object ( which you can then add the shape to space, and it would be a static shape since it has not body to interact with gravity, forces, etc )

So, whatever I add to space ( be it bodies, shapes, etc ), I must also remove, and destroy. And, of course, it is fatal to remove a body and destroy it without first removing and destroying its shapes ( since the shapes will be referencing the body still ).

So, to destroy a remove and destroy a body, it is probably best procedure to iterate over its shapes, and remove/destroy them first, and then destroy the body.

Also, shapes cannot be shared ( a shape cannot be belong to more than 1 body ).

Thanks for the information.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: body and shapes

Post by slembcke »

You cannot add a shape with a null body to a space. You can create the shape, and then set it's body (or change it's body) before adding it to the space though.

Also, you don't want to iterate a body and remove it's shapes. For the same reasons you generally can't iterate arrays or lists while removing their items, you can't remove Chipmunk objects from callbacks. The code that manages the physics objects or uses them should be responsible for tracking them. You just need to keep a list. You *can* use post-step callbacks to handle that, but it's awkward and doesn't really solve the problem of how you would track objects that aren't currently active at the time when you want to destroy them.

Shapes cannot be shared no. They don't use a lot of memory though, so it's not really much of an issue. It's hard to use up a lot of memory for 2D collision detection.
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 19 guests