Jump to content

[URGENT] Progress (0-1) calculation between two given hours


Recommended Posts

Hello community!

I would like to do custom sky gradients that are changing color properly based on time. I got everything working I wanted except the main thing, which is the calculation of the fading. First I tried using interpolateBetween, but I came across a topic on stack overflow (link) from which I learned that I can use the RGB colors and some calculation for interpolation. But of course this also needs some kind of progress definition. I had several attempts on calculating the progress based on the current time so I would have a kind of automatic system, but every of them failed and I can't figure out how to do the calculation.

Here are my current and past calculations (kind of messed up)

function getMillisecondsBetweenHours(from, to)
	local newFrom, newTo = from, to
	if newFrom >= 24 then newFrom = 0 end
	if newTo >= 24 then newTo = 0 end
	if newFrom > newTo then newFrom = to newTo = from end
	
	local milliseconds = ((newTo-newFrom)*60)*getMinuteDuration()
	return milliseconds
end

		--self.progress = ((currentMinute)/((self.nextHour-currentHour)/(getMillisecondsBetweenHours(currentHour, self.nextHour)/1000)))
		--self.progress = ((currentMinute*(self.nextHour-currentHour))/(self.nextHour-currentHour)/60)/(getMillisecondsBetweenHours(currentHour, self.nextHour)/1000)
		--self.progress = ((((currentMinute*1000)-getMinuteDuration())/1000)/(self.differenceMilliseconds/1000))
		--local now = getTickCount()
		--local elapsedTime = now - self.startTick
		--local duration = self.differenceMilliseconds - elapsedTime
		--local calculatedProgress = (((currentMinute*self.differenceMilliseconds)/1000))
		--local calculatedProgress = (((currentMinute*1000)*60))/self.differenceMilliseconds
		--self.progress = ((calculatedProgress)/(self.nextHour-currentHour))/60
		self.progress = ((currentHour*60*1000)+(currentMinute*1000))/getMinuteDuration() + 1/(self.differenceMilliseconds/60)--+(currentHour*60*1000)+(currentMinute*60))/1000) --+ 1/((currentHour*60*1000)+(currentMinute*1000)/1000)
		--elapsedTime / duration
		print(self.progress)
		
		--local newSkyTopR, newSkyTopG, newSkyTopB = interpolateBetween(skyTopR, skyTopG, skyTopB, self.loaded[currentWeather][self.nextHour]["sky"]["top"].r, self.loaded[currentWeather][self.nextHour]["sky"]["top"].g, self.loaded[currentWeather][self.nextHour]["sky"]["top"].b, self.progress, "Linear")
		--local newSkyBottomR, newSkyBottomG, newSkyBottomB = interpolateBetween(skyBottomR, skyBottomG, skyBottomB, self.loaded[currentWeather][self.nextHour]["sky"]["bottom"].r, self.loaded[currentWeather][self.nextHour]["sky"]["bottom"].g, self.loaded[currentWeather][self.nextHour]["sky"]["bottom"].b, self.progress, "Linear")

		setSkyGradient((getNextHourData(currentWeather)["sky"]["top"].r-skyTopR)*self.progress+skyTopR, (getNextHourData(currentWeather)["sky"]["top"].g-skyTopG)*self.progress+skyTopG, (getNextHourData(currentWeather)["sky"]["top"].b-skyTopB)*self.progress+skyTopB, (getNextHourData(currentWeather)["sky"]["bottom"].r-skyBottomR)*self.progress+skyBottomR, (getNextHourData(currentWeather)["sky"]["bottom"].g-skyBottomG)*self.progress+skyBottomG, (getNextHourData(currentWeather)["sky"]["bottom"].b-skyBottomB)*self.progress+skyBottomB)

Please help me solve this problem, I can't think about any solution, how do I calculate a progress which goes from 0 to 1 for example from 3PM to 6PM in game (which is 3 minutes in game, and I made the update timer for setSkyGradient repeat every second using getMinuteDuration). So I would like the progress to automatically be 0.5 if its past 1.5 minutes and so on. I think it supposed to be possible using the time, or maybe I should use getTickCount?

Thank you for your kind help in advance!

Edited by Dzsozi (h03)
Link to comment
  • Moderators
13 hours ago, Dzsozi (h03) said:

getTickCount

Use getTickCount (milliseconds) if you do it based on machine time.

Use getRealTime().timestamp (seconds) if you want realtime.

When doing the calculations, start with the highest dimension.

hours = math.floor(seconds / (60 * 60))

seconds = seconds - (hours * 60 * 60)

progress = seconds / (60 * 60)

 

You can also use %, to cut off large dimensions. Starting with the dimension you do not needs. Need hours + rest? = cut off days

But that doesn't work for days/years, since there are not always 365 days in a year.

 

Progress goes from 0 to 0,9999999999.

 

 

 

 

 

 

 

Edited by IIYAMA
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...