Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / SIN() question

Author
Message
nz0
AGK Developer
17
Years of Service
User Offline
Joined: 13th Jun 2007
Location: Cheshire,UK
Posted: 19th Apr 2017 00:06

If I wanted to operate a sin wave between -45 and 45, I use something like this:

sh#=sin(SinStep0#*_PI)*45.0
SinStep0#=SinStep0#+1.9

However, I want to ensure that the full wave is operating exactly over 60 samples (which seems to work out roughly stepping 1.9 per sample.
What's the actual calculation to get 60 (or any) sample points per wave?
programming is ace
User Banned
Posted: 19th Apr 2017 00:09
sh#=(sin(SinStep0#*_PI)*45.0) * 100
SinStep0#=(SinStep0#+1.9 * sh#) * 60
nz0
AGK Developer
17
Years of Service
User Offline
Joined: 13th Jun 2007
Location: Cheshire,UK
Posted: 19th Apr 2017 00:39
That doesn't work. The 1.9 is a "magic number" that is close to the right value.
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 19th Apr 2017 03:20
I'm not completely sure how you want your sine wave to be 'configured', but I set up an example for a full wave ( 360° ).

Change amplitude# and steps as you please...

Increasing or decreasing the number of steps won't change the wave form of course, but if I understood you correctly, you wanted a full 360° wave with X samples.

P.S. I never code without #option_explicit, I just dropped it for better readability!




PSY LABS Games
Coders don't die, they just gosub without return
nz0
AGK Developer
17
Years of Service
User Offline
Joined: 13th Jun 2007
Location: Cheshire,UK
Posted: 19th Apr 2017 19:33
Basically, if you called my 2 lines 60 times, it would be a complete sine wave.
Adding 1.9 to SinStep0# is a magic number that, after 60(ish) calls, our single sine wave is reset.

I wanted to know how to obtain the 1.9 "magic" number...
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 19th Apr 2017 20:36 Edited at: 19th Apr 2017 21:12
That's why I posted the code, so you could understand

A full wave goes from 0° to 360°
If you want to do 360 steps, you need 360°/360 = 1° steps
If you want to do 60 steps, you need 360°/60 = 6° steps

In your example, you do the following: ( Multiplying the angle# with PI changes the sine wave's period, multiplying the sine value with 45.0 changes the amplitude )

y#=sin ( angle# * PI ) * 45.0
angle# = angle# + 1.9

After 60 steps, your angle# is 60° * 1.9 = 114°
When calculating y#, you multiply the angle# by PI, so 114° * 3.1415927 = 358.14156°, which is almost 360°, which is a full circle, which 'resets' your sine wave

To obtain the 'magic' number IN YOUR EXAMPLE, you just have to do the following calculation: 360° / PI / 60 steps = 1,9098593171027440292266051604702
In MY EXAMPLE, I therefore just did 360° / steps, because I wasn't altering the period of the sine wave by multiplying it with PI or whatever value

(edit) https://en.wikipedia.org/wiki/Sine_wave - Scroll down and have a look at the animated graphic 'Illustrating the cosine wave's fundamental relationship to the circle'

Cheers
PSY
PSY LABS Games
Coders don't die, they just gosub without return
nz0
AGK Developer
17
Years of Service
User Offline
Joined: 13th Jun 2007
Location: Cheshire,UK
Posted: 20th Apr 2017 02:08
Thanks. I'll have a play later. That's a really nice explanation.

My situation requires me to have exactly 60 "samples" in a full looping wave. Never had to do that before, but I'm working with animated models which will go out of sync if I didn't get this fully locked in.



Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 20th Apr 2017 02:36

to compute the steps in degrees it's just StepDelta = 360.0 / Steps


PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code

Login to post a reply

Server time is: 2024-09-30 01:22:45
Your offset time is: 2024-09-30 01:22:45