2
$\begingroup$

I am not a mathematician, so apologies for my lack of knowledge/terminology.

For the day/night cycle in a video game, I'd like to use a sine wave to determine the lighting for the scene. (Where $X$ is the number of seconds elapsed)

For this graph, I'd like the maximum point to be $1$ and the minimum point to be $0$.

I'd like the full day/night to take $15$ minutes, or $900$ seconds. (So I believe that would be a "period" of $900$, though I'm not sure)

But this is where things get too complex for me. I'd like the night's duration to be half the day. In other words, the duration of the rise from $0.5$ to $1$ be twice the duration of the dip from $0.5$ to $0$.

Any help with this problem would be super appreciated by me and my team!

EDIT: I would like it to start at 1

$\endgroup$
  • $\begingroup$ What initial level of lighting do you want at the very start of game? $\endgroup$ – user810677 Sep 3 at 0:44
  • $\begingroup$ Something like this desmos.com/calculator/nq633oqmvw ? $\endgroup$ – user810677 Sep 3 at 1:32
  • 1
    $\begingroup$ Or if you don't want it to start at 0, try this one and hit the play button on the left desmos.com/calculator/ytgvxl8x7w $\endgroup$ – user810677 Sep 3 at 1:35
  • 1
    $\begingroup$ Well, if you want the curve from a very-close-to-reality simulation, there's lots of details here. There's a Desmos graph that lets you play around with axial tilt and latitude and see what kind of day/night cycle you'd get. Probably more than you want, but it's there if you'd like to check it out. $\endgroup$ – JonathanZ supports MonicaC Sep 3 at 3:23
  • $\begingroup$ $y=(1/2)(1+\cos(2\pi x/900))$. $\endgroup$ – Gerry Myerson Sep 3 at 3:36
0
$\begingroup$

The simplest way to do this is to take two sine waves, say $f$ and $g$, with $g$ having half the period of $f$, and piece them together accordingly:

Let the parameter $t$ be thought of as time, and $f(t)$ the brightness at time $t$. We will admit brightness between $-1$ and $1$, which can be converted as desired (e.g. height $-1 = 50$ lumens, and height $1 = 50,000$ lumens).

Let $f(t) = \sin(t)$, and $g(t) = -\sin(2t)$. Define a new function: $$ F(t) = \begin{cases} f(t), & t \in [0,\pi], \\ g(t), & t\in [\pi,\frac{3\pi}{2}]. \end{cases} $$ Notice that $f(\pi) = g(\pi) = 0$, so $F$ is continuous (as we might hope a brightness function in a video game to be). Moreover, $F$ can be copied side by side indefinitely, to form a new function which will be $\frac{3\pi}{2}$ periodic. To be technical, the function you really want is (where $n \in \mathbb{N}$): $$ G(t) = \begin{cases} f(t-\frac{3\pi n}{2}), & t \in [\frac{3\pi n}{2},\frac{3\pi n}{2} + \pi], \\ g(t-\frac{3\pi n}{2}), & t\in [\frac{3\pi n}{2} + \pi, \frac{3\pi n}{2} + \frac{3\pi}{2}]. \end{cases} $$ Below is an example of how it would look iterated over $2$ cycles:

enter image description here

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ I'm pretty sure the minimum of the desired graph is $0$, not $-1$. $\endgroup$ – Aiden Chow Sep 3 at 4:00
  • $\begingroup$ I tried this thing just now, where F is used when Y > .5 and G is used when Y <= .5. Didn't work. Is that not right? My question is, when do I make the switch $\endgroup$ – Eyesight Technology Sep 3 at 4:09
  • $\begingroup$ @AidenChow Yes I saw the desired min. was $0$ not $-1$ but it's trivial to just add $1$ to the entire function, and then scale down by $1/2$. I left those things out for the sake of simplicity and because it's not the focus of the problem, imo. $\endgroup$ – ccroth Sep 3 at 23:57
  • $\begingroup$ @EyesightTechnology I graphed it in Desmos exactly how I wrote it here, and it produces the above graph. Look carefully at the intervals for each piece of the function, and note the arguments of the component functions (for $G$) are $t - 3\pi n/2$ rather than just $t$. $\endgroup$ – ccroth Sep 4 at 0:01
0
$\begingroup$

Yes, the period would be 900 seconds. Since your range is from 0 to 1, your amplitude is .5, and your midline is also .5.

Assuming no phase shift (you're starting at your midline as the sun is rising), the basic function you're going to use is $f(t)=a\sin(\frac{2\pi}{P}t)+m$, where $P$ is the period, $a$ is the amplitude, and $m$ is the midline. For now, we'll leave $P$ alone and say $a=m=.5$, so you have $f(t)=.5\sin(\frac{2\pi}{P}t)+.5$.

As has been suggested, a piecewise function is probably easiest. However, since there's two parts day for one part night, you need to have two thirds of period taken up by the first half of the cycle, and one third taken up by the second half of the cycle. This means four thirds of a full period for day, and two thirds for night.

You can do the intermediate calculations for yourself to check it if you want, but you end up with $D(t)=.5\sin(\frac{\pi}{600}t)+.5$ for the day and $N(t)=.5\sin(\frac{\pi}{300}t)+.5$ for the night. To get them to line up right, introduce a phase shift to the night function to get $N(t)=.5\sin(\frac{\pi}{300}t-2\pi)+.5$.

From here, I would recommend having the in-game time calculated on a repeating cycle so you can just define the domain of this function as $[0,900)$, and then it should work. To clarify, your time function looks like this:

$$ F(t) = \begin{cases}.5\sin(\frac{\pi}{600}t)+.5, t \in [0,600) \\ .5\sin(\frac{\pi}{300}t-2\pi)+.5, t\in [600, 900) \end{cases}. $$

Edit: I'm just now noticing you wanted to have it start at 1. In that case, just set the timer to start 300 seconds in.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.