[SOLVED] New to the engine, I have a question on physics (2D)

Hello defolders,
I recently started using Defold and so far I’m loving it!
I am building a small 2D spaceship & asteroid super classic shooter.
The movement is free, A and D rotate the ship, W for thrust.
I am totally relying on physics for this, meaning that the ship collides with asteroids, they move as consequence, and all kinda works.
The problem i’m having is with the maximum ship speed.
To thrust, i apply a force:

msg.post("#collisionobject", "apply_force", {
			force = vmath.vector3(x, y, z),
			position = go.get_position()
		})

that #collisionobject is the ship collider. And it works, however it seems that no matter how much force i apply, the speed does not go beyond a certain threshold.

From my physics classes i remember that, if the force applied to a body is constantly growing, so is the speed, unless there is friction, which is not the case because i disabled everything.

Is there something i’m missing, or am doing wrong here?

3 Likes

Sorry I clicked submit too fast without saying “thanks and happy defolding!”
Cheers

Welcome to the forum!

Hard to say. How are you applying the force? In on_input() or in update()? How much force are you applying?

What if you print the velocity and damping?

function update(self, dt)
    local velocity = vmath.length(go.get("#collisionobject", "linear_velocity"))
    local damping = go.get("#collisionobject", "linear_damping")
    print(velocity, damping)
end

What kind of values are you seeing for the velocity? And the damping?

1 Like

There is linear damping by default that will limit the speed (think air resistance).
But, if you hit that, it’s likely that you are using a physics scale of 1. I.e. 1 physics unit is 1 pixel.
That’s probably not what you want.
You can try setting scale to 0.01, which means 1 physics unit is 100 pixels, and see if that helps.

Thanks both!!!

Mathias was right, the “Scale” parameter in the settings did the trick i applied it to 0.2 which is ehough, and now the ship is a rocket!

Anyhow, this is the pseudo-code which has been removed with a lot of other stuff and i have left only the movement code, to answer britzl, everything is done in the update method and using also dt. the factor i am multiplying by is 10.000:

go.property("acceleration", 10000)

local UP_VECTOR = vmath.vector3(0, 1, 0)

function init(self)
	self.thrusting = 0
end

function update(self, dt)
	if self.thrusting ~= 0 then
		local thrust_force_position = go.get_world_position()
		local thrust_force_direction = vmath.normalize(vmath.rotate(rotation, UP_VECTOR))
		local thrust_force = thrust_force_direction * self.acceleration * dt * self.thrusting

		msg.post("#collisionobject", "apply_force", { force = thrust_force, position = thrust_force_position })
	end
end

function on_input(self, action_id, action)
	if action_id == hash("thrust") then
		self.thrusting = action.released and 0 or 1
	end
end
2 Likes

I’m glad you figured i out!

Btw, I also wanted to mention that we recently added b2d and b2d.body modules, for more complete access to the Box2D functions. It’s an ongoing effort, but should help with at least some of the things you might want to do.

4 Likes