Built-in physic not working as expected

Hi all,

I’m playing with built-in physics (i don’t have any knowledge with any physics engine). My problem is:

  • Box bodies can get through each other, especially when moving speed is increased. At low speed, it won’t get through but still the edge of obj A can get into half of the body of obj B. The only way to correct this is to increase with scene->getPhysicsWorld()->setSpeed(1000);. What i want is when obj A contact obj B, it will push B to the direction A moving, and two obj wont get overlapped (or feel like 2 obj is solid).

p/s: sorry for my english

Such speed is too high for any physics engine. Google for “continuous collision detection”.

How are you pushing the objects?

@dotsquid: sorry, i use that number just for fun :smile:, 35 is enough for my need. Thank for your suggestion, but then this mean i must use “continuous collision detection” (or setSpeed). Is that overkill? because my movement is not that fast.

@SonarSystems: this is what happened, it even went through that object if i keep press left arrow key, it still can push the object, though. (if i don’t press the button).

And this is what i expect to:

To pust that object (on the left), i implement code like this in update() function:

switch (move) // this function only move obj A (box on the right)
    {
    case Movement::Left:
        this->setPositionX(this->getPositionX() - delta * 200);
        break;
    case Movement::Right:
        this->setPositionX(this->getPositionX() + delta * 200);
        break;
    default:
        break;
    }

And i expect, when obj A (on right hand) contact obj B (on left hand), A will push B to the left. And it happened like a first picture above, it can be solved by using setSpeed to higher (35 in my case). But i’ve read from internet sources, using high speed will cost performance, is it really worth it if i just need simple boxes collision?

So, you are moving your object by changing its position directly? This is a wrong way. You have to use forces or at least set the appropriate velocity.

2 Likes

@dotsquid is right

Thank you! you two help me very much. BTW can i ask another question?

I notice physics body is independent, won’t be affected much by update() function, right? for example: if i want a ball (a normal sprite) fly across the screen, i must update it with delta*speed, but when i add physics body to the ball, i only need to set velocity for the ball and the physics engine will do the rest?

How to not apply force on dynamic body when contact? I use onContactSeperate, and set that body velocity to zero, but is there any way better?

I think i should learn a bit about physics engine, thanks in advance!

Have you seen our Cocos2d-x Physics Tutorial series?