Page 1 of 1

3d world float to screen float

Posted: Fri Apr 17, 2009 4:47 pm
by LionX
can someone point me to a formula to quickly convert a float decimal vlaue to the screen's 4-bit decimal value. it should make things move smoother onscreen. i guess its kind of complex, because it depends how precise the decimal is.




float

to

GS_XYZ->x

Posted: Fri Apr 17, 2009 5:58 pm
by LionX
ok, lets work it out.

4bits holds 16 digits: 0-15

the GS says '15' = '.9375'

so '0001' = .9375 / 15

so '0001' = '.625'




now let do the float:

we divide 1.0f into 15 parts

so 1.0 / 15 = 0.066666666666666666666666666666667

lets just say 0.066




so when the worldscreen float have a decimal of '.066' then the screen decimal should have a value of '0001'(.625)




i think we are ready to convert it


1st we extract decimal number from our worldscreen flaot like this:

float f = 58.8686464...;


int intpart = (int) f;
float decpart = f - intpart;


then calculate our final screen 4 bit decimal like this:


int screen_xy_4bit_decimal = (int) (decpart / 0.066)


i thnk that should do it.
.


anyone think this doesnt work ?

Posted: Sat Apr 18, 2009 2:43 am
by Lukasz
There is a much eaiser way to do this.

Code: Select all

#define FTOI4(x) ((int)((x)*16.0f))
From here

The reason this works is because you want to keep 4 bits of the fraction portion of your float. Shifting 4 bits left is equal to multiplying by 16. So by multiplying your float with 16, you get 4 more bits in your int (when you cast it), which equal the fraction part.

On the VU1 you can use the FTOI4 instruction to do this for you.

Posted: Sat Apr 18, 2009 3:29 am
by LionX
nice and simple
:)


you are right:

if i have a float value of 123.45

then: 123.45 * 16.0 = 1975 (bin:11110110111)


if you cut off the last 4 bit you get : '1111011'

which brings us back to the whole number of '123'





and if you check the last 4 bits that we cut off is == 7 witch is about 0.45


THIS IS TOO COOL
.