-1

I currently have a function that updates a value using the sin function, and a timeFactor double that keeps track of how much time has passed since the program started:

double timeFactor;
double delta;
while(running) {
  delta = currentTime - lastTime;
  timeFactor += delta;
  var objectX = sin(timeFactor);
}

However, I need to convert this code to use the delta rather than the timeFactor.
I.e. For updating to sin(time+delta) I only want to use the current value of sin(time) and anything calculated from the value of delta.
I.e. calcualte sin(time+delta) == f(sin(time),delta)

How do I do this?

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
user1765354
  • 347
  • 1
  • 5
  • 21
  • It isn't clear what you are trying to do. What do you even mean by "convert the code to use the delta rather than the timeFactor"? If you want `sin(delta)` rather than `sin(timeFactor)` -- what is preventing you? – John Coleman Mar 08 '18 at 18:54
  • Does that mean swap sin(timeFactor) with sin(delta)? – duffymo Mar 08 '18 at 18:55
  • Do you mean that you want to update `objectX` using its previous value and `delta`, rather than using `timeFactor`? – Beta Mar 08 '18 at 19:11
  • Yes, I want to update objectX using its previous value and delta. – user1765354 Mar 08 '18 at 19:11
  • 1
    sin(A + B) = sin A cos B + cos A sin B – Yunnosch Mar 08 '18 at 19:15
  • @Yunnosch That is the way to go. It would thus require a second variable to be kept track of and updated at each step. Perhaps you could turn it into an answer. – John Coleman Mar 08 '18 at 19:18
  • If you are going to have to keep track of state anyway, why *don't* you want to keep track of `timeFactor`? Any alternative to what you are already doing will just burn more CPU cycles for no apparent gain. – John Coleman Mar 08 '18 at 19:25
  • @JohnColeman Thanks for the support. I am however not sure I got your idea of a second variable correctly. Please comment on my answer. – Yunnosch Mar 08 '18 at 19:36
  • @Yunnosch the problem with your current answer is that `cos(A)` is only `sqrt(1 - sin(A)*sin(A))` in 2 out of the 4 quadrants. I don't see any good alternative to keeping track of a second variable for the cosine. – John Coleman Mar 08 '18 at 19:41
  • @JohnColeman Good point. I will update the answer. – Yunnosch Mar 08 '18 at 19:43
  • @JohnColeman Should be fixed now. Feel free to comment. And of course thank you very much. – Yunnosch Mar 08 '18 at 20:03
  • @Yunnosch Looks great. +1 – John Coleman Mar 08 '18 at 20:12
  • OP, I edited the quesiton, adding a sentence to express the goal via function expression. Please double check that it correctly represents your goal. Otherwise I will of course roll back. – Yunnosch Mar 08 '18 at 20:19
  • OP, I did not touch this code line `var objectX = sin(timeFactor);`. I recommend to explain it a little. Is `var` a typedefed type? It is not really self-explanatory. Maybe you can shed a downvote if you explain it. – Yunnosch Mar 08 '18 at 20:29

1 Answers1

3

From math:
sin(A + B) == sin(A) * cos(B) + cos(A) * sin(B)
cos(A + B) == cos(A) * cos(B) - sin(A) * sin(B)

Store sin(A) and cos(A) in two variables. Then for updating them use temporary copy of one of them, otherwise you will update the second using the new instead of the old value of the first.

Assuming:

  • persistent objectX stores current and is initialised with initial sin(timeFactor)
  • persistent objectXc stores current and is initialised with initial cos(timeFactor)
  • temporary objectXt stores a copy of objectX

("persistent" as in "keeps value across executions of update code", in contrast to "temporary" as in "only keeps value during update code"; this is to avoid using the "global" attribute, which implies poor data design)

Update code:

objectXt = objectX;
objectX  = objectX * cos(delta) + objectXc * sin(delta);
objectXc = objectXc* cos(delta) - objectXt * sin(delta);

Credits to John Coleman for spotting the problem in initial idea to use
1 == sin(A)*sin(A)+cos(A)*cos(A)
That would have been actually
sin(time+delta)== f(sin(time), delta)
But it fails for 50% of a full period.
So I hope this
sin(time+delta)==f(sin(time), cos(time), delta)
also helps.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54