Thread Tools Display Modes
02-06-10, 12:25 AM   #1
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
Originally Posted by Nemnacill View Post
I'm not sure about how it would work in lua, but any pulsating effect can be achieved with a simple sinus function.

pseudo code:
var sequence = 0;
sequence ++;
AlphaProperty = sin(sequence);

this would result in the AlphaProperty going from 0 to 1, then from 1 to 0 and so on. google 'math sin lua' for more info

Good luck
True, but that would require an onupdate function. And an onupdate function gets called at every frame. So if you run at 120 FPS your calling a sin function 120 times per second. Not that efficient as running my way(look above).
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.
  Reply With Quote
02-06-10, 02:59 AM   #2
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by nightcracker View Post
<snip>So if you run at 120 FPS your calling a sin function 120 times per second.<snip>
Not true.

Code:
do
	local last_update = 0
	local updater = CreateFrame("Frame", nil, UIParent)

	updater:Hide()
	updater:SetScript("OnUpdate",
			  function(self, elapsed)
				  last_update = last_update + elapsed

				  if last_update >= 0.25 then
					  RunSinFunction()
					  last_update = 0
				  end
			  end)
end
That executes the sin function every quarter of a second.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
02-06-10, 04:25 AM   #3
Hati-EK
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 20
Originally Posted by Torhal View Post
Not true.

Code:
do
	local last_update = 0
	local updater = CreateFrame("Frame", nil, UIParent)

	updater:Hide()
	updater:SetScript("OnUpdate",
			  function(self, elapsed)
				  last_update = last_update + elapsed

				  if last_update >= 0.25 then
					  RunSinFunction()
					  last_update = 0
				  end
			  end)
