| | Cybernator
Registered: Jun 2002 Posts: 154 |
Spherical Coords
I was doing 3D plots like those in Oneder, +H2K, Late Ejaculation, etc.. It all works a-ok, except that it's pretty slow. :) Took the liberty to disassemble the above mentioned demos and I ended up nowhere. The speedcode relies on some values stored on zeropage, but I can't realize what these values are. What follows is part of the speedcode from Oneder which handles one single plot.
The X reg is either 00 or 80 (which points to the bank I think), which doesn't really matter.
,8000 a5 7a LDA $7a
,8002 e5 94 SBC $94
,8004 e5 ac SBC $ac
,8006 a8 TAY
,8007 b1 e4 LDA ($e4),Y
,8009 85 e1 STA $e1 ; absolute ypos on screen (?)
,800b 9d 02 a4 STA $a402,X ; modifies a separate speedcode to clear the plot later
,800e a5 17 LDA $17
,8010 e5 31 SBC $31
,8012 e5 49 SBC $49
,8014 8d 21 80 STA $8021
,8017 29 f8 AND #$f8
,8019 19 00 41 ORA $4100,Y
,801c 9d 01 a4 STA $a401,X ; modifies a separate speedcode to clear the plot later
,801f a8 TAY ; absolute xpos on screen (?)
,8020 ad 71 43 LDA $4371 ; bitmask ($80, $40, $20, $10, 8, 4, 2, 1)
,8023 91 e0 STA ($e0),Y ; hit it! :)
Now what the hell are eg. the first three instructions?
--
I wonder if this is handled with spherical coordinates. Conversion from spherical to cartesian should be fairly simple to implement: x=r*sin(theta)*cos(phi); y=r*sin(theta)*sin(phi); z=r*cos(theta)
Now the cool stuff about the multiplication of the trig. functions:
sin(theta)*cos(phi)=(sin(theta+phi)+sin(theta-phi))/2
sin(theta)*sin(phi)=(cos(theta-phi)-cos(theta+phi))/2
Which is simply a matter of ADC/SBC. As for the multiplication of the radius, I guess several tables would do the job. I still haven't thought of a fast way to handle the projection, though.
Now, all this is cool, but I've got some trouble. I made a PC proggy to test these conversions. It loads a file containing vertices in cartesian coord. system. Then converts them to spherical, which means that the rotation is simple INC/DEC on the angles. These rotated angles are converted back to cartesian and plotted on screen. But something is wrong: incrementing of one of the angles works a-ok, but if I increment the other one, the object distorts. This shouldn't happen, rite? I mean, it's logical. Or maybe there's another way to handle this rotation (still hadn't reached this topic at university :)). Afterall, perhaps there's some bug in the proggy, but I can't find out what it is.
In case you wanna see the proggy, here it is: http://www.geocities.com/lazeristoski/polar.zip
Arrows handle the angles. A/Z handles the radius, but that is not important.
Is it a good idea to handle this with spherical coordinates?
|
|
... 27 posts hidden. Click here to view all posts.... |
| | Cybernator
Registered: Jun 2002 Posts: 154 |
Hugi's such a great piece. Thanks for the link Optimus! Btw, was the sinus generator by Baze using Taylor's stuff?
Ben wrote:
> Can you describe the type of distortion
> you experience?
Erm... Hardly! :) If possible, take a look at the distortion yourself. Uhm, something crossed my mind.. If I wanted to make such a distortion on purpose, I wouldn't have been able to. :P
> However, are you sure your projection
> functionality properly handles sign/phase
> changes?
There's no projection implemented. The X and Y coords of the rotated object are directly used, Z is thrown out. According to the book from where I've taken the formulas, Y axis goes right, Z goes up, and X comes out. So practically I mix them, 'coz on monitor X goes right and Y goes down, but I don't think that can cause a distortion. It must be a very stupid bug. I'll write the thing from scratch. In fact, as soon as Oswald said that Oneder wasn't using spherical coords, I've just thrown the idea. :)
All these maths and floats (thanx for the PDFs, White Flame) freak me out sometimes. I'll just have to dive in the PC-scene as well. Lotsa stuff can be learnt from there. (now I deserve an asskick :P)
Btw, the fire-ish effect in "Halfway There" (the very first one) by Dekadence.. Does it have a formal name? I'd like to google for some info.
|
| | Optimus
Registered: Jan 2002 Posts: 122 |
>optimus, on c64 sines are not generated runtime, but are precalculated.
Do you mean that this is how the Kernel ROM sin routine does it? (Else, of course I am aware that democoders are using precalculated sines and don't generate them in realtime, the link was just interesting for people who want to pregenerate them in 256b intros or so) |
| | Optimus
Registered: Jan 2002 Posts: 122 |
>Btw, was the sinus generator by Baze using Taylor's stuff?
Baze's words: "well I'm not quite sure but that algorithm simulates a harmonic oscillator"
I had his emails with further explanations which I can't find for the moment, I think we talked about this and I had sent them either to you (or another guy?) a long time ago. Last time, I couldn't understand how it works (and I was supposed to be the math student ;), perhaps in the future I will. There are lessons in my uni I keep loosing, having a lot of connections with maths from demomaking I read around here.. ;P |
| | Oswald
Registered: Apr 2002 Posts: 5094 |
Optimus: I meant that demos use precalced sine tabs, iirc the kernal uses same tables but not much... but really no idea how it calx the sine... this also a way to precalc sinetabs, simply use the kernals floating point accumulator and its sine routine.. :) |
| | WVL
Registered: Mar 2002 Posts: 902 |
emulating an oscillator would be pretty easy.
the math :
let's say you've got a pendulum moving around, you can write the forces acting on the pendulum like :
F = -k*x (x being the offset from the center, k being the spring constant).
the equation of motion becomes :
d2 x(t) -k*x
------- = ------
dt2 M
the solution to this is that x ~ some sine
an emulation would be :
have the variables x,v,a,t
set x to -1, v&t to 0,a to whatever
loop :
1 store the value of x in the sine at position t
1b t=t+1
2 a=-k*x
3 v=v+a/M*dt (make dt=1 ;)
4 x=x+v
5 check if x is -1 again ,in which case the sine ends and the loop stops..
(a better check would be that x< -0.99 and speed is negative or something)
that's it..
btw, the amplitude of the sine is set by choosing the correct starting value for x at t=0.
the frequency of the sine is set by (sqrt(k/m)) (iirc) ;)
|
| | Graham Account closed
Registered: Dec 2002 Posts: 990 |
some amiga and pc coders used oscilators for their sine calculation. i remember sanity had one in their amige demo-os too. but you need lots of bits and on c64 its propably smaller to just store a quater of the sine and mirror the rest. |
| | WVL
Registered: Mar 2002 Posts: 902 |
it's true that you need lotsa bits. but in return you only have to do add's. if you want a large difference in the sines you need to calc, oscillators are pretty good IMO.
ofcourse my favorite way is to add 2 sines to get new sines with different amplitudes (amplitude ranging from 0x to 2x the original sine). |
| | Cybernator
Registered: Jun 2002 Posts: 154 |
> I think we talked about this and I had sent
> them either to you (or another guy?)
Yes, it was me. :) I have that somewhere on the HD, or maybe I've burnt it on CD.
> Last time, I couldn't understand how it works
> (and I was supposed to be the math student ;)
<weed_mode :P>There's one nasty thing about these sciences, you can't just sit and learn. It takes practise, and it's a pain to practise something you can't see a result of. Solving numbers is just so boring, unless you can use that in your demo (the result I'm talking about :)). It took me some time to understand how the rotation formulas are derived, after which I'm not coding this stuff blindly, or at least I feel so. It seems so simple now.. Or does it? One needs to know some of the fundamental trig identities in order to derive. I have no idea how these identities are derived. I just used them blindly. At uni I'm having quite some troubles with physics, electrotechincs (or whatever it's called in English), and with math as well. I'm having enough motivation to stop my LDAs and STAs for a while, and spend more time on linear algebra. Although I don't get much of WVL's explanation (even if it's veeery familiar :)), he gave me enough motivation to spend more time on physics as well. Hmm... Anyone's got any idea why should I learn electrotechincs? :P</weed_mode>
If I understood the article in Hugi, functions like sin, cos, ln, on a computer are actually calculated by some approximations. Quite interesting. In fact, this discussion turned pretty interesting. :) Another thing which I'm really curious about: how does one calculate the value of PI? All I know is that it's the ratio of the circumference of a circle and its diameter. Say the diameter is 1, the circumference would then equal PI. But how do I calculate PI? (they've stopped, let us continue :)) |
| | WVL
Registered: Mar 2002 Posts: 902 |
calculating PI is a really tedious job. The best way might be to use some kind of series, derived from an integration in the imaginary/real plane around a point where the value of the function you want to integrate becomes infinite.
(this is real hard to explain, so i won't)
you get something like
PI/4 = 1 - 1/3 + 1/5 = 1/7 + 1/9 - etc..
but this converges very slowly -> you have to do a lot of calculations to get a reasonable accuracy!
just imagine adding 1/1001, which you do after 500 steps -> it still changes the calculated value in the 3rd decimal. So you need WAY MORE than 1000 steps, to get 3 decimals correctly!!
ofcourse there are faster ways, but in general, forget about it and just use PI=3.1415926536 ;)
|
| | Graham Account closed
Registered: Dec 2002 Posts: 990 |
there's countless ways to calculate pi. most formulas used to calculation millions of digits from pi involved some fast converging arctan formula. but in 1995 a completely new way and much more efficient way to calculate pi was found: the Bailey-Borwein-Plouffe algorithm. with that algorithm you can calculate the n-th hexadecimal digit of pi without calculating the previous ones. very interesting, especially because you can find similar formulas for other numbers like log(2) or sqrt(2).
http://mathworld.wolfram.com/bimg673.gif |
Previous - 1 | 2 | 3 | 4 - Next | |