Methods for rotating a set amount over a set time?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
VIVr
Prole
Posts: 2
Joined: Tue Oct 04, 2011 8:59 am

Methods for rotating a set amount over a set time?

Post 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?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

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

Post 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
Last edited by kikito on Wed Oct 05, 2011 11:30 am, edited 1 time in total.
When I write def I mean function.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1024
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

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

Post 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
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
VIVr
Prole
Posts: 2
Joined: Tue Oct 04, 2011 8:59 am

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

Post by VIVr »

OK thanks. I will try to implement one of these in my code.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 30 guests