question

Official forum for the Chipmunk2D Physics Library.
Post Reply
z._.c
Posts: 8
Joined: Thu Mar 14, 2013 1:21 pm
Contact:

question

Post by z._.c »

when i create a cpShape, cpShape->body is NULL. This shape can happen collision with other shape? Collision Handler can be called?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: question

Post by slembcke »

Are you doing something like this?

Code: Select all

cpShape *shape = cpCircleShapeNew(NULL, 5.0, cpvzero);
A shape *must* be attached to a body before it can be added to a space. You either need to pass the body to the cp*ShapeNew() function, or set it afterwards using cpShapeSetBody() function.

Why do you want the body to be NULL?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
z._.c
Posts: 8
Joined: Thu Mar 14, 2013 1:21 pm
Contact:

Re: question

Post by z._.c »

slembcke wrote: Why do you want the body to be NULL?
Because you say "For shapes used for queries that aren’t attached to bodies, you can also use cpShapeUpdate()" and "You can either create a body/shape pair before querying, or you can create a shape passing NULL for the body and position the shape using cpShapeUpdate() to set the position and rotation of the shape.".
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: question

Post by slembcke »

The only function a shape with a NULL body has is to be used with queries. If you want to add it to a space and simulate it, you must attach it to a body first. You might want to do something like this perhaps:

Code: Select all

cpShape *shape = cpShape*New(...);
cpShapeUpdate(shape, position, rotation);

cpBool colliding = cpSpaceShapeQuery(space, shape, NULL, NULL);
if(!colliding){
  cpBody *body = cpBodyNew(...);
  cpBodySetPosition(body, position);
  cpbodySetAngle(body, rotation);
  cpSpaceAddBody(space, body);
  
  cpShapeSetBody(shape, body);
  cpSpaceAddShape(space, shape);
} else {
  cpShapeFree(shape);
}
If you are looking for a way to control a shape without it being attached to a body, you cannot do that. For that you need to use a rogue body. http://chipmunk-physics.net/release/Chi ... ougeStatic
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
z._.c
Posts: 8
Joined: Thu Mar 14, 2013 1:21 pm
Contact:

Re: question

Post by z._.c »

Thanks. I got it.
magni
Posts: 1
Joined: Tue May 06, 2014 6:43 am
Contact:

Re: question

Post by magni »

slembcke wrote:The only function a shape with a NULL body has is to be used with queries.
And by that you mean its only function is as the query shape, right? I.e. I can't use cpShapeUpdate to place a whole bunch of NULL body shapes and then cpSpaceShapeQuery them with another NULL body shape? (I tried and it didn't seem to work.)

In my contraption building game I have a set of shapes used for the physics simulation and a different set of shapes used for collision checking when placing new building elements. I can think of two alternatives to NULL body shapes:

1. Add the non-physics shapes to the same space, but set them as sensor shapes so they don't affect physics collisions.

2. Add the non-physics shapes to a different space, which is never stepped, and manually position and reindex them each frame (or at least each frame that a shape query needs to be performed).

Which is better for performance? Or is there an even better third option?
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 23 guests