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 > Spherical Coords
2004-11-19 20:27
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?
2004-11-20 10:29
Oswald

Registered: Apr 2002
Posts: 5020
Hi Cybernator !

what one-der does is:

it uses a rotation matrix. With a rot matrix you normally get your rotated coordinates by doing:

xcoord= xco*xxm+yco*yxm+zco*zxm

where: xco,yco,zco = original coords

xxm, yxm, zxm = the matrix elements to get the rotation of the x coord

so ycoord, and zcoord is calculated similarly just with different matrix elements.

(if you already know this, sorry for boring you:)

now the trick is with the matrix elements.

you precalc before the rotation xco*xxm, yco*yxm, zco*zxm, etc... BUT you dont use all possible coordinates, u only use coordinates like ...-32,-24,-16,-8,0,8,16,24,32... which can be done pretty FAST. if u precalc this stuff to t he zp, u get a code like in one der.

lda xco*xxm
adc yco*yxm
adc zco*zxm (sbc is used if the given coord was negative)
sta xcoord

voila! oh, and the coordinates are stored in a self modded way, guess you already figured that out :)


about the spherical stuff... I dont know... triple check your equations, they are probably incorrect.
2004-11-20 12:45
Cybernator

Registered: Jun 2002
Posts: 154
> *snip*
> (if you already know this, sorry for boring you:)

I've had enough time to figure out how these matrices work. :)

> you precalc before the rotation xco*xxm, yco*yxm,
> zco*zxm, etc...
> BUT you dont use all possible coordinates, u only use
> coordinates
> like ...-32,-24,-16,-8,0,8,16,24,32... which can be done
> pretty FAST.
> if u precalc this stuff to the zp, u get a code like in
> one der.

Neat! :) Though I guess this is gonna make troubles when importing .max
models.

> lda xco*xxm
> adc yco*yxm
> adc zco*zxm (sbc is used if the given coord was negative)
> sta xcoord

> voila! oh, and the coordinates are stored in a self
> modded way,
> guess you already figured that out :)

You mean they are hardwired in the speedcode, rite? Yes, that's the first thing I figured out.
This is no problem 'coz I'm not going for object transforms. And even if I was, again I see no problem. :)

> about the spherical stuff... I dont know... triple check
> your equations, they are probably incorrect.

The PC experiment simply uses: x=r*sin(theta)*cos(phi); y=r*sin(theta)*sin(phi); z=r*cos(theta)
I stole these, haven't tried to figure out how they work. :)

For cartesian to spherical, I use:
r=sqrt(x*x + y*y + z*z); theta=arccos(z/r); phi=arctg(y/x)
I'm sure the problem doesn't occur here. If it did, the object wouldn't distort _while rotating_.

Thanks a lot, Os!

Btw, is projection handled in Oneder (Wonder? :))? When rotating the object around the X axis (in usermode), I can't see any perspective transform.
2004-11-20 17:24
Oswald

Registered: Apr 2002
Posts: 5020
right one der doesnt do perspective transformations (just look at the code..) also another trick is obvious: it does not ORA the pixels...

the screenmode should be also noted, it uses 3 charsets, each layed out like:

0,1,2,3...31
32,33,34..63
64,65,66..95

obviously: one char row is exactly 256 bytes in memory, which makes calculations easy.
(2 table lookup and one ora table lookup to get the addy) look at the code.

the spherical stuff is still over my knowledge, but from my experiences the trouble is with the equations...

either you mixed up something when implementing the code for stuff like:

sin(theta)*cos(phi)=(sin(theta+phi)+sin(theta-phi))/2
sin(theta)*sin(phi)=(cos(theta-phi)-cos(theta+phi))/2

after having done several 3d rotators, I know: if ur object distorts when rotating about a specified axis, either the equations are wrong, or you mixed up a sine/axis in the code.
2004-11-20 20:32
Cybernator

Registered: Jun 2002
Posts: 154
> also another trick is obvious: it does not ORA
> the pixels...

Yeah, that bugged me. As far as I can see, there're no runtime modifications on the bitmask table. So I guess some pixels get cleared, but I really can't notice anything (except that if I stop the rotation, I can never see two pixels near each other with respect to the X axis).

> obviously: one char row is exactly 256 bytes in memory,
> which makes calculations easy.

That won't help me, I have to go for sprites.

