Physics scale

I want to finally learn how to tune physics in Defold. I want 40 pixels to be 2 meters, so according to physics manual I am setting Scale to 0.05 (2/40). The manual states:

Do note that the gravity (…) has to be increased to accommodate for the change in scale.

But there is no further explanation how to set gravity accordingly - could you please explain this? Value -10 is not looking realistic, because a ball with mass 5 is falling like in a glycerine. About -200 of gravity it is starting to fall like a 5 kg ball on earth. Am I understading correctly the values? Value 5 set in mass is 5 kg? And value -200 in Gravity is -200 m/s^2?

I’m not an expert and can only refer to our manual:

"The physics engine simulates Newtonian physics and it is designed to work well with meters, kilograms and seconds (MKS) units. Furthermore, the physics engine is tuned to work well with moving objects of a size in the 0.1 to 10 meters range (static objects can be larger) and by default the engine treats 1 unit (pixel) as 1 meter. This conversion between pixels and meters is convenient on a simulation level, but from a game creation perspective it isn’t very useful. With default settings a collision shape with a size of 200 pixels would be treated as having a size of 200 meters which is well outside of the recommended range, at least for a moving object.

In general it is required that the physics simulation is scaled for it to work well with the typical size of objects in a game. The scale of the physics simulation can be changed in game.project via the physics scale setting. Setting this value to for instance 0.02 would mean that 200 pixels would be treated as a 4 meters. Do note that the gravity (also changed in game.project) has to be increased to accommodate for the change in scale."

I believe the physics scale applies to all units (MKS), but I’m not entirely sure. It could be that it is only applied to the size as shown in the example above. @JCash probably knows.

1 Like

The -10 for gravity is a simple replacement for the gravity on earth which is ~9.82 m/s^2

Since we don’t scale this constant (I’m not entirely sure why), it makes sense you scale this too with the same constant (20 meters per pixel), at which point you end up with -200 m/s^2.

2 Likes

That’s the same way I found it out - I think it would be good to add it to the manual :wink:

Now what I don’t understand sometimes is that when I am changing the size of the dynamic collision object it start to behaves differently when collided with static object (ground) and kinematic object (player). I thought it could be somehow regarded to the Physics Scale.

The kinematic object can “push” dynamic object (boulder below), but the speed of it’s moving is not related to the mass of that dynamic object (as for kinematic’s mass is infinite), but only behaves like it would have two states:

See difference in pushing two shapes (radius 17 and 25)
When kinematic object is pushing a bigger object - it is moved slowly. Sadly, there is no way to change that speed, only I can make object smaller (the treshold seems to be a strange 17 pixels radius here - larger radius is moved slowly, while 17 and smaller radius makes no difference to player’s speed).

One more important notice here that probably is the reason for speed of pushed dynamic object - with radius 17 and smaller - player’s trigger is not detecting collision with the dynamic object (animation should be changed to pushing like on the second gif, but isn’t).

What I suspect the most is that I should use dynamic object for player to let box2D resolve the collision, but in that way I will lost control on hero (or would need to change it to applying forces instead of changing positions) or resolve in some different way the collision between kinematic and dynamic object for it to look naturally.

How do you think I should solve that issue and allow to create different objects of different mass that will be pushed with a suitable speed?

In both gifs, the the ball is moving, so it seems you are able to detect the collision?

One more important notice here that probably is the reason for speed of pushed dynamic object - with radius 17 and smaller - player’s trigger is not detecting collision with the dynamic object (animation should be changed to pushing like on the second gif, but isn’t).

How does the ball move in the first gif? By adding a force to it?

“Players trigger” - where is it relative to the player’s box, and how large is it?

Sorry - I meant a hitbox/trigger (I shouldn’t use word trigger :smiley: ) - player has only one shape and it’s attached to the kinematic collision object.

Aaaa, and I found out why such strange number is a treshold - I am using raycasts to detect the object on the right/left. From top right/left and down left/right corners. So upper corner is “missing” the smaller ball (radius of 17*2 > 32px) :confused: It’s my mistake. But it’s regarding the animation state, so don’t mind it.

I am not applying force to the ball. That’s maybe why I don’t understand it correctly, but when kinematic object is just pushing on a dynamic object it moves a little bit in a frame - I guess it’s correct, because Box2D is resolving dynamic object’s position and detects a “wall” that the dynamic object is in that frame pounded in that kinematic “wall”, so it moves a little bit out to the right.

Simplifying:

  1. Kinematic objects is moved 1 px to the right.
  2. Box2D is updating dynamic objects.
  3. Dynamic object is plunged into kinematic object by 1 px.
  4. Box2D moves dynamic object 1px to the right, to resolve the penetration.

Am I understanding this correctly?

So question is - should I apply force to the dynamic object in the frame, when kinematic object detects it instead? I wish to let box2D do the collision instead of me, but that would need a change of the kinematic into a dynamic object - is there a way around?

EDIT: Attaching a small project to show it - no matter the mass of the dynamic object, it is pushed like it’s nothing:

Physcis_pushing_repro.zip (18.5 KB)