sorry for bumping an old thread,
but i guess this is the only way to get the facing of the survey tool while staying out of process.
i've tried OP's code and get a strage behavior when the angle of the survey tool is greater than 180°.
i've debugged the whole last day and have ask a whole lot of people but nobody was able to help me.
dump of the vars and a possible error in the calculation below.
so, here is the code i'm using:
Code:
ulong packed = Memory.ReadUInt64(Memory.ReadUInt(Memory.ReadUInt(ObjectBaseAdress + 0x1c8) + 0x4) + (0x20 + 0x100));
Console.WriteLine("[DEBUG] Packed: " + packed.ToString());
var x = (packed >> 42) * (1.0f / 2097152.0f);
var y = (((packed << 22) >> 32) >> 11) * (1.0f / 1048576.0f);
var z = (packed << 43 >> 43) * (1.0f / 1048576.0f);
var w = x * x + y * y + z * z;
Console.WriteLine("[DEBUG] x: " + x.ToString() + " " + " y: " + y.ToString() + " " + " z: " + z.ToString() + " " + " w: " + w.ToString());
if (Math.Abs(w - 1.0f) >= (1.0f / 1048576.0f))
{
Console.WriteLine("[DEBUG] w if match");
w = (float)Math.Sqrt(1.0f - w);
}
else
{
Console.WriteLine("[DEBUG] w if no match");
w = 0.0f;
}
Console.WriteLine("[DEBUG] w after if: " + w.ToString());
var Y = 2 * z * w + 2 * x * y;
var X = 1 - 2 * y * y - 2 * z * z;
Console.WriteLine("[DEBUG] Y: " + Y.ToString() + " " + " X: " + X.ToString());
var result = Math.Atan2(Y, X);
var angle = result * 180 / Math.PI;
Console.WriteLine("[DEBUG] angle pre if: " + angle.ToString());
if (angle < 0)
{
angle = angle + 360;
}
Console.WriteLine("[DEBUG] result: " + result.ToString());
Console.WriteLine("[DEBUG] angle: " + angle.ToString());
return angle;
yes it's basicly the same as the OP's code.
as stated, the calculation works flawless for angles smaller than 180°
here is a dump of the vars when everthing works:
angle smaller 180°
Code:
[DEBUG] Packed: 365024
[DEBUG] x: 0 y: 0 z: 0,348114 w: 0,1211834
[DEBUG] w if match
[DEBUG] w after if: 0,9374522
[DEBUG] Y: 0,6526805 X: 0,7576333
[DEBUG] angle pre if: 40,7440068657897
[DEBUG] result: 0,711117070263206
[DEBUG] angle: 40,7440068657897
[DEBUG] My Rotation: 46,533
[DEBUG] Survey Angle: 40,744
so, no big deal here.
next is a dump when it doesn't work:
angle larger 180°
Code:
[DEBUG] Packed: 1133353
[DEBUG] x: 0 y: 0 z: 1,08085 w: 1,168236
[DEBUG] w if match
[DEBUG] w after if: n. def.
[DEBUG] Y: n. def. X: -1,336472
[DEBUG] angle pre if: n. def.
[DEBUG] result: n. def.
[DEBUG] angle: n. def.
[DEBUG] My Rotation: 226,264
[DEBUG] Survey Angle: n. def.
so this is the problem.
things i noticed this far:
- x and y are always 0, no problem, i don't need them
- the if condition is always met
- if w is larger than 1, the math.sqrt will fail! because the value (1.0f - w) is negative.
- when the code works, z and w are below 1
- when the code fails, z and w are above 1
i can't find the solution to this problem. i've search the net and read stuff about the quaternion calculation.
if someone could help me with this or point me in the right direction i would really appreciate this.
thanks