Hey,
This is my code, i am trying to point a certain limb to a certain position:
This is the mathematical part of the code coverted from someone (sorry can't remember) dbp's source.
struct Quaternion
{
float w;
float x;
float y;
float z;
};
struct Euler
{
float x;
float y;
float z;
};
Quaternion ReturnQuat;
Euler ReturnEuler;
void PointLimb(int obj, int limb, float px, float py, float pz);
void Limb_SetDirection(int obj, int limb, float ax, float ay, float az);
void QuatConjugate(Quaternion q);
void EulerToQuat(Euler Rotation);
void QuatToEuler(Quaternion Rotation);
void QuatMult(Quaternion Angle, Quaternion Rotation);
void PointLimb(int obj, int limb, float px, float py, float pz)
{
if (dbObjectExist(obj)==0) { return; }
if (dbLimbExist(obj,limb)==0) { return; }
float t_ax=dbObjectAngleX(obj); float t_ay=dbObjectAngleY(obj); float t_az=dbObjectAngleZ(obj); dbPointObject(obj,px,py,pz);
float ax=dbObjectAngleX(obj);float ay=dbObjectAngleY(obj); float az=dbObjectAngleZ(obj);
dbRotateObject(obj,t_ax,t_ay,t_az);
Limb_SetDirection(obj,limb,ax,ay,az);
}
void Limb_SetDirection(int obj, int limb, float ax, float ay, float az)
{
if (dbObjectExist(obj)==0) { return; }
if (dbLimbExist(obj,limb)==0) { return; }
if (ax==0) { ax += 0.0000001; }
if (ay==0) { ay += 0.0000001; }
if (az==0) { az += 0.0000001; }
Euler e;
Quaternion q1;
Quaternion q2;
Quaternion q3;
e.x=dbObjectAngleX(obj);
e.y=dbObjectAngleY(obj);
e.z=dbObjectAngleZ(obj);
EulerToQuat(e); q1=ReturnQuat;
QuatConjugate(q1); q1=ReturnQuat;
e.x=ax; e.y=ay; e.z=az;
EulerToQuat(e); q2=ReturnQuat;
QuatMult(q2,q1); q3=ReturnQuat;
QuatToEuler(q3);
dbRotateLimb(obj,limb,ReturnEuler.x,ReturnEuler.y,ReturnEuler.z);
}
void QuatConjugate(Quaternion q)
{
ReturnQuat.w=q.w;
ReturnQuat.x=q.x*-1;
ReturnQuat.y=q.y*-1;
ReturnQuat.z=q.z*-1;
}
void EulerToQuat(Euler Rotation)
{
float SRX;
float CRX;
float SRY;
float CRY;
float SRZ;
float CRZ;
Rotation.x = Rotation.x/2;
Rotation.y = Rotation.y/2;
Rotation.z = Rotation.z/2;
SRX = dbSin(Rotation.x);
CRX = dbCos(Rotation.x);
SRY = dbSin(Rotation.y);
CRY = dbCos(Rotation.y);
SRZ = dbSin(Rotation.z);
CRZ = dbCos(Rotation.z);
ReturnQuat.w = CRX*CRY*CRZ + SRX*SRY*SRZ;
ReturnQuat.x = SRX*CRY*CRZ - CRX*SRY*SRZ;
ReturnQuat.y = CRX*SRY*CRZ + SRX*CRY*SRZ;
ReturnQuat.z = CRX*CRY*SRZ - SRX*SRY*CRZ;
}
void QuatToEuler(Quaternion Rotation)
{
ReturnEuler.x = dbATANFULL((2*(Rotation.w*Rotation.x + Rotation.y*Rotation.z)),(1 - 2*(Rotation.x*Rotation.x + Rotation.y*Rotation.y)));
ReturnEuler.y = dbASIN(2*(Rotation.w*Rotation.y - Rotation.z*Rotation.x));
ReturnEuler.z = dbATANFULL((2*(Rotation.w*Rotation.z + Rotation.x*Rotation.y)),(1 - 2*(Rotation.y*Rotation.y + Rotation.z*Rotation.z)));
}
void QuatMult(Quaternion Angle, Quaternion Rotation)
{
ReturnQuat.x = Rotation.x * Angle.w + Rotation.y * Angle.z - Rotation.z * Angle.y + Rotation.w * Angle.x;
ReturnQuat.y = (0-Rotation.x) * Angle.z + Rotation.y * Angle.w + Rotation.z * Angle.x + Rotation.w * Angle.y;
ReturnQuat.z = Rotation.x * Angle.y - Rotation.y * Angle.x + Rotation.z * Angle.w + Rotation.w * Angle.z;
ReturnQuat.w = (0-Rotation.x) * Angle.x - Rotation.y * Angle.y - Rotation.z * Angle.z + Rotation.w * Angle.w;
}
After this I do the folowing:
//Rotate Player's Head Through Neck
int p = dbPickObject(dbMouseX(),dbMouseY(),1,qGetFreeObjectID()-1);
if(p==0)
{
dbHideObject(ntom);
}
else
{
float pd = dbGetPickDistance();
float px = dbGetPickVectorX();
float py = dbGetPickVectorY();
float pz = dbGetPickVectorZ();
float wx = px+dbCameraPositionX();
float wy = py+dbCameraPositionY();
float wz = pz+dbCameraPositionZ();
dbShowObject(ntom);
dbPositionObject(ntom,wx,wy,wz);
PointLimb(id,neckLimb,wx,wy,wz);
}
Where the
ntom variable is an object, and where PointLimb is from the mathematical part.
Well basicaly the neck of the player really does not point to the desired position.
Is there some other way, or something in my code?
Thanks in advanced,
RedEye