-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
CoreEverything related to Core moduleEverything related to Core module
Milestone
Description
This code might actually work:
// Generate a 4x4 rotation matrix
std::array<std::array<float, 4>, 4> rotationMatrix(const Vector3& front, const Vector3& target) {
Vector3 frontNorm = front.normalize();
Vector3 targetNorm = target.normalize();
// Compute rotation axis and angle
Vector3 axis = frontNorm.cross(targetNorm);
float dot = frontNorm.dot(targetNorm);
float angle = std::acos(std::clamp(dot, -1.0f, 1.0f)); // Clamp to avoid precision issues
// Normalize the axis
if (axis.x != 0 || axis.y != 0 || axis.z != 0) {
axis = axis.normalize();
} else {
// Handle case when front and target are the same or opposite
if (dot > 0) {
// Same direction, no rotation needed
return {{
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
}};
} else {
// Opposite direction, rotate 180 degrees around an arbitrary perpendicular axis
axis = {1, 0, 0}; // Choose x-axis as arbitrary axis
}
}
// Compute rotation matrix using Rodrigues' formula
float c = std::cos(angle);
float s = std::sin(angle);
float t = 1.0f - c;
float x = axis.x, y = axis.y, z = axis.z;
return {{
{t * x * x + c, t * x * y - s * z, t * x * z + s * y, 0},
{t * x * y + s * z, t * y * y + c, t * y * z - s * x, 0},
{t * x * z - s * y, t * y * z + s * x, t * z * z + c, 0},
{0, 0, 0, 1}
}};
}
```Metadata
Metadata
Labels
CoreEverything related to Core moduleEverything related to Core module