So I got one dynamic object on the world, and it’s moving because I’ve set it some velocity. However, when I try to get its position, I get the accurate one for as long as it doesn’t collide with static objects and bounces.
I think the physics engine changing the velocity puts the position out of sync. According to what I can see from the rendered graphics, the object is moving properly and nothing wrong can be seen. It’s just that when I fetch the position of the object through the API, I get a value which is quite different from the actual position of the object as per the render.
To debug this, I created a trail (series of points) which is formed right where the API tells me the object is (using getPosition method). The points of the trail is created every step in the game (called 60 times a second). Here’s part of the code responsible for the trail:
auto organismPosition = organism.getNode()->getParent()->convertToWorldSpaceAR(organism.getNode()->getPosition()); auto trail = DrawNode::create(); trail->drawPoint(organismPosition, 5, Color4F::MAGENTA); this->addChild(trail);
The trail follows the object perfectly, until object hits a static object and bounces. I’m putting a link to a video which shows how it looks like:
In this video, the object did change velocity seconds before the collision because of a scheduled call of a function which randomly changes the velocity slightly.
You can check out the entire project here: https://github.com/nahiyan/darwin/tree/master/simulations/Organism
I’m using the default physics engine that comes with cocos2d-x (v4).
Can anyone suggest me why this is happening? Let me know if you need more information to understand the problem better.