Chipmunk objects disappearing under certain conditions

Chipmunk objects disappearing under certain conditions

I’m a cocos noob and I’m trying to build something based on 2d physics. I’ve downloaded the programmer’s guide samples and, in chapter 12, i’m tinkering with PhysicsDemoCorrelationToSprite code.
The program creates a certain number of physics objects and set them up in random places on the screen, giving them random velocity. Those objects are limited by a world edge box, so they rebound as they get to the screen limits, back and forth, and over and over on each other.
Then, I let a new object enter the screen following a simple straight path and colliding with them. This object has the same rules of the others except that it is led by a move to action.
I observed some behaviors regarding chipmunk physics (I’m using c++ 3.6 cocos2dx) I’d like a confirmation of. My guess:

a) when two objects happen to find themselves in the same phisical space position, one is destroyed.

This can be seen at times when the simulation starts; sometimes the demo randomly places two (I’ve seen more) objects in the same place: one disappears.
Note that the world is set to let the object collide and rebound away, so this should’n be possible. But random positioning makes it happen.

b) an object under a “move” action behaves like a static object with infinite mass, even if it is defined differently.

Such a move action pushes other objects away bruntly, like it were a crashing level wall. Anyway something else interesting happens:

c) This crashing level wall hits another object at the world limit. If hit in a way it cannot find an “escape route”, this object is pushed to the world’s edge, encounters a barrier, and disappears (is destroyed). Same thing as a)? Or another mechanic?

d) It seems the objects disappear in another condition, too; when they are pushed beyond a limit in velocity after a hit. They rocket out of the screen… disappearing. It is similar too c), but doesn’t happen at the world boundary…

My questions are:

  • what happens to the object when it disappear? is there a event/condition in the engine I can check to catch it and, for instance, play a self destruct animation?
  • or this is a chipmunk engine known behaviour so the only way to set for it is running a physics world update listener and continuosly check for the object presence in play? If so, getting a destruction effect to play just before it could not be possible…

Thank you everybody and sorry for tl;dr; :cat:

To answer your questions:
There is not a disappear/destroy feature in the built in Chipmunk physics wrapper. Bodies and their shapes stay in the physics world until removed or the world is destroyed. Where they ‘stay’ is a result of the forces that are applied to bodies.

Objects are probably experiencing very high forces and as a result quickly achieving very high velocities due to manually moving objects or the correction of overlapping two objects (shapes) that are not allowed to overlap (when shapes connected to bodies overlap, forces are applied to correct that).

More details below that should make things more clear.

a) Not “disappearing”, but maybe moving away real fast. In Chipmunk, when two shapes are overlapping and they are defined to collide, then Chipmunk will apply a force to correct that unwanted overlap (bodies don’t collied in chipmunk, but shapes can if they are not in the same collision group).

So if we define two bodies to start in the world and they are overlapping, each step of the simulation will apply a force to correct that. If the body masses are significantly different, then the significantly less massive body can be fired like a bullet away from the more massive object.

The default dynamic body with a shape attached to it that is going too fast can effectively teleport through a barrier like a wall. This is a result of the physics simulation needing to actually detect a collision in a “current state” update. If in one physics update the body is on one side of the wall and on the next update it is on the other side of the wall, then there effectively was no collision. There are ways to fix this, like using a segment query or ray cast. Or by increasing the number of physics world updates and setting a maximum velocity.

There are also two open bugs as of 3.7 that can cause a similar problem on initialization that can make it seem like the bodies are disappearing or teleporting: https://github.com/cocos2d/cocos2d-x/issues/12691
https://github.com/cocos2d/cocos2d-x/issues/12567
In these bugs, for some reason the position is huge numbers or NAN. Not sure what causes that.

b) We should probably not set the position of bodies except during initialization. Or if we do, we should reset the forces on them and other bodies around them immediately after the move.

It is also recommended not to manually set the velocity of dynamic bodies. Instead, we should apply an impulse force or a normal continous force to get them moving as desired. Or let the forces caused by other object that impact them to determine where they go.

Checkout out this from the Chipmunk documentation for the reasons why: https://chipmunk-physics.net/release/ChipmunkLatest-Docs/#cpBody-Movement

A physics simulation is meant to simulate a real physics world. But using a move by action is like the hand of god pushing an object through the world with infinite power and moving it with sudden position changes.

Chipmunk (and Box2d) both have three body types. Dynamic, Kinematic, and Static.
Understanding the difference between them is important.

We can move Kinematic bodies, but we should do so by setting their velocity and then stopping them when they get to where we want them to be.

But in Chipmunk and Box2d, Kinematic bodies are treated as if they have infinite mass, so they do not respond to forces.

Chipmunk Body Types
https://chipmunk-physics.net/release/ChipmunkLatest-Docs/#cpBody-BodyTypes

Box2d Body Types
http://www.box2d.org/manual.html#_Toc258082973

c) Not sure what you mean by “crashing level wall”, but if you are manually positioning object in the physics world, then as described above, the forces can be sudden and immense, and can shoot some bodies away. e.g. the body can get a sudden very high velocity due to a very large force and as a result even move through other objects as described above.

d) What is doing the pushing? If you are manually moving an object that is doing the pushing, then that manual move is unstoppable by any mortal physics object.

1 Like

Thank you very much for the extensive and clear reply. I need to wonder about it some more, but anyway I see I have to implement more careful checks to avoid “simulation explosion” conditions. Need to test it thru. Thank you. :fish:

just one aditional note to Heyalda’s:

take into account that current chipmunk implementation does not support kinematic bodies. Well, it does, but it is called “rogue bodies” and you have to handle it yourself, bypassing the physics wrapper