/* fixatan2: * Like the libc atan2, but for fixed point numbers. */ fixed fixatan2(fixed y, fixed x) { fixed r; if (x==0) { if (y==0) { *allegro_errno = EDOM; return 0L; } else return ((y < 0) ? -0x00400000L : 0x00400000L); } *allegro_errno = 0; r = fixdiv(y, x); if (*allegro_errno) { *allegro_errno = 0; return ((y < 0) ? -0x00400000L : 0x00400000L); } r = fixatan(r); if (x >= 0) return r; if (y >= 0) return 0x00800000L + r; return r - 0x00800000L; }
void evaluatePhysics() { if (theShip.x > MAPWIDTH) { theShip.x = MAPWIDTH; theShip.velX *= -1; } if (theShip.x < 0) { theShip.x = 0; theShip.velX *= -1; } if (theShip.y > 768) { theShip.y = 768; theShip.velY *= -1; } if (theShip.y < 0) { theShip.y = 0; theShip.velY *= -1; } theShip.rotation += theShip.velRotation; theShip.velX += fixtof(fixsin(ftofix(theShip.rotation))) * (theShip.rocketPower/theShip.mass) * 0.99; theShip.velY -= fixtof(fixcos(ftofix(theShip.rotation))) * (theShip.rocketPower/theShip.mass) * 0.99; theShip.x += theShip.velX; theShip.y += theShip.velY; OBJECT *theRock = rockHeader; static float distance; static float diffX; static float diffY; static float fG; static float fGX; static float fGY; static float rockX; static float rockY; static float shipX; static float shipY; shipX = theShip.x + (theShip.width / 2); shipY = theShip.y + (theShip.height / 2); while (theRock != NULL) { rockX = theRock->x + (theRock->width) / 2; rockY = theRock->y + (theRock->height) / 2; diffX = (shipX - rockX); diffY = (shipY - rockY); distance = (pow(pow(diffX, 2)+ pow(diffY, 2), 0.5)); fG = 25 * theShip.mass * theRock->mass / pow(distance, 2); fGX = fixtof(fixcos(fixatan(ftofix(diffY/diffX)))) * fG / theShip.mass; fGY = fixtof(fixsin(fixatan(ftofix(diffY/diffX)))) * fG / theShip.mass; theShip.velX += ((shipX > rockX) ? -fGX : fGX); theShip.velY += ((shipX > rockX) ? -fGY : fGY); putpixel(screenBuffer, (int)shipX, (int)shipY, makecol(255, 255, 255)); if (shipX > theRock->x && shipX < theRock->x + theRock->width) { if (shipY > theRock->y && shipY < theRock->y + theRock->height) { theShip.x = 462; theShip.y = 359; theShip.rotation = 0; theShip.velRotation = 0; theShip.velX = 0; theShip.velY = 0; theShip.fuel = theShip.maxFuel; if (currentTime > highTime) { highTime = currentTime; } currentTime = 0; } } theRock = theRock->next; } }