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 » Wed Apr 13, 2011 5:28 am

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: 1547
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Tweening

Post by ivan » Wed Apr 13, 2011 5:47 am

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: 1260
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Tweening

Post by BlackBulletIV » Wed Apr 13, 2011 5:53 am

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: Denver, CO
Contact:

Re: Tweening

Post by TechnoCat » Wed Apr 13, 2011 2:32 pm

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 » Wed Apr 13, 2011 11:43 pm

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 3 guests