end
That executes the sin function every quarter of a second.
Code:
last_update = last_update -0.25
is more precise and does not skip seconds (even if this would just be some miliseconds) than reseting the whole value to 0 - (ie last_update could be 0.25182965 - so you would missing 0.00182965 - if you have this ~1000 times there's something missing

Last edited by Hati-EK : 02-06-10 at 04:27 AM.
  Reply With Quote
02-06-10, 06:43 AM   #4
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
Originally Posted by Hati-EK View Post
Code:
last_update = last_update -0.25
is more precise and does not skip seconds (even if this would just be some miliseconds) than reseting the whole value to 0 - (ie last_update could be 0.25182965 - so you would missing 0.00182965 - if you have this ~1000 times there's something missing
Nope, because THIS time if you run at 120 fps then the function would run a lot more then when at 10 fps.
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.
  Reply With Quote
02-06-10, 08:08 AM   #5
Hati-EK
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 20
Originally Posted by nightcracker View Post
Nope, because THIS time if you run at 120 fps then the function would run a lot more then when at 10 fps.
this is right - at 120fps the function is more fluently ... as the frequency is another - 1/120 vs. 1/10 - but the sampling frequency is 0.25 - so at 120fps it would take 30frames to match the sampling frequency - with 10fps it would take 2.5frames (3frames as there are NO floating frames)


120 fps:
approx. elapsed = 1/120 = 0.0083333333333333s per OnUpdate ( called frequence g in rest of text )
sampling frequency f = 0.25 Hz (or 4fps = 1/4Hz)
f = 30*g
so every 30-frame((n*30)%120, n<-N{0,1,2,..) the SinusFunction() is called - at this point it works fine

now think of ppl having an average fps of not 120fps - like you said 10fps
drawing frequency h = 1/10 Hz
sampling frequency f = 0.25 Hz
resulting that point of collide = frame2.5, as there is no frame 2.5, it will take frame 3 (>=)
time between frame2.5 and frame3 is t=((frame3-frame2)/2);
frame2 is drawn after 0.2s, frame3 after second 0.3 (on time graph) - would result in a 0.05s delay

so with last_update=0, it will result in:
frame1 drawn - frame2 drawn - frame3 drawn - function fires, last_update is now 0 again - frame4 drawn - frame5 drawn (NOW it should fire again - but it doesn't as this is again 0.2 not 0.25) - frame6 drawn - function fires and again 3 frames required


with last_update=last_update-sampling_frequency:
frame1 drawn - frame2 drawn - frame3 drawn - function fires, last_update now is 0.05 - frame4 drawn (last_update 0.15) - frame5 drawn(last_update 0.25) - function fires correctly at (50% of fps as it should with sampling frequency 0.25 - and not at 60%) - frame 6 drawn - etc...


and ppl have not an average fps of continuously 120fps (atleast not the most have)

Last edited by Hati-EK : 02-06-10 at 08:19 AM. Reason: replaced 1 s by Hz
  Reply With Quote
02-06-10, 08:31 AM   #6
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
Originally Posted by Hati-EK View Post
this is right - at 120fps the function is more fluently ... as the frequency is another - 1/120 vs. 1/10 - but the sampling frequency is 0.25 - so at 120fps it would take 30frames to match the sampling frequency - with 10fps it would take 2.5frames (3frames as there are NO floating frames)


120 fps:
approx. elapsed = 1/120 = 0.0083333333333333s per OnUpdate ( called frequence g in rest of text )
sampling frequency f = 0.25 Hz (or 4fps = 1/4Hz)
f = 30*g
so every 30-frame((n*30)%120, n<-N{0,1,2,..) the SinusFunction() is called - at this point it works fine

now think of ppl having an average fps of not 120fps - like you said 10fps
drawing frequency h = 1/10 Hz
sampling frequency f = 0.25 Hz
resulting that point of collide = frame2.5, as there is no frame 2.5, it will take frame 3 (>=)
time between frame2.5 and frame3 is t=((frame3-frame2)/2);
frame2 is drawn after 0.2s, frame3 after second 0.3 (on time graph) - would result in a 0.05s delay

so with last_update=0, it will result in:
frame1 drawn - frame2 drawn - frame3 drawn - function fires, last_update is now 0 again - frame4 drawn - frame5 drawn (NOW it should fire again - but it doesn't as this is again 0.2 not 0.25) - frame6 drawn - function fires and again 3 frames required


with last_update=last_update-sampling_frequency:
frame1 drawn - frame2 drawn - frame3 drawn - function fires, last_update now is 0.05 - frame4 drawn (last_update 0.15) - frame5 drawn(last_update 0.25) - function fires correctly at (50% of fps as it should with sampling frequency 0.25 - and not at 60%) - frame 6 drawn - etc...


and ppl have not an average fps of continuously 120fps (atleast not the most have)
What I say now I assuming you meant this:

Code:
do
	local last_update = 0
	local updater = CreateFrame("Frame", nil, UIParent)

	updater:Hide()
	updater:SetScript("OnUpdate",
			  function(self, elapsed)
				  last_update = last_update + .25

				  if last_update <= -.25 then
					  RunSinFunction()
					  last_update = 0
				  end
			  end)
end
Then that is equal to this:
Code:
do
	local start = 0
	local updater = CreateFrame("Frame", nil, UIParent)

	updater:Hide()
	updater:SetScript("OnUpdate", function(self, elapsed)
		start = start + .25
		SomeFrame:SetAlpha(math.sin(start))
	end)
end
Which means that the speed of the pulsing is determined by your FPS, not the amount of fluentness(is that a word?).
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.

Last edited by nightcracker : 02-06-10 at 08:42 AM.
  Reply With Quote
02-06-10, 08:41 AM   #7
Hati-EK
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 20
Originally Posted by nightcracker View Post
What I say now I assuming you meant this:

Code:
do
	local last_update = 0
	local updater = CreateFrame("Frame", nil, UIParent)

	updater:Hide()
	updater:SetScript("OnUpdate",
			  function(self, elapsed)
				  last_update = last_update + .25

				  if last_update <= -.25 then
					  RunSinFunction()
					  last_update = 0
				  end
			  end)
end
Then that is equal to this:
Code:
do
	local start = 0
	local updater = CreateFrame("Frame", nil, UIParent)

	updater:Hide()
	updater:SetScript("OnUpdate", function(self, elapsed)
		start = start + .25
		SomeFrame:SetAlpha(math.sin(start))
	end)
end
Which means that the speed of the pulsing is determined by your FPS, not the amount of fluentness(is that a word?).

nah
i meant this :
Code:
do
	local last_update = 0
	local updater = CreateFrame("Frame", nil, UIParent)

	updater:Hide()
	updater:SetScript("OnUpdate",
			  function(self, elapsed)
				  last_update = last_update + elapsed

				  if last_update >= 0.25 then
					  RunSinFunction()
					  last_update = last_update-0.25
				  end
			  end)
end

Originally Posted by Torhal View Post
This is true, if you're looking to be precise.
well it's a sinus function - where little differences may cause weird results or atleast not nice looking ones

Last edited by Hati-EK : 02-06-10 at 08:44 AM.
  Reply With Quote
02-06-10, 08:30 AM   #8
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by Hati-EK View Post
Code:
last_update = last_update -0.25
is more precise and does not skip seconds (even if this would just be some miliseconds) than reseting the whole value to 0 - (ie last_update could be 0.25182965 - so you would missing 0.00182965 - if you have this ~1000 times there's something missing
This is true, if you're looking to be precise.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote

WoWInterface » Developer Discussions » Graphics Help » "Pulsating" effect...what's the proper method?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off