Tweening

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Tweening

Post by Anxiety »

How can i tween an image between two positions? Yet again, its for the game im making,

Thanks!
I can't come up with a good signature!
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Tweening

Post 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
Last edited by ivan on Wed Apr 13, 2011 6:02 am, edited 1 time in total.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Tweening

Post 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)
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: Tweening

Post by TechnoCat »

vrld's HUMP library will help with interpolations (tweens) too. http://vrld.github.com/hump/#timer-Interpolator
User avatar
EmmanuelOga
Citizen
Posts: 56
Joined: Thu Apr 22, 2010 9:42 pm
Location: Buenos Aires, Argentina
Contact:

Re: Tweening

Post 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
--------------------------------------------------------------------------------------------------------
http://EmmanuelOga.com
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests