Calculating Something Else Problem

Miscellaneous Forums/General Discussion/Calculating Something Else Problem

I'm having problems calculating something that should be simple too. I'm trying to pass back a sine loop with a specified wavelength and amplitude.

The idea is to return a smooth value for texture manipulation ( texture positioning, scaling, etc. )

I'm using
Return Sin(Abs((TimePeriod/CurrentTime)*180.0-90))*Amplitude


TimePeriod is constant and CurrentTime is always >0 and <=TimePeriod. Both in Milliseconds.

It's almost ok, but it kinda "bumps" as it cycles to a new period. It's not quite smooth at the end, as it is at the halfway point ( or possibly vice versa, I've watched this thing animate so many times, I'm dizzy! ) It's almost as though I'm cutting it off too soon. I'm taking it across a scale of 180 degrees though, which seems right, no?

You need to make sure that DeltaCurrentTime is always a demonitator of Timeperiod.

For example assume timeperiod is 10 and DeltacurrentTime is 3

10/3 10/6 10/9 10/3 As you can see the forth time you call the function the gap is 4 and not 3

However if you made timeperiod 12, and deltacurrettime 3
12/3 12/6 12/9 12/12 12/3 etc

Obviously it is presupmtuis of me to assume that you hadnt been aware of this. But you havent produced code for production of DeltaCurrenttime or even implied that its calculated at all. But this error would definetly produce the end of cylce "Jump" you described

If the formula I've got there looks right, then yep, it'll be a timing problem. I can fix that but it's complicated and didn't want to burden anyone else with it. Just wanted to make sure my formula was correct first.

Well no, sorry didnt think of it before. Swap Currenttime and TimePeriod round (and dont bother to reset currnttime after a full loop, but this might not be totaly ness)

Return Sin(Abs((CurrentTime/Timeperiod)*180.0-90))*Amplitude

Even if you dont bother changing anything else this should help (I think)

Edit: Are you sure you didnt edit your code, because I feel really stupid for not actualy seeing that the first time, and just reading the dianosis without looking at the paitent.

Quite sure, yep, although I think I may have written it incorrectly in the first instance. I've fiddled with this so many times, I can't even remember what it was to begin with. Probably best to rip it out and begin over.

OK, I think this is how the code originally was. Including timing code.

Local TimeSoFar:Float=TimeNow-PeriodBegan
			
	If TimeSoFar>Value2
				
		TimeSoFar=TimeSoFar Mod Value2
				PeriodBegan=TimeNow-TimeSoFar
				
	End If
			
	Local PeriodFactor:Float=(Float(TimeSoFar)/Value2)
						
	Return Cos(Abs((PeriodFactor)*180.0-90))*Value1



Local TimeSoFar:Float=TimeNow-PeriodBegan
			
	If TimeSoFar>Value2
				
		TimeSoFar=TimeSoFar - Value2  ' <--------------------
				PeriodBegan=TimeNow-TimeSoFar
				
	End If
			
	Local PeriodFactor:Float=(Float(TimeSoFar)/Value2)
						
	Return Cos(Abs((PeriodFactor)*180.0-90))*Value1
This will obviously fall down if TimesoFar is ever > Value2*2 And it doesnt fix the problem. But it is faster

You're right, it doesn't fix the problem and TimeSoFar can be > Value2*2 which is why I changed it to a mod.

As far as I can tell, the problem does not lie in the timing.

TimeSoFar can be > Value2*2


Well thats your problem right threre, Value2 is the length of a whole cycle, and when Timesofar is greater than that it is reduced to "LESS THAN VALUE2" in both mine and your method.

For TimesoFar to ever be > Value2*2 then DaltaTimesofar has to be greater than Value2. But Value two is the wavelength, so you are taking sample too infrequently.

Float(TimeSoFar)/Value2

This seems a bit pointless Timesofar is already a float. Maybe

TimeSoFar/Float (Value2)

For TimesoFar to ever be > Value2*2 then DaltaTimesofar has to be greater than Value2. But Value two is the wavelength, so you are taking sample too infrequently.


Nope, I'm taking a sample every frame. It's just that it doesn't get updated when it's not being displayed so every time it is reactivated, it's possible for TimeSoFar to be very high, which is why I coded it so that this doesn't cause a problem.

Yes, TimeSoFar is a float now, but wasn't originally. I obviously spotted that it either needed to be a float or I needed to cast to a float for the division and ended up doing both.

Still neither of those are the problem.

Gabriel: I've got a TSineCounter type in my framework, take a look, it *might* help you out.