How do I add a dynamic collision box on my player?

I want to have a box in my scene which can be pushed by the player. I have applied a dynamic collision object in the box sprite, now I want to add the same collision object around my player, so that he can push the box. How do I do that?



Is this the correct way of applying collision to my player?

I assume the player has a kinematic collision object? The problem you are encountering is that kinematic object can’t affect dynamic ones. Only two dynamic objects that collide will be automatically resolved. And you don’t want to use a dynamic object on three player. My recommendation is that you apply a force to the dynamic box object from your player script when you detect a collision between the two.

1 Like

Kinematic bodies should still collide with dynamic ones if the mask matches. The simulation might be unpredictable, though, because kinematic bodies are moved with set_position(). In your example above you should be ok I think, because the movement is so slow.

You can also set linear velocity on a kinematic body. Although this won’t make it move (like in vanilla Box2D) dynamic bodies react like it does if linear velocity is set. This helps interaction with dynamic bodies.

Update: Push a box, three ways:

Box (2.6 KB)


True. I meant this but didn’t explain it very well.

Is this working well in the tests you’ve made @totebo?

1 Like

I would describe it as black magic; since the linear velocity or angular velocity is there but not visible, it’s a question of guessing the values. It seems to work reasonably well. I updated my post above with an example of this; if the linear velocity is set on the kinematic player, you get a nice “bounce” as you collide with the dynamic box, as you would expect when two objects collide.


The simulation is incorrect.
With such dimensions of collision shapes physics scale must be 0.025 (1/40).
And I don’t know why, but linear velocity must be premultiplied by dt before set on kinematic object.

The scale is set to the Defold default, so I suppose the simulation is correct if we assume the world is huge?

That’s interesting, thanks! I have noticed that apply_force shouldn’t use dt, so I assumed the same logic applied to linear velocity.

How do I add the collision object to my player? I mean, do I add it in my player_boy.collection or main.collection??

I don’t know the specifics for defold, as I’m a newcomer, but from my experiences from other engines, put it in the player_boy.collection. That way, once you instance the collection, it comes with the collision object. But, that’s just my experiences with other game engines. Could be different with defold : /

1 Like

Tried it, but still no collision is detected when I run the game.

Like i said, i’m a beginner, so sorry.

You usually add it to the same game object:

How do I convert the kinematic player to a dynamic collision object, when it loses contact with the static collision object (i.e., the platform), so that it falls along with the box?

You can’t swap the collision object type at runtime, but a workaround is to have two collision objects, one kinematic and one dynamic. Then enable/disable them as needed with, "enable") or, "disable").