Rotate a physics object manually (SOLVED)

Is there a good way to set a dynamic object rotating?

Say I’ve got a field of asteroids, which are dynamic objects using the physics engine, and I want them to be slowly spinning in space. Right now, they’re just a static orientation until the actually colide with something else.

One way to rotate dynamic objects could be to apply a force on the side of the object. E.g. in the opposite direction of the travel vector, slightly offset from the center of the object.

3 Likes

I have a small function for applying torque that applies two forces on opposite sides of the object in opposite directions.

local YVECT = vmath.vector3(0, 1, 0)
local NEG_YVECT = vmath.vector3(0, -1, 0)
local XVECT = vmath.vector3(1, 0, 0)
local NEG_XVECT = vmath.vector3(-1, 0, 0)

-- Apply torque to a dynamic collision object component
function apply_torque(url, worldpos, t)
	local halft = t / 2
	msg.post(url, "apply_force", { force = NEG_XVECT * halft, position = worldpos + NEG_YVECT })
	msg.post(url, "apply_force", { force = XVECT * halft, position = worldpos + YVECT })
end

5 Likes

That’s really handy - thanks for sharing! :+1:

3 Likes