Why does the physics engine do this?


britzl previously you said maybe I can apply a force to it, but I haven’t figured out how. Why on earth does the physics allow this cucumber go through the static wall at the bottom? You can see the game play in this example so it will give you an idea of the type of game I’m going for. And typically it’s working great. But sometimes this happens and I’m like I can’t release this with this issue. Please let me know how I can workaround this.

A little more video.

Looks weird. Could you please record a video with physics debugging enabled when this happens?

Well this isn’t the same issue but similar. This time two objects got welded together then exploded. Typically if it gets stuck on the wall though it will stay that way until combined with another veggie to spawn a new one.

Watch at 6 seconds the redonion gets welded into the cucumber then it “explodes” when it separates.
media.gab.com/system/pending_media_attachments/files/006/268/838/original/e911c82065bc5b07.mp4

Hmm, yeah, looks weird. Is it always the long thin stem of the vegetable which gets stuck?

No it’s has been the carrot and cucumber in the past. Plus the long thin stem (red onion and regular onion). I can test by spawning the fatter items early to see if they do it as well.

Are you using a variable time step? Box2D requires a fixed time step to work correctly. Defold defaults to a variable time step for box2D, I don’t understand why a fixed time step is not the default.

My guess is that box2d is fighting with itself. Maybe it is caused by a gravity factor which applies some reduction in the y position, and the collision object incrementing the y position. So if gravity keeps dropping the object back into the range that triggers the collision object to increase the y value back to the previous position then you get an infinite loop of shaking. Some sort of scaling factor parameter change might fix it. It could be worth looking on the box2d website for clues, if no one here has an answer. Also debugging with prints of the y value of the trapped object would be useful maybe.

Yes I have fixed turned on. But it does it whether it is on or off. Are you saying I need to move my code out of update and into fixed update as well?

BTW: the docs say new projects should default to fixed but they don’t.

Ok, I changed the code to run in fixed_update instead of just update…and I have fixed update checked in physics engine. one of my beta testers sent me the following:

I can take a quick look at the project if you want to send it to bjorn@defold.se

Fixed update or anything else will not help here. IMHO.

You spawn objects almost inside other objects and expect them to just smoothly slide apart, right? But Box2D solver does what all physics engines do - “explosion”. I mean, it creates a sort of explosion of the created bodies because they repel against each other.

Same thing with the stuck objects.

Try to spawn physics objects of small size, and then smoothly increase their scale (it should be uniform).

4 Likes

by smoothly increasing the size, won’t that be noticeable on screen? But I get what your saying…if I spawn them small it shouldn’t spawn inside of an object. I’ll give it a shot. Thank you.

If the idea works, you can split vegetables into two game objects: physical and visual. Scale the physical one, but not the visual one. Set the visual’s position from the physical one.

This appears to work. While sometimes the object will appear to be through the wall…it wiggles itself out very quickly (and doesn’t explode). Further testing will be needed, but I think this did it! Oh and I just scaled the object. I was already scaling down the object (I know I should use smaller graphics, but that would come under optimization phase. So I just put a leading zero after the decimal. Then I multiply by 10 on the resize and it works!

veggieID = factory.create(“main:/factories#garlicfactory”, pos, nil, nil, .013)
resizeVeggie(veggieID)

function resizeVeggie(veggieID)
current_scale = go.get_scale(veggieID)
current_scale.y = current_scale.y * 10
current_scale.x = current_scale.x * 10
go.set_scale (current_scale, veggieID)
end

3 Likes

Brilliant idea!

3 Likes

FYI. It still happened once in testing but it’s a lot less now. I can probably tweak how I grow the object to fix it further.

Maybe I’ll test if I’m near an edge and move a little further away before scaling up.

1 Like