1
votes

I have a cpBody with single cpShape that floats in my scene colliding with other bodies et c. How can I easily make this body stay in one place and act kind like a static obstacle staying in one place so it's not longer moving but still colliding with other bodies.

I just want to stop body from moving when user taps on it. That's why I'm asking. I'm not an expert in Chipmunk but I think it must be easy.

2
I'm not using Chipmunks, but there should be an option to specify static body somewhere. - nhahtdh
You can add static body, not convert dynamic one to static. I don't want to remove the dynamic body and then add the static body with same parameters. I'm just asking cause maybe there is a quick way of doing this that I am not aware of. - Rafa de King

2 Answers

5
votes

The way you'd do this with the public API is to remove the body and shape from the space. Create a new static body with the same position/rotation as the old dynamic body. Use cpShapeSetBody() to change the body to the new static one, and then readd the shape to the space.

1
votes

You can call cpBodySetMass to INFINITY, and force the object to sleep with cpBodySleep. This is how a static object is implemented internally (at least about the mass).

EDIT

I am not sure whether you need to call cpBodySleep after this or not, but I don't think it hurts to call.

Modify cpBody.h and put #define CP_ALLOW_PRIVATE_ACCESS 1 at the beginning. Then from cpBody*, access ->node.idleTime and set it to INFINITY.

EDIT 2

The above solution is a working solution, but not very good in term of SE practice. It is better to define a function that makes the object static or dynamic so that you can call without disabling private property for the whole object.