Page 1 of 1

question

Posted: Fri May 31, 2013 11:08 am
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?

Re: question

Posted: Fri May 31, 2013 12:26 pm
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?

Re: question

Posted: Fri May 31, 2013 1:31 pm
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.".

Re: question

Posted: Fri May 31, 2013 5:58 pm
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

Re: question

Posted: Fri May 31, 2013 10:29 pm
by z._.c
Thanks. I got it.

Re: question

Posted: Thu May 29, 2014 7:52 am
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?