Re: Programming question

From: Omar Vega <omar_at_Techsource.COM>
Date: Wed Oct 27 1999 - 17:39:28 EDT

Here's some C code for a square root estimation using integer math. The
typecasting might need a little massaging but the basic algorithm should
work for your app.

#define XABS(x) ((x)<0?-(x):(x))

int
root( x )
    long long x;
{
    long long temp, t1, t2, t3, t4;

 if ( x < 10 )
     return (int)0;
 else
 {
     temp = x / 10;
     while ( XABS(temp -(x/temp)) > 1)
  temp = (temp + (x/temp))/2;
 }
 return((int)temp);
}

----- Original Message -----
From: jeff hendrix <jhendrix@Quark.Com>
To: <vectorlist@lists.cc.utexas.edu>
Sent: Wednesday, October 27, 1999 5:09 PM
Subject: Programming question

> I'm working hard to try and finish up space wars that will run on space
duel
> hardware.
> I've got most things in it working, except I'm having trouble with the
> gravity.
> I'm implementing the following procedure:
>
> // calculate distance from the sun
> xLen = sunPos.x - objPos.x;
> yLen = sunPos.y - objPos.y;
> lengthsqr = (xLen * xLen) + (yLen * yLen);
> length = sqrt(lengthsqr);
> // calculate unit vector
> unitX = xLen / length;
> unitY = yLen / length;
> // gravity is inverse r squared
> unitX = unitX/lengthsqr;
> unitY = unitY/lengthsqr;
> // update object speed
> speed.x += unitX;
> speed.y += unitY;
>
> I have everything figured out except how to get the square root of
> lengthsqr.
> Is there any easy or fast way to calculate the square root of a 16 bit
> number on a 6502?
> Or is there another way to calculate the pull of gravity, separated into x
> and y components, that still follows the rule of the inverse of the square
> of the distance?
>
> -jeff
>
> ps. I might soon have a "incomplete" version that can be played on MAME.
>
>
Received on Wed Oct 27 16:22:44 1999

This archive was generated by hypermail 2.1.8 : Fri Aug 01 2003 - 00:32:47 EDT