< User:Technoboy10

How I code with sine (no trig required)

Basic

Basically all you need to know for this method is that the sine function goes from -1 to 1 and back again as you feed it bigger and bigger numbers.

Once we've established this, we can adapt the sine function to all sorts of purposes (smooth size change, a boat rocking back and forth, a sprite floating up and down, etc.) by noticing that if we multiply the result of a block by a number (let's call it 'n'), the range of results we get is the range of the original block, multiplied by n.

This sounds like a good way to change a sprite's position from one side of the stage to the other. Let's create two scripts to do this, one to make a variable get bigger so that the sine block will change like we want it to, and another to move a sprite around.

Just so you know, sine is usually shortened to 'sin'.

when gf clicked
set [x v] to (0)
forever
change [x v] by (1)
end
when gf clicked
forever
set x to (([sin v] of (x)) * (240)) //this will make x go from -240 to 240 - the whole width of the stage!
end

Awesome! But it's kinda slow. We can make it go faster by changing the x variable by a bigger number, like 3.

when gf clicked
set [x v] to (0)
forever
change [x v] by (3)
end
when gf clicked
forever
set x to (([sin v] of (x)) * (240)) //this will make x go from -240 to 240 - the whole width of the stage!
end

Looks pretty good. But what if we want to restrict the sprite to the right side of the stage? Well, the x coordinates on the right half of the stage range from 0 to 240. Unfortunately, the sine function starts at a negative number. We can fix that by adding our multiplier to the result of the sine function, like this:

when gf clicked
forever
set x to ((([sin v] of (x)) * (240)) + (240))
end

Cool! But now our x value is going to go from 0 to 480, not 0 to 240 like we want. It's actually really easy to fix that - just divide our multiplier and adder by 2.

when gf clicked
forever
set x to ((([sin v] of (x)) * (120)) + (120))
end

With a little bit of math, you can set the range to whatever you need it to be.

when gf clicked
forever
set size to ((([sin v] of (x)) * (20)) + (40)) //after multiply, range is -20 to 20. Add 40 to that and the new range is 20 to 60.
end

Advanced

Can I animate multiple things but still use one variable?

It's actually pretty easy to do that. Just multiply x by some number (call it m) inside of the sine block, and sine will change m times as fast as it normally does.

([sin v] of ((x) * (2)))

Can I combine the sine-using script with the x-changing script?

Yep!

when gf clicked
set [x v] to (0)
forever
change [x v] by (1)
set x to (([sin v] of (x)) * (240))
end

What if x gets too big and crashes my project?

It turns out that the sine function repeats after x gets to 360, so we can prevent that by making x repeat after it gets to 360. I usually use the mod function for that.

when gf clicked
set [x v] to (0)
forever
change [x v] by (1)
set [x v] to ((x) mod (360)) //translated: make x loop after it gets to 360
end