PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Converting code to fixed point

Hi.

I'm really struggling getting this code converted from floating point to fixed point, I don't know why. Hopefully somebody here can spot what I'm doing wrong.

Original code:
Code:

float angle,x,y,lx,ly,vx,vy,ax,ay,dx,dy,dr,dt;
int power;

...

m_planets[i].size = randint(20)+5;
m_planets[i].GM = 0.1f * PI * (m_planets[i].size * m_planets[i].size);

...

angle=m_player1.angle*PI/180;
power=m_player1.power;

x=m_player1.x;
y=m_player1.y;

vx = power*cos(angle);
vy = -power*sin(angle);

dt = 0.1f;

lx=x;
ly=y;

x += vx*dt;
y += vy*dt;

//calculate acceleration
ax = 0.0f;
ay = 0.0f;

for (i=0;i<10;i++)
{
if(m_planets[i].size>0)
{
dx = x-m_planets[i].x;
dy = y-m_planets[i].y;

dr = sqrt((dx*dx)+(dy*dy));

ax -= m_planets[i].GM*dx/(dr*dr*dr);
ay -= m_planets[i].GM*dy/(dr*dr*dr);
}
}
vx += ax*dt;
vy += ay*dt;

m_raster.myline((int)lx,(int)ly,(int)x,(int)y,rgb(255,255,255),(m_background->GetPitch()>>1),m_background->GetPixels());



And what I've got so far:
Code:

// Fixed point defines
#define FPP 8
#define FP_ONE (1<<FPP)
#define FPMUL(x,y) (((x)*(y))>>FPP)
#define FPDIV(x,y) (((x)<<FPP)/(y))
#define FP(x) ((int)((x)*FP_ONE+0.5f))
#define IFP(x) ((x)<<FPP)

...

long angle,x,y,lx,ly,vx,vy,ax,ay,dx,dy,dr,dt;
int power;

...

m_planets[i].size = randint(20)+5;
m_planets[i].GM = FPMUL(FPMUL(FP(0.1f),FP(PI)),IFP((m_planets[i].size * m_planets[i].size)));

...

angle=FP(m_player1.angle*PI/180);
power=IFP(m_player1.power);

x=IFP(m_player1.x);
y=IFP(m_player1.y);

vx = FPMUL(power,FP(cos(angle)));
vy = FPMUL(-power,FP(sin(angle)));

dt = FP(0.1f);

lx=x;
ly=y;

x += FPMUL(vx,dt);
y += FPMUL(vy,dt);

//calculate acceleration
ax = 0;
ay = 0;

for (i=0;i<10;i++)
{
if(m_planets[i].size>0)
{
dx = abs(x-IFP(m_planets[i].x));
dy = abs(y-IFP(m_planets[i].y));

long t = FPMUL(dx,dx)+FPMUL(dy,dy);
float t1=sqrt(t>>FPP);
dr = FP(t1);

if(dr>0)
{
ax -= FPDIV(FPMUL(m_planets[i].GM,dx),FPMUL(dr,FPMUL(dr,dr)));
ay -= FPDIV(FPMUL(m_planets[i].GM,dy),FPMUL(dr,FPMUL(dr,dr)));
}
}
}
vx += FPMUL(ax,dt);
vy += FPMUL(ay,dt);

m_raster.myline(lx>>FPP,ly>>FPP,x>>FPP,y>>FPP,rgb(255,255,255),(m_background->GetPitch()>>1),m_background->GetPixels());



I had to add the float t1 in just so I could step through a debug and watch the values, because it seemed I was getting overruns using ints instead of longs. And I had to add the check for dr being 0 before the division, even though I believe the only way dr can be zero is because the calculation of dr is not right.

I haven't had enough coffee yet to figure out whether the value I get out of sqrt needs to be shifted or not.

Anybody got any ideas? :)

Thanks,

Pam
What, did I accidently click on the "hide post forever" button? :wink:

I've tried a number of things, including verifying the order of operations in the math.

I still can't get it to work right.

Pam
I haven't had time to go through and find the problem. However I have a suggestion.

If you multiply an int against a fixed point value then the result is a fixed point value (32.0 * 24.8 = 24.8 ). You are converting "power" to fixed point to multiply it against a fixed point value, so you have to shift back after the fixed point mult. Just multiply power as a regular int against what you want to scale, and you don't have to right shift afterward, and you will have an extra 8 bits of range to work with.

The primary problem you will run into with fixed point math is overflow. I'll let you know if I see anything else.

Dan East
cos and sin are floating point routines that accept radians. You are converting radians to fixed point and passing that to sin and cos, so those will return off the wall results. You need to create your own fixed point LUT for sin / cos. Instead of passing radians pass something like 0 to 2047, where 2048 is equivalent to 2*pi.

Dan East
Thanks Dan. You are, of course, right as usual.

Pam
Dan East wrote:
If you multiply an int against a fixed point value then the result is a fixed point value (32.0 * 24.8 = 24.8 ). You are converting "power" to fixed point to multiply it against a fixed point value, so you have to shift back after the fixed point mult. Just multiply power as a regular int against what you want to scale, and you don't have to right shift afterward, and you will have an extra 8 bits of range to work with.


I was assuming power is an int. However after I thought about it power may be a float with a range something like 0.0 to 1.0. If so then you would have to convert it to fixed point as you are doing.

Dan East