How do I sine a variable?

0 favourites
  • 8 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • I'm trying to figure out a formula to sine a variable from 0 to 500, then back to 0 repeatedly in a sine shape; preferably in a single line. Does anyone have a straight-forward way to do this?

  • Since Sin() goes from 0 to 1 to 0 to -1, maybe abs(Sin()*500) would fit your need ?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • How would you control the speed, though?

  • As says the sine function returns a value between -1 and 1. The input to the function repeats every 2 Pi because of it's relation to Pi and a circle. However, if you tweak the input and output a little you can change them to the ranges you want

    Firstly the input; if you wanted it to repeat every 20 seconds instead of every 6.28 ( 2 * Pi ) seconds what you need to do it "normalise" your input by dividing it by the desired period time / 20 this will give you a value between 0 and 1. Now you can multiply by the required period. (time / 20) * (2 * Pi) or simplified (time / 10) * Pi.

    Secondly the output range. You only want a positive value, but there's actually 2 ways of doing this, depending on how you want it to behave. An easy option is to use absolute to convert it to a positive value like so abs(sin(time)). But remember that sine is a curve, what you've effectually done is place a mirror along the x-axis, hence it will be rounded at the top and spiky at the bottom. In animation this gives you a bouncing ball effect. Accelerating as it falls, and decelerating as it climbs. The other option is to offset the curve so that it is always positive 1 + sin(time) but this will change the range to between 0 and 2. We wanted an output of 0 to 500 anyway so we need half the output then scale it by 500 ((1+sin(time)) / 2) * 500 or if we simplify it (1+sin(time)) * 250.

    Let's combine those 2 parts together:

    250 * (1 + sin((time / 10) * Pi))

    When testing things like this it's useful to actually visualise the results on a graph. I recommend trying it out on a graphing tool like Desmos.

    Here's some of the formulas for the expressions I mentioned, you should just be able to paste them into Desmos.

    • \sin x
    • \sin\left(\frac{x\ \cdot\ \pi}{10}\ \right)
    • \left|\sin\left(x\right)\right|
    • 1-\left|\sin\left(x\right)\right|
    • \frac{\left(1\ +\ \sin\left(\frac{x\ \cdot\ \pi}{10}\ \right)\right)}{2}\
    • \left(1\ +\ \sin\left(\frac{x\ \cdot\ \pi}{10}\ \right)\right)\ \cdot\ 5
  • Nepeo - thank you for the explanation! Very helpful; I’ll have to read it a few times to understand but it’s exactly what I was looking for!

    Thank you, too, Semorah!

  • I didn't know Desmos, it's a cool website ! :D

    Nepeo's formula repeats way better, mine only works for 1 Wave.

    Just keep in mind that if you want to start from 0 you need to start your timer from X=-5

  • Glad I could help, if either of you have any questions feel free to ask me. These sorts of transforms are very useful to know, and can be applied to all sort of stuff. They can be a little difficult to visualise at first because you have to think about a range of values not a single absolute value. Also the complete solution can be very confusing to look at. Think about each step you need to do, then work them together to get the result.

    I briefly touched on some similar maths in my tutorial for the Advanced Random plugin for tweaking noise functions. Noise functions basically take an input value and transform it into a new value. I actually used sine for some examples in the tutorial because it behaves exactly like a noise function.

    I've come across a few websites over the years that allow you to look at graphs of a function like that. I normally just search around for them. Saying that I'm fairly sure I've used Desmos in the past, and it's one of the best ones I've used! is right that if you want it to start at 0 you will have offset it on the x direction by a quarter of it's period. Usefully cosine is identical to sine but offset by a quarter of it's period. So you can use (1 + cos((x * Pi) / 10)) * 5 (or for Desmos \left(1\ +\ \cos\left(\frac{x\ \cdot\ \pi}{10}\ \right)\right)\ \cdot\ 5 ) to get the same curve but starting at 0.

  • Not to forget though: The sine behavior has a "value only" option and can be used for that too.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)