> either you mixed up something when implementing the code
> for stuff like:
> sin(theta)*cos(phi)=(sin(theta+phi)+sin(theta-phi))/2
> sin(theta)*sin(phi)=(cos(theta-phi)-cos(theta+phi))/2

Nope, those were just some non-implemented ideas about how I'd handle the multiplication on C64. The PC version multiplies those the natural way. :)
2004-11-21 13:10
Optimus

Registered: Jan 2002
Posts: 122
I will make a guess since I had gone into exactly the same problem a long time ago when I first tried to code a 3d dots rotator on PC. Only one of the three rotation was working, the rest distorting the object in a strange way. However, my code was not using the spherical coords way but the very common equations for 3-axis rotations (I didn't used matrices then, but it must be the same in another way). I am not sure if it's the same with your code..

I handled the 3 rotations separately. And so my mistake was that I always tried to rotate from the base object, because that's how I wrote the equations in paper. At each frame I should 1) make X-rotation from the base object, 2) make Y-rotation from the X-rotated object, 3) make Z-rotation from the already X&Y rotated object. But now I am thinking it, I am not sure if it's the case for your code, because spherical coords are not dependent from base x,y,z but from 2 angles. Perhaps something on the conversions from cartesian to spherical and back?

I guess I have to try coding the spherical rotation myself and figure out if I go into problems. I was wondering lately why everyone uses the matrix rotations, since the spherical seems to have less calculations (If your objects are already stored in spherical coords). I guess it's because there are only rotations on 2 angles, not enough for games, but cool for an object rotator in demos and nobody would notice..
2004-11-21 17:57
Monte Carlos

Registered: Jun 2004
Posts: 351
Perhaps it's the precision.
Outside, where r is greater, you get a higher inaccuracy.
But this must not be a drawback, because the velocity increases simultaneously.
2004-11-22 11:32
Krill

Registered: Apr 2002
Posts: 2847
the code required to calculate the matrix can be optimised to take 9 or 10 raster lines, it all involves only a fixed amount of adds and shifts, using those sine addition theorems. (you'll need the formulas for two sin/cos terms and those for three sin/cos terms.)

for +h2k, i made the objects in lightwave 3d on the pc and then wrote a little pascal tool to convert the object file containing the points coords (in 4-byte real numbers) to c64 ints. that was fairly easy, i dont remember any problems occuring because of the steps oswald mentioned. actually the objects have coords of say -15 to 15, in steps of 1. the scaling is simply done by a sine with a bigger amplitude.

and i dont recognize the missing ora by just looking at the normal effect (without user mode), so why do all coders get so upset about that? ;)
2004-11-22 14:04
Oswald

Registered: Apr 2002
Posts: 5020
krill: coz the missing ora feels somehow unfair/cheating :)
2004-11-22 15:51
JackAsser

Registered: Jun 2002
Posts: 1989
If the missing ORA stills looks the same as with it then it's just lame to keep it. i.e. non-optimaized. Demo coding is ALL ABOUT cheating. Not cheating and having abdundant instructions is just lame. :D
2004-11-23 08:44
Optimus

Registered: Jan 2002
Posts: 122
>ORA in Oneder

Funny. I didn't noticed that before. Not only that but yesterday while I was coding sine dots (on the CPC) I had mistakenly forgotten to OR the pixels, but it took myself a while to notice that :)
 
... 27 posts hidden. Click here to view all posts....
 
Previous - 1 | 2 | 3 | 4 - 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
Guests online: 112
Top Demos
1 Next Level  (9.8)
2 Mojo  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Comaland 100%  (9.6)
6 No Bounds  (9.6)
7 Uncensored  (9.6)
8 Wonderland XIV  (9.6)
9 Memento Mori  (9.6)
10 Bromance  (9.5)
Top onefile Demos
1 It's More Fun to Com..  (9.7)
2 Party Elk 2  (9.7)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.5)
5 TRSAC, Gabber & Pebe..  (9.5)
6 Rainbow Connection  (9.5)
7 Wafer Demo  (9.5)
8 Dawnfall V1.1  (9.5)
9 Quadrants  (9.5)
10 Daah, Those Acid Pil..  (9.5)
Top Groups
1 Nostalgia  (9.3)
2 Oxyron  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 Crest  (9.3)
Top Original Suppliers
1 Black Beard  (9.7)
2 Derbyshire Ram  (9.5)
3 hedning  (9.2)
4 Baracuda  (9.1)
5 Jazzcat  (8.6)

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