Page 1 of 1

Tweening

Posted: Wed Apr 13, 2011 5:28 am
by Anxiety
How can i tween an image between two positions? Yet again, its for the game im making,

Thanks!

Re: Tweening

Posted: Wed Apr 13, 2011 5:47 am
by ivan
Anxiety wrote:How can i tween an image between two positions? Yet again, its for the game im making
If I remember correctly, 'tweening' in Flash is a bit of a general term that includes modulating/alpha blending the sprite not only transforming it.
If you're only trying to change position of a sprite you could trying something like:

Code: Select all

elapsed = elapsed + delta
local t = math.min ( elapsed / totalTime, 1 )
x = (fpx - ipx) * t
y = (fpy - ipy) * t
if t == 1 then
  -- target reached
end
where fpx, fpy is the final position and ipx, ipy is the initial position

I personally find it easeier to use a velocity instead of storing the 'total' and 'elapsed' time:

Code: Select all

local dx = fpx - x
local dy = fpy - y
local dist = math.sqrt ( dx * dx + dy * dy )
local step = velocity * delta
if dist <= step then
  x = fpx
  y = fpy
  -- target reached
else
  x = x + dx / dist * step
  y = y + dy / dist * step
end

Re: Tweening

Posted: Wed Apr 13, 2011 5:53 am
by BlackBulletIV
The most raw way is to use a timer and some target variables. For example:

Code: Select all

time = 0 -- the current time
target = 1 -- the target time
start = 0 -- the starting value
targetVal = 300 -- the target value

function love.update(dt)
  time = time + dt
  current = start + (targetVal - start) * (time / target)
end
You could then use current as the position. However, this approach quickly gets messy. I personally create a class to handle it all for me, but EmmanuelOga created a tweening library which you might want to use.

EDIT: Ninja'd. (At least with showing a raw method anyway)

Re: Tweening

Posted: Wed Apr 13, 2011 2:32 pm
by TechnoCat
vrld's HUMP library will help with interpolations (tweens) too. http://vrld.github.com/hump/#timer-Interpolator

Re: Tweening

Posted: Wed Apr 13, 2011 11:43 pm
by EmmanuelOga
Here are a couple of examples you can try which use the "tweener" library.


https://github.com/EmmanuelOga/tweener/ ... mples/love

To run it, get git. Download it here:

https://github.com/EmmanuelOga/tweener/zipball/master

then:

cd tweener;
love examples/love/follow
love examples/love/circle
love examples/love/simple
love examples/love/size_and_pos