Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > sin/cos table generator
2012-11-17 18:59
Rudi
Account closed

Registered: May 2010
Posts: 125
sin/cos table generator

this is my sine table generator:
for(;x<256;){a+=b;b+=c;*s++=0xf-((a+t)>>3);x++==127?c=-c:0;}
not implemented on c64 yet.

it is harmonic oscillator based on simple physics.
a and b are initial seeds.

this one will work on 8-bit with a little offset-tweaking,
between the region[0..59] where x+=95 and y+=22.
the drawback is its amplitude will be something like -16 -> 16.
tweaking might be required. or an additional byte for bigger values.

i found some interesting areas while running through all possible states for b and c regions (total 65536 states) but none of them where perfect.

the table (after offsetting) looks something like this:
9,11,13,15,16,18,19,21,22,23,24,25,26,26,27,27,28,28,28,28,27,27,26,26,25,24,23, 22,21,19,18,16,15,13,11,9,8,6,5,4,3,2,1,1,0,0,0,0,0,0,0,0,1,1,2,3,4,5,6

i want to fit the code as tiny as possible
the code would probably more or less look something like this;

in loop:
adc b
pha
lda b
adc c
pha
lda #$f
sbc (a+t) ror,3
inx
cpx #127
beq chg_sign
sta [table]

of course this would not be usable for a demo.
but intros in 256b size or less, i hope it might bring some ideas if anyone is interested.

im not sure if my code is very useable but just too give you an idea. if anyone know any other/better tricks please let me know. im kind of tired, i wish there could be another byte to increase the sine-amplitude resolution.
 
... 3 posts hidden. Click here to view all posts....
 
2012-11-26 17:51
Skate

Registered: Jul 2003
Posts: 494
Most of you already know that but i'd like to remind again. When it comes to 256 bytes, there is no "best/shortest" sine table generator routine. It depends on the effect and the "other parts" of the code. I usually merge some other stuff with sine calculation. So, when you check sine calculation block, it can be over 40 bytes but doing more than just sine calculation. Of course base method is important but even if your routine is under 30 bytes, sometimes some other 40+ bytes routine might result a smaller sized effect. That happened to me at least twice. So, don't forget to check all known methods with your 256b effect or it may never reach 256b limit. ;)
2012-11-26 18:09
Burglar

Registered: Dec 2004
Posts: 1101
for a pseudo sine, check You Can't Process This REM Statement with a Normal Brain by cruzer
2012-11-30 10:31
Perplex

Registered: Feb 2009
Posts: 255
Quoting Burglar
for a pseudo sine, check You Can't Process This REM Statement with a Normal Brain by cruzer


Indeed, a parabola is pretty close to a sine curve, and suitable for many demo effects.

http://6581.no/sine_vs_parabola.png
2012-11-30 11:22
Oswald

Registered: Apr 2002
Posts: 5094
well, nobody cares if its a sine or just some nice curve anyway :) except.. I'm wondering how a sine table made of parabolas would affect a 3d rotation. :)
2012-11-30 11:49
enthusi

Registered: May 2004
Posts: 677
Your vectors would just wobble and vary in length between rotation steps.
2012-11-30 11:52
chatGPZ

Registered: Dec 2001
Posts: 11386
so it wouldnt be a big difference for most routines =)
2012-11-30 15:13
Perplex

Registered: Feb 2009
Posts: 255
Quoting Oswald
well, nobody cares if its a sine or just some nice curve anyway :) except.. I'm wondering how a sine table made of parabolas would affect a 3d rotation. :)


http://6581.no/plotball-sine.html

http://6581.no/plotball-parabola.html

Conclusion: don't use parabolas for rotations.
2012-12-01 07:37
Graham
Account closed

Registered: Dec 2002
Posts: 990
Using parabolas as sines will look strange on other applications aswell. The first derivation of a "parabola-sine" would be a triangle wave which will definitely look less smooth than a real sine.
2012-12-01 12:01
Karoshier
Account closed

Registered: May 2010
Posts: 15
Perplex: oh well, the rotation with the parabola approximation you pointed out might be not as good as the one with the sine, but honestly I like the twist it adds to it :P
2012-12-16 17:49
Internium
Account closed

Registered: Feb 2012
Posts: 3
This could be also a good method to approximate a sine: http://devmaster.net/forums/topic/4648-fast-and-accurate-sineco.. They use the Maclaurin series.
Previous - 1 | 2 - Next
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
DJ Gruby/TRiAD
Sulevi/Virtual Dreams
chancer
Guests online: 144
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 Edge of Disgrace  (9.6)
6 What Is The Matrix 2  (9.6)
7 The Demo Coder  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 No Listen  (9.6)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 Dawnfall V1.1  (9.5)
7 Rainbow Connection  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Triad  (9.3)
5 Censor Design  (9.3)
Top Swappers
1 Derbyshire Ram  (10)
2 Jerry  (9.8)
3 Violator  (9.7)
4 Acidchild  (9.7)
5 Cash  (9.6)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.048 sec.