Convert relative vehicle attached object's position and rotation to absolute one and vice-versa
#1

When I use AttachObjectToVehicle, how can I convert the vehicle-relative position and rotation to absolute world coordinates, so that if used on CreateObject with these coordinates, the object would appear on the same position as the attached one (provided the vehicle won't move)? I know one has to use GetVehicleRotationQuat, but I am totally baffled by this function's output. Anyone please help.
Reply
#2

u dont have to use GetVehicleRotationQuat .. im pretty sure ..

if you do, then u have no choice but give up because probably 99% of forum dont know how to use this function..

Cant u get Vehicle's Z Angle then just use Cosine and Sine to add to the vehicle'x X,Y,Z coords to get the absolute position of the object?
Reply
#3

Z Angle would work well on players, but the vehicle might be tilted or on its roof, I want this to be accurate.
Reply
#4

oh... well then.. good luck lol

thats the only way i know and how i would do it..

can you post a solution if you figure this out?
Reply
#5

Okay, I've made a code to apply a quaternion on a vector, but no rotation yet:
pawn Код:
stock GetVehicleRelativeVector(veh, Float:v1[3], Float:v2[3])
{
    new Float:q1[4];
    GetVehicleRotation(veh, q1);
    GetQuatConjugate(q1, q1);
    RotateVectorQuat(v1, q1, v2);
}

stock RotateVectorQuat(Float:v1[3], Float:q[4], Float:v2[3])
{
    new Float:q1[4], Float:q2[4];
    q1 = q;
    q2[1] = v2[0], q2[2] = v2[1], q2[3] = v2[2];
    GetQuatProduct(q1, q2, q2);
    GetQuatConjugate(q1, q1);
    GetQuatProduct(q2, q1, q2); //(q1 Ч q2) Ч q1'
    v2[0] = q2[1], v2[1] = q2[2], v2[2] = q2[3];
}

stock GetVehicleRotation(veh, Float:q[4])
{
    return GetVehicleRotationQuat(veh, q[0], q[1], q[2], q[3]);
}

stock GetQuatConjugate(Float:q1[4], Float:q2[4])
{
    q2[0] =  q1[0];
    q2[1] = -q1[1];
    q2[2] = -q1[2];
    q2[3] = -q1[3];
}

stock GetQuatProduct(Float:q1[4], Float:q2[4], Float:q3[4])
{
    new Float:w = q1[0]*q2[0] - q1[1]*q2[1] - q1[2]*q2[2] - q1[3]*q2[3];
    new Float:x = q1[0]*q2[1] + q1[1]*q2[0] + q1[2]*q2[3] - q1[3]*q2[2];
    new Float:y = q1[0]*q2[2] - q1[1]*q2[3] + q1[2]*q2[0] + q1[3]*q2[1];
    new Float:z = q1[0]*q2[3] + q1[1]*q2[2] - q1[2]*q2[1] + q1[3]*q2[0];
    q3[0] = w, q3[1] = x, q3[2] = y, q3[3] = z;
}
Edit:
I've tried this, but the angles seem to be swapping to reversed rotation:
pawn Код:
stock GetQuatEuler(Float:q[4], Float:r[3])
{
    r[0] = atan2(2*(q[0]*q[2]-q[1]*q[3]), 1-2*(q[2]*q[2] - q[3]*q[3]));
    r[1] = atan2(2*(q[0]*q[1]-q[2]*q[3]), 1-2*(q[1]*q[1] - q[3]*q[3]));
    r[2] = asin(2*(q[1]*q[2] + q[0]*q[3]));
}
Reply
#6

Now I've found the solution... after exactly a year. Isn't it interesting?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)