So, in effect: How to calculate an actors' position, angle, pitch and roll based on another actor's pos, angle etc., while having an offset in the xyz (forward/sideways/up) axis?
I got some help before when I had a similar problem with aligning a turret to a tank but that one had two shortcomings: It worked with the normal of the underlying sector's plane, and the equation only involved z offset.
This is the turret aligning code I used so far in my Hunter TC.
Code: Select all
const offsetX = 8.0;//not used :(
const offsetY = 0.0;//not used :(
const offsetZ = 16.0;
override void Tick()
{
Super.Tick();
//how to add the x and y offsets?
double forward = offsetZ * sin(master.pitch) * cos(master.angle) * cos(master.roll) + offsetZ * sin(master.roll) * sin(master.angle);
double sideways = offsetZ * sin(master.pitch) * sin(master.angle) * cos(master.roll) - offsetZ * sin(master.roll) * cos(master.angle);
double upwards = offsetZ * cos(master.pitch) * cos(master.roll);
SetOrigin((master.pos.X + forward, master.pos.Y + sideways, master.pos.Z + upwards), true);
AlignToPlaneTurret(master,0,false);
}
void AlignToPlaneTurret(Actor a, double pitchAdjust = 0, bool ceiling = false)
{
if (!a)
{
return;
}
double ceilingAdjust = 0;
Vector3 fnormal;
if (!ceiling)
{
fnormal = a.CurSector.FloorPlane.Normal;
}
else
{
fnormal = a.CurSector.CeilingPlane.Normal;
ceilingAdjust = 180.0;
}
Vector2 fnormalp1 = (fnormal.x != 0 || fnormal.y != 0) ? (fnormal.x, fnormal.y).Unit() : (0, 0);
Vector2 fnormalp2 = ((fnormal.x, fnormal.y).Length(), fnormal.z);
double dang = self.Angle;
double fang = atan2(fnormalp1.y, fnormalp1.x);
double fpitch = atan2(fnormalp2.x, fnormalp2.y);
double ddiff1 = sin(fang - (dang - pitchAdjust));
double ddiff2 = cos(fang - dang);
//the values we want in the end
double pitchTarget = (fpitch * ddiff2 - pitchAdjust) + ceilingAdjust;
double rollTarget = -fpitch * ddiff1 + ceilingAdjust;
double angleTarget = dang + ceilingAdjust;