C
C#10mo ago
MrScautHD

Hey guys, how can i check if my 2 Quaternions looking in the same direction?

Hey guys, how can i check if my 2 Quaternions looking in the same direction?
7 Replies
SpReeD
SpReeD10mo ago
public static Quaternion LookAt(Vector3 sourcePoint, Vector3 destPoint)
{
Vector3 forwardVector = Vector3.Normalize(destPoint - sourcePoint);

float dot = Vector3.Dot(Vector3.forward, forwardVector);

if (Math.Abs(dot - (-1.0f)) < 0.000001f)
{
return new Quaternion(Vector3.up.x, Vector3.up.y, Vector3.up.z, 3.1415926535897932f);
}
if (Math.Abs(dot - (1.0f)) < 0.000001f)
{
return Quaternion.identity;
}

float rotAngle = (float)Math.Acos(dot);
Vector3 rotAxis = Vector3.Cross(Vector3.forward, forwardVector);
rotAxis = Vector3.Normalize(rotAxis);
return CreateFromAxisAngle(rotAxis, rotAngle);
}
public static Quaternion LookAt(Vector3 sourcePoint, Vector3 destPoint)
{
Vector3 forwardVector = Vector3.Normalize(destPoint - sourcePoint);

float dot = Vector3.Dot(Vector3.forward, forwardVector);

if (Math.Abs(dot - (-1.0f)) < 0.000001f)
{
return new Quaternion(Vector3.up.x, Vector3.up.y, Vector3.up.z, 3.1415926535897932f);
}
if (Math.Abs(dot - (1.0f)) < 0.000001f)
{
return Quaternion.identity;
}

float rotAngle = (float)Math.Acos(dot);
Vector3 rotAxis = Vector3.Cross(Vector3.forward, forwardVector);
rotAxis = Vector3.Normalize(rotAxis);
return CreateFromAxisAngle(rotAxis, rotAngle);
}
MrScautHD
MrScautHDOP10mo ago
thx! oh but i mean to check if a Quaternion look in the same direction as another one that is creating a new one
SpReeD
SpReeD10mo ago
Game Development Stack Exchange
Orienting a model to face a target
I have two objects (target and player), both have Position (Vector3) and Rotation (Quaternion). I want the target to rotate and be facing right at the player. The target, when it shoots something
bighugemassive3
bighugemassive310mo ago
You could convert both to euler angles then do an equality check on those 2 vectors?
MrScautHD
MrScautHDOP10mo ago
Euler angles could be everything It could -390 but it could 30
public unsafe bool AreQuaternionsLookingSameDirection(Quaternion q1, Quaternion q2) {
Vector3 axis1;
float angle1;

Raymath.QuaternionToAxisAngle(q1, &axis1, &angle1);

Vector3 axis2;
float angle2;

Raymath.QuaternionToAxisAngle(q2, &axis2, &angle2);


Quaternion quaternion1 = Quaternion.CreateFromAxisAngle(Vector3.UnitY, 30 * Raylib.DEG2RAD);
Quaternion quaternion2 = Quaternion.CreateFromAxisAngle(Vector3.UnitY, 391 * Raylib.DEG2RAD);

//Quaternion quaternion1 = Quaternion.CreateFromAxisAngle(axis1, angle1);
//Quaternion quaternion2 = Quaternion.CreateFromAxisAngle(axis2, angle2);

// Define a common point (e.g., the origin)
Vector3 point = Vector3.UnitZ;

// Rotate the point using both quaternions
Vector3 rotatedPoint1 = Vector3.Transform(point, quaternion1);
Vector3 rotatedPoint2 = Vector3.Transform(point, quaternion2);

// Calculate the distance between the two rotated points
float distance = Vector3.Distance(rotatedPoint1, rotatedPoint2);

// Define a tolerance for distance comparison
float tolerance = 0.001f; // Adjust as needed

// Check if the distance is within the tolerance
return distance < tolerance;
}
public unsafe bool AreQuaternionsLookingSameDirection(Quaternion q1, Quaternion q2) {
Vector3 axis1;
float angle1;

Raymath.QuaternionToAxisAngle(q1, &axis1, &angle1);

Vector3 axis2;
float angle2;

Raymath.QuaternionToAxisAngle(q2, &axis2, &angle2);


Quaternion quaternion1 = Quaternion.CreateFromAxisAngle(Vector3.UnitY, 30 * Raylib.DEG2RAD);
Quaternion quaternion2 = Quaternion.CreateFromAxisAngle(Vector3.UnitY, 391 * Raylib.DEG2RAD);

//Quaternion quaternion1 = Quaternion.CreateFromAxisAngle(axis1, angle1);
//Quaternion quaternion2 = Quaternion.CreateFromAxisAngle(axis2, angle2);

// Define a common point (e.g., the origin)
Vector3 point = Vector3.UnitZ;

// Rotate the point using both quaternions
Vector3 rotatedPoint1 = Vector3.Transform(point, quaternion1);
Vector3 rotatedPoint2 = Vector3.Transform(point, quaternion2);

// Calculate the distance between the two rotated points
float distance = Vector3.Distance(rotatedPoint1, rotatedPoint2);

// Define a tolerance for distance comparison
float tolerance = 0.001f; // Adjust as needed

// Check if the distance is within the tolerance
return distance < tolerance;
}
i made it work bit messy right now but this works! thx guys!
bighugemassive3
bighugemassive310mo ago
I think you can mod the components with 360 though? double x = vec1.X % 360 Not too sure though, haven't done 3d stuff in a while
MrScautHD
MrScautHDOP10mo ago
fixed it already, but anyways thx
Want results from more Discord servers?
Add your server