It is currently Thu Oct 19, 2017 10:21 pm

All times are UTC - 7 hours



Forum rules


Related:



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Thu Dec 29, 2016 4:08 pm 
Offline

Joined: Wed Sep 01, 2010 12:17 am
Posts: 7
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?


Top
 Profile  
 
PostPosted: Thu Dec 29, 2016 4:47 pm 
Offline

Joined: Wed May 19, 2010 6:12 pm
Posts: 2287
I use this LUT for anything using sine and cosine.

Code:
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


Top
 Profile  
 
PostPosted: Thu Dec 29, 2016 4:55 pm 
Offline

Joined: Sun Mar 27, 2011 10:49 am
Posts: 192
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:
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.


Top
 Profile  
 
PostPosted: Thu Dec 29, 2016 5:13 pm 
Offline

Joined: Sun Mar 19, 2006 9:44 pm
Posts: 915
Location: Japan
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?

_________________
http://www.chrismcovell.com


Top
 Profile  
 
PostPosted: Fri Dec 30, 2016 9:25 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19104
Location: NE Indiana, USA (NTSC)
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group