Search In
• More options...
Find results that contain...
Find results in...

3. Site Suggestions Get Offset From Entity Given World Coords (general calculation)

Recommended Posts Hi all,

I just wanted to share a general equation for implementing relative distance or offset from entity given world coords (hash: 0x2274BC1C4885E333). I thought it was as straight forward as the difference of two vectors but it's not. I put in a bunch of numbers on my screen and realized it was using a dot product of directions.

Here is my implementation:

public Vector3 RelativeDistance(Vector3 Origin, Vector3 Rotation, Vector3 Position){    Vector3 RightVector = RelativeRightVector(Rotation);    Vector3 ForwardVector = RotationToDirection(Rotation);    return new Vector3(-1 * Vector3.Dot(RightVector, (Origin - Position)), -1 * Vector3.Dot(ForwardVector, (Origin - Position)), Position.Z - Origin.Z);}public Vector3 RelativeRightVector(Vector3 Rotation)//source is from scripthookdotnet{   double num = Math.Cos(Rotation.Y * (Math.PI / 180.0));   return new Vector3((float)(Math.Cos(-Rotation.Z * (Math.PI / 180.0)) * num), (float)(Math.Sin(Rotation.Z * (Math.PI / 180.0)) * num), (float)Math.Sin(-Rotation.Y * (Math.PI / 180.0)));}public Vector3 RotationToDirection(Vector3 Rotation)//I forgot where I got this from, I had it for a long time{   float z = Rotation.Z;   float num = z * 0.0174532924f;   float x = Rotation.X;   float num2 = x * 0.0174532924f;   float num3 = Math.Abs((float)Math.Cos((double)num2));   return new Vector3   {     X = (-(float)Math.Sin(num)) * num3,     Y = (float)Math.Cos(num) * num3,     Z = (float)Math.Sin(num2)   };}

Edited by nm710

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

• 1 User Currently Viewing 0 members, 0 Anonymous, 1 Guest

×
×
• Create New...