Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script Help Focus Distance Debug Variable?
TheGreatCthulhu Offline
Member

Posts: 213
Threads: 10
Joined: Oct 2010
Reputation: 32
#13
RE: Focus Distance Debug Variable?

@Daemian:
Let me offer some more insight and advice about how to calculate distances in 3D space:

You basically have two 3D vectors that tell you the location of each entity
locFrom = (x1, y1, z1)
locTo = (x2, y2, z2)


To get the vector that represents the distance between them, you simply need to calculate the (locTo - locFrom) vector, and get its length.
The subtraction part is easy:
locDelta = (locTo - locFrom) = (x2 - x1, y2 - y1, z2 - z1) = (x3, y3, z3)

(That corresponds the r-r' vector in the image below.)
[Image: central.png?w=300&h=&cache=cache]

Finally, you get the length by calculating
dist = Sqrt(x3^2 + y3^2 + z3^2)
Where ^2 stands for squared.

(See the image below to see why the length of a vector is calculated in this way - it's just the Pythagorean theorem.)
[Image: vect.h9.gif]

Note that there's no need for absolute value, since squared quantities are always positive.

What you're doing will generally not give you the correct distance - you also start with two vectors:
locFrom = (x1, y1, z1)
locTo = (x2, y2, z2)


But then you do this (I use || to denote abs value):
dist1 = Sqrt(|y1|^2 + |Sqrt(|x1|^2 + |z1|^2)|^2);

which is basically the same as this, since you take the square of the square root:
dist1 = Sqrt(x1^2 + y1^2 + z1^2);

Which is the distance from the origin to the first entity.

Then you calculate
dist2 = Sqrt(|y2|^2 + |Sqrt(|x2|^2 + |z2|^2)|^2);

which equivalent to:
dist2 = Sqrt(x2^2 + y2^2 + z2^2);

That's the distance from the origin to the second entity.
With that, you have two distances, without any information about the directions.

Then you do this:
dist = Max(dist1, dist2) - Min(dist1, dist2)

which is the same as
dist = |dist2 - dist1|

This will only give you the amount their distances differ from the origin, which will match the actual distance between the two objects only if they are on the same line (ray) from the origin. For example, if they are at different angles with respect to the origin, but at the same distance from the origin, the result would be 0, even though there's a non-zero distance between the objects themselves.

BTW, when programming/scripting, if speed is important, you want to do as few Sqrt operations as possible, because these are computationally pretty expensive; on the other hand, + and * are relatively cheep. Also, it's the game will take less time to calculate a*a then MathPow(a, 2), so if it's just the square, people often write a*a, and use the math function for higher powers.

So, with regard to all that, the code should look something like this:
PHP Code: (Select All)
float GetEntitiesDistance string &in fromEntitystring &in toEntity )
{
    
float x1 GetEntityPosXfromEntity );
    
float y1 GetEntityPosYfromEntity );
    
float z1 GetEntityPosZfromEntity );
    
    
float x2 GetEntityPosXtoEntity );
    
float y2 GetEntityPosYtoEntity );
    
float z2 GetEntityPosZtoEntity );

    
float x3 x2 x1;
    
float y3 y2 y1;
    
float z3 z2 z1;
    
float distance MathSqrt(x3*x3 y3*y3 z3*z3);

    return 
distance;

(This post was last modified: 12-29-2014, 01:46 PM by TheGreatCthulhu.)
12-29-2014, 01:41 PM
Find


Messages In This Thread
RE: Focus Distance Debug Variable? - by Daemian - 12-20-2014, 03:40 PM
RE: Focus Distance Debug Variable? - by Mudbill - 12-20-2014, 09:34 PM
RE: Focus Distance Debug Variable? - by Mudbill - 12-20-2014, 10:09 PM
RE: Focus Distance Debug Variable? - by Daemian - 12-27-2014, 04:22 PM
RE: Focus Distance Debug Variable? - by TheGreatCthulhu - 12-29-2014, 01:41 PM
RE: Focus Distance Debug Variable? - by Daemian - 12-29-2014, 11:12 PM



Users browsing this thread: 1 Guest(s)