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

#1

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?

collision_2

collison_1

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

0 Likes

#2

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

#3

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 Pushing.zip (2.6 KB)

3 Likes

How do I make a kinematic object (player) push a dynamic object?
#4

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

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

1 Like

#5

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.

2 Likes

#6

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.

0 Likes

#7

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.

0 Likes

#8

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

0 Likes

#9

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

#10

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

0 Likes

#11

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

0 Likes

#12

You usually add it to the same game object:

0 Likes

#13

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?

0 Likes

#14

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 msg.post(collisionobject_url, "enable") or msg.post(collisionobject_url, "disable").

3 Likes