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:
And what I've got so far:
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
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