Page 1 of 1

Routines for sine and parabolic motion?

Posted: Thu Dec 29, 2016 4:08 pm
by imamelia
I've been trying to figure out formulas for getting a sprite to move in a sine wave or a parabola. Getting it close isn't hard, but the problem is making it customizable and precise. Specifically, I want to be able to set the exact parameters: the height and period of the sine wave and the height, width, and direction of the parabola. It would be easier if I could hardcode the parameters, but I don't want them to be hardcoded; I want them to be options for input, which would allow different values to be used for different sprites, or even different parts of the same sprite. I'd imagine lookup tables might help, but...surely such a table would have to be very large to encompass all possible values?

Re: Routines for sine and parabolic motion?

Posted: Thu Dec 29, 2016 4:47 pm
by psycopathicteen
I use this LUT for anything using sine and cosine.

Code: Select all

sine:
dw 0,6,13,19,25,31,38,44,50,56,62,68,74,80,86,92,98,104,109,115,121,126,132,137,142
dw 147,152,157,162,167,172,177,181,185,190,194,198,202,206,209,213,216,220,223,226,229
dw 231,234,237,239,241,243,245,247,248,250,251,252,253,254,255,255,256,256

cosine:
dw 256,256,256
dw 255,255,254,253,252,251,250,248,247,245,243,241,239,237,234,231,229,226,223,220,216
dw 213,209,206,202,198,194,190,185,181,177,172,167,162,157,152,147,142,137,132,126,121
dw 115,109,104,98,92,86,80,74,68,62,56,50,44,38,31,25,19,13,6,0,-6,-13,-19,-25,-31,-38
dw -44,-50,-56,-62,-68,-74,-80,-86,-92,-98,-104,-109,-115,-121,-126,-132,-137,-142
dw -147,-152,-157,-162,-167,-172,-177,-181,-185,-190,-194,-198,-202,-206,-209,-213,-216
dw -220,-223,-226,-229,-231,-234,-237,-239,-241,-243,-245,-247,-248,-250,-251,-252,-253
dw -254,-255,-255,-256,-256,-256,-256,-256,-255,-255,-254,-253,-252,-251,-250,-248
dw -247,-245,-243,-241,-239,-237,-234,-231,-229,-226,-223,-220,-216,-213,-209,-206,-202
dw -198,-194,-190,-185,-181,-177,-172,-167,-162,-157,-152,-147,-142,-137,-132,-126,-121
dw -115,-109,-104,-98,-92,-86,-80,-74,-68,-62,-56,-50,-44,-38,-31,-25,-19,-13,-6
dw 0,6,13,19,25,31,38,44,50,56,62,68,74,80,86,92,98,104,109,115,121,126,132,137,142
dw 147,152,157,162,167,172,177,181,185,190,194,198,202,206,209,213,216,220,223,226,229
dw 231,234,237,239,241,243,245,247,248,250,251,252,253,254,255,255,256,256

Re: Routines for sine and parabolic motion?

Posted: Thu Dec 29, 2016 4:55 pm
by adam_smasher
Use a table to get the hard to compute value, but do the rest of the math on the CPU.

Supposing you have a procedure Sine to get the sine value for an angle in A (either with a table or with Real Math), you can e.g. double the amplitude easily:

Code: Select all

LDA Angle
JSR Sine
ASL
If you want non-power of two multipliers, you can use the SNES' built-in multiplier or implement your own software multiplier.

And if you generate the table for the smallest amplitude and longest period you want, you never have to divide.

Re: Routines for sine and parabolic motion?

Posted: Thu Dec 29, 2016 5:13 pm
by ccovell
You'll need one rather large sine table. If you want different frequencies of the sinewave, I guess you'll just have to sample from it, skipping a variable # of values.

For scaling a sine wave's amplitude, you can do addition of inverse sines. You'll need 2 pointers into the same sine table. If you add the values of the table at (x) and at (pi+x), you'll get zero amplitude, for example.

Anyone got ideas for parabolas?

Re: Routines for sine and parabolic motion?

Posted: Fri Dec 30, 2016 9:25 am
by tepples
To implement parabolic motion, such as gravity, keep displacement and velocity variables for each object. Then add a constant to the velocity each frame.

Once you have this in place, you can make sinusoidal motion without tables: subtract a fraction of displacement from center from the velocity each frame. (This is a second-order differential equation whose solution is a sinusoid.) The frequency depends on this fraction.