Sprites tied to physics lag one frame behind (Issue-4152)

Having continued my game I get this behaviour on all shapes most of the time. Is there a way to avoid this one frame “tunneling” of dynamic objects?

It’s not a game breaker, but it’s noticeable when the camera follows an object that burrows into solid objects for one frame. Can I force a graphic update at the same time as the physics is updated somehow?

Can you please share a minimal example of this behaviour?

I was just about to! A minimal project (no code!) is attached, showing the issue clearly:

With debug:
45

Without debug:
54

PhysicsTunneling.zip (34.6 KB)

3 Likes

I believe, this is how physics works with a such high gravity. Continuous Collision Detection (making ball a bullet) may fix this, but this is not accessible in Defold.

That might be true, although I’ve never seen this before as I’ve never had gravity set to so high. Is there an alternative way to make things move naturally? With the default settings all objects added appear planet sized and move very slowly.

In game.project under Physics you can set the scale of the physics simulation. I think Box2D maps 1 pixel to 1 meter in its simulation, so setting the scale to e.g. 0.02 would map 50 pixels to 1 meter instead.

Regarding the overlap issue, after some googling it seems like a quirk in Box2D unfortunately. Described in better detail in this thread; http://www.box2d.org/forum/viewtopic.php?t=6438#p29192

There is an ongoing feature we are developing to decouple the physics simulation from the game loop, allowing you to run the physics at a higher (or lower) rate. It’s not prioritised in our backlog yet and still early in development: Teaser Fridays and Roadmap talks

Being able to run physics at a higher rate should make the overlap smaller and not as noticeable.

6 Likes

That would be amazing. Thanks for the reply!

4 Likes

This has turned into a bit of a problem now, as the ball in my game actually gets stuck within the terrain. I’ve spent all morning trying to solve it, without success. Here’s what I’ve tried so far:

  • Adjust gravity and scale
  • Limit the velocity by adding linear damping
  • Cap the velocity programmatically, by reading linear velocity and send a counter impulse to stop the ball going too fast
  • Add a trigger to detect a collision and slam on the brakes with impulses
  • Add other shapes inside the ball

Any other ideas?!

If you are trying to “fix” the dynamic physics simulation by modifying linear velocity etc. you might be better off using a kinematic collision object and handle the simulation and collisions yourself instead. That way you get more explicit control of how the collision objects behave.

The manuals on resolving kinematic collisions should be helpful; https://www.defold.com/manuals/physics/#_resolving_kinematic_collisions

2 Likes

That may be what I’ll end up doing actually, I’m banging this ball against a brick wall. :slight_smile:

5 Likes

I’m at this again!

If -10 is “natural gravity”, how do I create a dynamic body football bouncing naturally? No matter what settings I use, I seem to end up with a football that moves much, much too slowly for it to feel “natural”.

Has someone done this and can point me in the right direction?

More reasonable values would be gravity -1000 and physics scale 0.01. Our template projects have been updated with these values btw.

1 Like

Yes, those values appear more natural. BUT they have the issue of tunneling, which I’m trying to avoid by being a good boy and following all the rules. :slight_smile:

I noticed that! But it still says -10 is “natural”, but I think that may be natural only on a small comet.

Where does it say this?

See screenshot above. It’s in the tooltip when you hover over the textbox.

I guess the comment refers to our common constant of gravity (~9.82) here on earth.

So, to recap:

  • Gravity appears earth-like at -1000 and physics scale 0.01.
  • With this level of gravity dynamic objects burrow into each other for one frame on collision.
  • There is no solution, aside from manually dealing with physics using kinematic objects.

:sob:

Box2D has an isBullet property that can be set on dynamite dynamic bodies to perform continuous collision detection. We do not expose this property.

Can you share an example project where this problem is very obvious and I’ll play around with the property? If it solves your problem we’ll look into adding it in the API.

Sure! This example (from above, but with the debug flag unchecked) displays the issue clearly:

PhysicsTunneling.zip (52.6 KB)

I’m not sure bullet is the answer, though, since apparently it’s more resource intensive:

I was curious that I hadn’t seen this behaviour in other engines I’ve used (Flash and Gideros), so I created a minimal example in Gideros with just a ball bouncing. It doesn’t display any tunneling, even with very high gravity (100000). In Gideros, a gravity of 100 feels earth-like.

In case it has any value, here is the Gideros project:
GiderosPhysicsTunneling.zip (8.5 KB)

Any idea why the Defold engine behaves so differently? Could it be that in Defold collisions are resolved a frame later, resulting in the overlap?