Set gravity has no effect after spawning object with static collision object

Hello, noob here finding weird behavior :slight_smile:

I have:

  • an object that spawns objects (using a factory) with static collision on mouse click.
  • another object with a script that turns gravity back on when I press space (gravity is disabled in init).
  • an object with dynamic collision

When I start the game, the object stays in the air until I press space. Then it falls normally, but if I have spawned some of the static collision object before I press space, the object does not fall. Any ideas?

Here is how I turn on gravity:
physics.set_gravity(vmath.vector3(0.0, -2000.0, 0.0))

It sounds like a bug in that case… Strange though. Could you please share a project where this is reproduced?

I created a very small project that reproduced the issue. But it seems the problem is not with spawning a static object but with generating any input.

If I move the mouse or press any key before pressing space, the object will not fall. If I keep my hand out of the mouse and keyboard until I press space, the object falls.

If you still want the project I can send it. Should I zip it and attach it here?

I feel there is no need for a project. With this code and a dynamic object in the scene it is enough:

function init(self)
	msg.post("#", "acquire_input_focus")
	physics.set_gravity(vmath.vector3(0.0, 0.0, 0.0))
	print("gravity disabled on init")
end

function on_input(self, action_id, action)
	if action_id == hash("space") then
		if action.pressed then
			physics.set_gravity(vmath.vector3(0.0, -2000.0, 0.0))
			print("gravity enabled")
		end	
	end	
end

The problem is that the dynamic object comes to rest when the gravity is disabled. The object will not wake up because of the change in gravity in the scene. Instead of you need to manually wake it up:

physics.set_gravity(vmath.vector3(0.0, -2000.0, 0.0))
physics.wakeup("/dynamic#collisionobject")

https://box2d.org/documentation/md__d_1__git_hub_box2d_docs_dynamics.html#autotoc_md61

Side-note: It might make sense to wake up sleeping bodies when we change the gravity, but that would be a breaking change.

1 Like

Thanks a lot! I will try that as soon as I am back home from vacation, it seems very likely this is the solution.

PS: In any case, how come does it work if I do not click on anything before pressing space?

Edit: I confirm that this solved my problem

I am not able to reproduce this.