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 > calculating of square roots ?
2006-06-29 00:59
Trifox
Account closed

Registered: Mar 2006
Posts: 108
calculating of square roots ?

hi all, for my newest project i am in urgent need to calculate the length of a 2d vector, reminding pythagorian math i remember that i have to calculate the roots of a fixed point (8bits.8bits) number, how can that be mastered in a convenient way ?!?!?!

thx
2006-06-29 06:19
A Life in Hell
Account closed

Registered: May 2002
Posts: 204
Quote: hi all, for my newest project i am in urgent need to calculate the length of a 2d vector, reminding pythagorian math i remember that i have to calculate the roots of a fixed point (8bits.8bits) number, how can that be mastered in a convenient way ?!?!?!

thx


The answer is, as always, a lookup table :)
2006-06-29 07:58
Compyx

Registered: Jan 2005
Posts: 631
Quote: The answer is, as always, a lookup table :)

And some logic to go with that, as a lookup-table for 16-bit fixed point numbers would be 65536 bytes, which looks an awful lot like the total memory available on a C64 :)

Look at this page: http://en.wikipedia.org/wiki/Methods_of_computing_square_roots for some information on how to approximate the square root of an arbitrary number. Some parts of the equations can be implemented as lookup-tables, other parts you'll have to implement as code.

I've done these things in the past, don't remember how I did them though, as it was a long time ago I did any 'serious' mathematics in assembly.



2006-06-29 08:25
WVL

Registered: Mar 2002
Posts: 902
Actually I do somewhat the same in pinball dreams to calculate the total speed of the ball : v=sqrt(vx^2+vy^2), now because i wanted a very high resolution, i couldnt use a lookup table.

Good thing for me was that i knew the angle vx and vy made, so i used that as a basis for my calculation (I also have a very convenient and easy way to calculate this angle!).

then you have the following equations that hold :

sin(angle)=vy/v and cos(angle)=vx/v

or, written out for v :

v=vy/sin(angle) or v=vx/cos(angle)

now, sin and cos are interesting, because they're max 1. So i can rewrite this like

v=vy*(1/sin(angle)=vy*((1/(sin(angle))-1)+vy

now, if i make use of only 45' where sin is between sqrt(2)/2 (say .5) and 1 , this means that 1/sin(angle) is between 1 and 2 and (1/sin(angle))-1 is between 0 and 1!!

So i simply have a 1 byte lookup table (0bits.8bits) with the value of (1/sin(angle))-1 and do the calculation using a simple multiplication! :) My table only has to be 32 bytes long (256 makes a complete circle, 64 makes one quarter and i only need half of that).

(you have to use 1/cos(angle)-1 if you're in the other 45' ofcourse, but you can use the same table).

I hope this helps :)

Anyway, if you need to know this for 3d vectors, I'm guessing you're approaching it from the wrong direction and you seriously need to investigate doing the calculations using matrices!
2006-06-29 08:35
JackAsser

Registered: Jun 2002
Posts: 2014
@trifox: may I ask why you need to know the length of the vector, perhaps there are otherways. The best optimization is to not do it at all. :D
2006-06-29 08:53
Graham
Account closed

Registered: Dec 2002
Posts: 990
There is a binary method of calculating a square root, if you totally optimize it towards 6502 asm it is almost as fast as a binary divide routine.
2006-06-29 09:01
Trifox
Account closed

Registered: Mar 2006
Posts: 108
thank you, i need i for some kind of billard simulator, and the reason why i want it to is simply to tread collisions of balls correctly ... hmm, if i ever get this done ?!??! i just got cc65 ready to handle some parts of the maths easily, especcially cross product and scalar produkt calculations is too heavy math for me on doing it directly in asm ...
2006-06-29 12:05
enthusi

Registered: May 2004
Posts: 677
Since you work in C anyway...
Maybe this one?
http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/sqrt.ht..
For 6502 it might be rather slow though but you can chose an accuracy rather simple...
This method can also be quickened quite significantly by using tables (oh, surprise).
2006-06-29 14:31
PopMilo

Registered: Mar 2004
Posts: 146
Here is one way to do it from

http://www.geocities.com/oneelkruns/asm1step.html

Returns the 8-bit square root in $20 of the
16-bit number in $20 (low) and $21 (high). The
remainder is in location $21.

sqrt16 LDY #$01 ; lsby of first odd number = 1
STY $22
DEY
STY $23 ; msby of first odd number (sqrt = 0)
again SEC
LDA $20 ; save remainder in X register
TAX ; subtract odd lo from integer lo
SBC $22
STA $20
LDA $21 ; subtract odd hi from integer hi
SBC $23
STA $21 ; is subtract result negative?
BCC nomore ; no. increment square root
INY
LDA $22 ; calculate next odd number
ADC #$01
STA $22
BCC again
INC $23
JMP again
nomore STY $20 ; all done, store square root
STX $21 ; and remainder
RTS
2006-06-29 15:18
Trifox
Account closed

Registered: Mar 2006
Posts: 108
oh, i found a interresintg routine here:
http://zeus.osix.net/modules/article/?id=770

which should do exactly what i want ... ;)
2006-06-30 04:31
Honesty

Registered: Jan 2003
Posts: 121
There had also been an article in 64ér magazin about this... When still in need i will look up it...

 
... 92 posts hidden. Click here to view all posts....
 
Previous - 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 - 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
Benson/Tristar & Red..
The Syndrom/TIA/Pret..
Unlock/Padua/Albion
Durandal
MaD ][/Starship
Andy/AEG
Malmix/Fatzone
Worluk
kbs/Pht/Lxt
Guests online: 115
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 The Demo Coder  (9.6)
6 Edge of Disgrace  (9.6)
7 What Is The Matrix 2  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 No Listen  (9.7)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 X-Mas Demo 2024  (9.5)
7 Dawnfall V1.1  (9.5)
8 Rainbow Connection  (9.5)
9 Onscreen 5k  (9.5)
10 Morph  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Censor Design  (9.3)
5 Triad  (9.3)
Top Fullscreen Graphicians
1 Joe  (9.7)
2 Sulevi  (9.6)
3 The Sarge  (9.6)
4 Veto  (9.6)
5 Facet  (9.6)

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