Page 1 of 1

Methods for rotating a set amount over a set time?

Posted: Tue Oct 04, 2011 9:41 am
by VIVr
So, when developing my little project with overly large ambitions I stumbled over a rather annoying problem. It's related to enemies going at certain angles and certain speeds, and changing the angle while they continue to go forward.

I want to make a function that, over a certain amount of seconds, rotates a given object, in my case an enemy (I am not working with love.physics) a given amount of degrees, no more, no less. My current approach that basically revolves around angle difference / time to be taken in seconds * dt is not working so well. That is, the test row of enemies starting at same angle and speed are rather spreading out post-rotation and that just isn't tidy. I'm actually calling love.timer.getDelta() in the rotation function separately, maybe that's my problem, but I'd assume the timer is the same during one calculation?!

I'm aware I could implement a completely fixed step to solve the problem, but as far as I understand that also opens up an entirely different can of worms. So my question is, is there a method of getting a fully fixed rotation over time that isn't the "fix rotation to a predeclared variable after rotating" hack I am currently using?

Re: Methods for rotating a set amount over a set time?

Posted: Tue Oct 04, 2011 9:58 am
by kikito
This is certainly doable with tween.lua

Code: Select all

tween = require 'tween'

function love.update(dt)
  -- you must call this once inside love.update
  tween.update(dt)
  ...
  -- enemy1's angle should get equal to math.pi in 2 seconds
  tween(2, enemy1, { angle = math.pi })
  -- enemy2's angle should be made math.pi/2 in 0.5 seconds
  tween(0.5, enemy2, { angle = math.pi/2 })
  ...
end

Re: Methods for rotating a set amount over a set time?

Posted: Tue Oct 04, 2011 1:14 pm
by Taehl
Here's an alternate way to do it (which doesn't use tween.lua):

Code: Select all

function love.load()
	-- make two example enemies
	enemies = {
		{x=100,y=100, a=0, aTime=0, aDiff=0},
		{x=200,y=100, a=1, aTime=0, aDiff=0},
	}
end

function love.update(dt)
	for k,e in pairs(enemies) do
		if e.aTime > 0 then
			e.aTime = e.aTime - dt
			e.a = e.a + e.aDiff * dt
		end
	end
end

-- Parameters: Enemy to set, time rotation will take (in seconds), degrees to rotate
function setRotateOverTime(e, time, degrees)
	e.aTime, e.aDiff = time, math.rad(degrees/time)
end

function love.keypressed(k)
	-- Rotate enemy#1 90 degrees over 0.7 seconds when any key is pressed
	setRotateOverTime(enemies[1], .7, 90)
end

Re: Methods for rotating a set amount over a set time?

Posted: Wed Oct 05, 2011 8:56 am
by VIVr
OK thanks. I will try to implement one of these in my code.