**Not a member of Pastebin yet?**

**, it unlocks many cool features!**

__Sign Up__- Okay, so when you calculate how far to move your ship each iteration, you'll end up with a value to add to each coordinate.
- I'm assuming you are doing this with a look-up table based on the angle at which you are facing.
- You said the biggest change was 255 which is an 8-bit value. Perfect.
- So I'm calling the amount you move each iteration (deltaX,deltaY,deltaZ).
- I'm sorry, I know this is in conflict with some of your other notation.
- Anyways, suppose that coordinates are 24-bit integers. (actually smaller, they'll be on [-1000000,1000000]
- Your coordinats are (x0,y0,z0).
- The object against which to compare is (x1,y1,z1).
- In the program overhead, if MapData is not empty, compute for each item:
- (x0-x1)^2+(y0-y1)^2+(z0-z1)^2
- Call this value and call it d2 (distance squared). Keep this value for each item somewhere! It will need 44 bits, so a 48-bit integer (6 bytes) for each item in mapData will suffice.
- DO NOT compute the square root
- Every time a new object is added to MapData, you'll need to compute this value.
- If the object starts at the same place as an existing object, you can just copy this value. No need to calculate.
- If the object starts at a small offset from an existing object, somewhat faster math can be used to calculate this value.
- So, let's get down to business <s>to defeat the huns.</s>
- With this precomputed value, we'll now be able to compute distance a lot faster.
- Whenever you move, you'll need to update d2 in every object in MapData:
- d = deltaX*(x0-x1)+deltaY*(y0-y1)+deltaZ*(z0-z1)
- d = d + d
- d2= d2+ d
- The computations require:
- 3 of:
- Subtracting 24-bit integers
- Multiplying the resulting 24-bit integer by an 8-bit value to get a 32-bit integer
- Adding each of these 32-bit integers together.
- Doubling this 34-bit value
- Adding this to the 48-bit value, d2
- Whenever you want to compare a distance, don't compute the square root! For example, if you want to see if the object is within 10000 units:
- if (d2<=100000000){ //10000*100000
- //Do something
- }
- Let me know if you encounter a situation where a square root is necessary! I can either:
- -Restructure the code so that it doesn't need a square root
- -Create a fast square root algorithm that'll work on integers.

RAW Paste Data