#### Archived

This topic is now archived and is closed to further replies.

# Speed question

## Recommended Posts

In a 2D game I''m making, I have some alien spaceships that can roatate to any orientation. They need to be able to fire lasers and such from any angle, so the lasers need to be able to move along a ray extending from the alien at a certain angle. I move all the objects in my game by assigning them an x velocity and a y velocity, and adding these velocities to the object''s coordinates each frame. If I have the initial coordinates (x,y) and my angle A, how can I calculate the necessary x and y velocities for a projectile if it needs to move at a certain speed: sqrt((vx^2)+(vy^2)) = speed^2. I have figured out how to do this, but I need to use the sqrt() function in the calculation. I figure that having to calculate many sqrt()s each frame would be a big performance drain, so I need a way to do this without square rooting anything. In Andre LaMothe''s TOTWPG, he has a function called Fast_Distance_2D that speedily approximates sqrt(x^2+y^2). I think my problem can be solved with a similar method, but I have no idea how Fast_Distance_2D works, so I can''t figure it out. Here''s the code for Fast_Distance_2D:

int Fast_Distance_2D(int x, int y)
{
// this function computes the distance from 0,0 to x,y with 3.5% error

// first compute the absolute value of x,y
x = abs(x);
y = abs(y);

// compute the minimum of x,y
int mn = MIN(x,y);

// return the distance
return(x+y-(mn>>1)-(mn>>2)+(mn>>4));

} // end Fast_Distance_2D



##### Share on other sites
Doesn''t anyone understand LaMothe''s code?

Things are not what they are.

##### Share on other sites
Well, you don''t REALLY need to know how it works in order to use it. But I know that I don''t like using stuff I don''t understand, so here goes:

Without loss of generality (WOLOG) assume x < y

For every value of x and y, y = ax for some a = y / x. The whole idea behind this algorithm is finding an optimal value for a. The value used in this algorithm is 21/2, but I haven''t been able to verify that this is the optimal value (it works well though).

Note: 231 / 160 is approximately 21/2

So after a little algebraic manipulation:
y = 231x / 160
160y = 231x
160xy = 231x2
160xy + 25x2 = 231x2 + 25x2
160xy + 25x2 = 256x2
256y2 + 160xy + 25x2 = 256x2 + 256y2
(16y + 5x)2 = 256(x2 + y2)
16y + 5x = 16(x2 + y2)1/2
y + 5x / 16 = (x2 + y2)1/2
y + (16 - 8 - 4 + 1)x / 16 = (x2 + y2)1/2
y + x - x / 2 - x / 4 + x / 16 = (x2 + y2)1/2

And that''s it. The only mystery left is where they got 21/2 from.

Give a man a fish and you feed him for a day; teach him to use the Net and he won''t bother you for weeks.

##### Share on other sites
Actually, I really do need to know how it works. I can't use the actual function itself in my calculation (it does not perform the operation I need), but I hope to apply the principle of it to my problem.

EDIT: I've solved my problem. Woohoo!

Things are not what they are.

[edited by - myme15 on June 9, 2002 11:38:35 PM]

##### Share on other sites
And what''s wrong with this:

You need to have a velocity and an angle the ship is in and simply get the amount of movement done:

---

Velocity_this_frame_by_x = cos(angle)*velocity;
Velocity_this_frame_by_y = sin(angle)*velocity;

---

---
Keep it simple, stupid

##### Share on other sites
quote:

And what''s wrong with this:

You need to have a velocity and an angle the ship is in and simply get the amount of movement done:

---

Velocity_this_frame_by_x = cos(angle)*velocity;
Velocity_this_frame_by_y = sin(angle)*velocity;

---

Well.. Most compilers are set on radians, so you have to divide the angle by 3.14 and multiply it by 180

And it''s prolly wise to use a lookup table, but besides that,
nice work

##### Share on other sites
quote:
The only mystery left is where they got 21/2 from.

My first guess is that it''s chosen for all those speedy bitshifts at the end. Other than that, the math works out nicely with the perfect squares and roots. Besides, all mathamaticians have a place in their hearts for 21/2

##### Share on other sites
Are you nuts?

You are using trig for simple lasers? Use vectors instead (vector line equations). They are so much easier and the most complicated operator you will ever enounter is divide. For velocity all you have to do is adjust t. Im assuming you want all your objects moving at a velocity that wont change its direction (you dont need an x and y velocity).

##### Share on other sites
To clarify my problem for you people, basically I wasn''t thinking straight. I failed to realize that

(cos(angle)*speed)^2 + (sin(angle)*speed)^2 = speed^2

Now that I realize this, my question seems kinda stupid .

llvllatrix: I find trig very easy and fast (as long as you use lookup tables). I''m a high school freshman, and I haven''t studied vector math yet, so I don''t use it. Maybe I should read up on it and see the error of my ways

Things are not what they are.

##### Share on other sites
Would using sqrt these days cause a huge performance hit? Im using lots of them in my code and it runs fine. I though these computers had seperate math processors to handle things like sqrt with no performance hit? Floating point is no longer slow either?

##### Share on other sites
Floating point is very fast with the latest processors, but I''m not taking any chances with sqrt.

Things are not what they are.

• ### Forum Statistics

• Total Topics
628333
• Total Posts
2982130

• 24
• 9
• 9
• 13
• 11