Animate variables at equal speeds

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.
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Animate variables at equal speeds

Post by veethree »

Subject lacks sense..But this hopefully wont..

What i'm trying to achieve is something fading from one color to another, And i actually have achieved this. But the problem is that the speed at which it fades between the two colors depends on how far apart the RGB values are, But i want it to fade at the same speed. So any idea how that can be done? Thanks in advance.

Here's the code:

Code: Select all

function love.load()
	math.randomseed(os.time())
	cur = {r = math.random(0, 255), g = math.random(0, 255), b = math.random(0, 255)}
	des = {r = cur.r, g = cur.g, b = cur.b}
	fades = 150
	showhud = true
end

function love.update(dt)
	if cur.r < des.r then
		cur.r = cur.r + fades * dt
	end
	if cur.g < des.g then
		cur.g = cur.g + fades * dt
	end
	if cur.b < des.b then
		cur.b = cur.b + fades * dt
	end
	if cur.r > des.r then
		cur.r = cur.r - fades * dt
	end
	if cur.g > des.g then
		cur.g = cur.g - fades * dt
	end
	if cur.b > des.b then
		cur.b = cur.b - fades * dt
	end
end

function love.draw()
	love.graphics.setBackgroundColor(cur.r, cur.g, cur.b)
	if showhud then
		love.graphics.print("Des: R: "..des.r.." G: "..des.g.." B: "..des.b, 10, 10)
		love.graphics.print("Cur: R: "..math.floor(cur.r).." G: "..math.floor(cur.g).." B: "..math.floor(cur.b), 10, 25)
	end
end

function love.keypressed(key)
	if key == "n" then
		des.r = math.random(0, 255)
		des.g = math.random(0, 255)
		des.b = math.random(0, 255)
	elseif key == "f3" then
		showhud = not showhud
	end
end
You can also download the .love below if that's what you prefer.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Animate variables at equal speeds

Post by tentus »

Think about "steps" in the process of fading. You're already multiplying by "fades" (a speed variable?), so now you need to make that also account for a variable which basically describes how much change you do.

To use a real world analogy, imagine two men walking to two different point. One is point is further than the other. If we could alter the stride length of one of the men, we could make them arrive at their disparate destinations in the same number of steps.

Hence my word choice above. We need a "step" size that varies to accommodate for different distances between colors.
Kurosuke needs beta testers
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Animate variables at equal speeds

Post by trubblegum »

Pretty good analogy.
Something like :

Code: Select all

step = (final - initial) / (dt / duration)
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Animate variables at equal speeds

Post by kikito »

You can also use tween.lua to do this in 3 lines. It will handle the transitions, multiple color components, and the like.

Disclaimer: I'm the programmer of tween.lua.
When I write def I mean function.
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: Animate variables at equal speeds

Post by veethree »

kikito wrote:You can also use tween.lua to do this in 3 lines. It will handle the transitions, multiple color components, and the like.

Disclaimer: I'm the programmer of tween.lua.
I don't like using libraries too much..i prefer doing things myself. (with some help from the forums in some cases..lol) I don't really know why..feels like cheating. lol
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Animate variables at equal speeds

Post by Robin »

veethree wrote:I don't like using libraries too much..i prefer doing things myself. (with some help from the forums in some cases..lol) I don't really know why..feels like cheating. lol
Don't let that feeling hold you back. Using libraries means less bugs, which makes your games more stable, and means a higher degree of code reuse.
Help us help you: attach a .love.
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Animate variables at equal speeds

Post by trubblegum »

Robin wrote:
veethree wrote:I don't like using libraries too much..i prefer doing things myself. (with some help from the forums in some cases..lol) I don't really know why..feels like cheating. lol
Don't let that feeling hold you back. Using libraries means less bugs, which makes your games more stable, and means a higher degree of code reuse.
And they can allow you to do stuff you might otherwise need a science degree for, without first getting the degree.

If, like me, you're a very bad person who already knows they're going to a special hell where all the beer is warm and flat, and everybody knows your name but they all mispronounce it intentionally, you might even occasionally rewrite libs to do things your way .. but would you walk across the country because using a car is like cheating?
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Animate variables at equal speeds

Post by coffee »

veethree wrote:
kikito wrote:You can also use tween.lua to do this in 3 lines. It will handle the transitions, multiple color components, and the like.

Disclaimer: I'm the programmer of tween.lua.
I don't like using libraries too much..i prefer doing things myself. (with some help from the forums in some cases..lol) I don't really know why..feels like cheating. lol
Apple say "There's an app for that!", Lovers usually answer "There's a lib for that!". And there's people like Kikito that have so many libs that can also say "and it's mine!". :D
There isn't nothing wrong about using libraries of course. However if you trying to do things for yourself I agree with you because it feels like cheating. Less effort is nice and is a lot less time wasted but also means less learning, less brain-development, more dependence. It's a question of want be more or less user than coder. So I cheer you up and say that you aren't alone in that pursuit of do things.
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: Animate variables at equal speeds

Post by veethree »

trubblegum wrote:
Robin wrote:
veethree wrote:I don't like using libraries too much..i prefer doing things myself. (with some help from the forums in some cases..lol) I don't really know why..feels like cheating. lol
Don't let that feeling hold you back. Using libraries means less bugs, which makes your games more stable, and means a higher degree of code reuse.
And they can allow you to do stuff you might otherwise need a science degree for, without first getting the degree.

If, like me, you're a very bad person who already knows they're going to a special hell where all the beer is warm and flat, and everybody knows your name but they all mispronounce it intentionally, you might even occasionally rewrite libs to do things your way .. but would you walk across the country because using a car is like cheating?
If i ever make a game that requires something that would require a science degree to write, Of course i'd use a lib. But for simple shit like this i'd just rather do it myself.

As for your car metaphor, The way i see it, If you walk, You get across the country all by yourself and you have all the bragging rights. But if your friend gives you a piggy back ride half the way, you still get there, but your friend did a lot of the work.

But i have nothing against libs and people who use them, I just like doing things myself.
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Animate variables at equal speeds

Post by trubblegum »

People seem to set a lot of store by this "bragging rights".
My take is that - beyond gaining experience with the language - the only real benefit in writing your own lib is that you can optimize it for your own specific requirements.
Post Reply

Who is online

Users browsing this forum: No registered users and 143 guests