Best way of moving physicsBody efficiently?

Hi again!

I have created a physics level consisting of boxes and one bigger box that refers to a player, as seen below:

I have implemented TouchBegin(attached to touchBegan) with the following code:

bool HelloWorld::onTouchBegin(Touch* touch, Event* event)
{
    edgeNode->runAction(MoveBy::create(1.0, Vec2(20,0)));
    return true;
}

So that the “player” will move a little to the right whenever the user is touching the screen but when the “player” reaches a box that blocks its way, it should stop but instead i can easily make the bigger box come through/inside the other physicsBodies.
Like this:


So my question is - How can i make this physicsBody move with perfect collision detection almost no matter the speed of the movement? Should i use another function or optimize something?

Thank you.
I appreciate your time!

This answer may be incorrect as you’re using the built-in physics library instead of box2d; but in box2d physics bodies usually overlap when their mass value is set too low. Try increasing the mass. One other thing, building levels like this out of boxes is very very innefficient, because each body(box) is a separate physics entity and every one will need a separate physics calculation. In box2d you can build structures like this using chainshape or edgeshape. I imagine there is something similar in the built-in physics library. As I said I haven’t used it, but if you’re relying heavily on physics, making a physics based game, I would use box2d. There are a lot of level/physics editors that you can use to build levels like this and load them in.

Thank you! But in docs, it says that it is recommended to use the built-in physics…

built in physics is recommended because it’s simple and easy to use, but "if you’re relying heavily on physics, making a physics based game, I would use box2d. @UKDeveloper99 " too.

3 Likes

You should avoid Move actions when dealing with physics. Move actions doesn’t take care of physics body.
Try to apply velocity with setVelocity or impulse.

1 Like

Great! But can you show me an example of applying and to set the correct move action, would help very much!
Thank you.