Rotation around pivot point is a specific case of more general rotation function
This commit is contained in:
parent
2e60b35c8e
commit
60cbb26f9f
|
|
@ -131,34 +131,37 @@ void VectorScale(Vector3 *v, float scale)
|
||||||
v->z *= scale;
|
v->z *= scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rotate vector by axis and angle around the pivot point
|
// Rotate vector by axis and angle from the global center
|
||||||
void VectorRotate(Vector3 *v, Vector3 pivot, Vector3 axis, float angle)
|
void VectorRotate(Vector3 *v, Vector3 axis, float angle)
|
||||||
{
|
{
|
||||||
VectorNormalize(&axis);
|
VectorNormalize(&axis);
|
||||||
float tx = v->x - pivot.x;
|
|
||||||
float ty = v->y - pivot.y;
|
|
||||||
float tz = v->z - pivot.z;
|
|
||||||
|
|
||||||
float a = angle * (PI / 180.0f);
|
float a = angle * (PI / 180.0f);
|
||||||
float sina = sin(a);
|
float sina = sin(a);
|
||||||
float cosa = cos(a);
|
float cosa = cos(a);
|
||||||
float cosb = 1.0f - cosa;
|
float cosb = 1.0f - cosa;
|
||||||
|
|
||||||
float xrot = tx * (axis.x * axis.x * cosb + cosa)
|
float xrot = v->x * (axis.x * axis.x * cosb + cosa)
|
||||||
+ ty * (axis.x * axis.y * cosb - axis.z * sina)
|
+ v->y * (axis.x * axis.y * cosb - axis.z * sina)
|
||||||
+ tz * (axis.x * axis.z * cosb + axis.y * sina);
|
+ v->z * (axis.x * axis.z * cosb + axis.y * sina);
|
||||||
|
|
||||||
float yrot = tx * (axis.y * axis.x * cosb + axis.z * sina)
|
float yrot = v->x * (axis.y * axis.x * cosb + axis.z * sina)
|
||||||
+ ty * (axis.y * axis.y * cosb + cosa)
|
+ v->y * (axis.y * axis.y * cosb + cosa)
|
||||||
+ tz * (axis.y * axis.z * cosb - axis.x * sina);
|
+ v->z * (axis.y * axis.z * cosb - axis.x * sina);
|
||||||
|
|
||||||
float zrot = tx * (axis.z * axis.x * cosb - axis.y * sina)
|
float zrot = v->x * (axis.z * axis.x * cosb - axis.y * sina)
|
||||||
+ ty * (axis.z * axis.y * cosb + axis.x * sina)
|
+ v->y * (axis.z * axis.y * cosb + axis.x * sina)
|
||||||
+ tz * (axis.z * axis.z * cosb + cosa);
|
+ v->z * (axis.z * axis.z * cosb + cosa);
|
||||||
|
|
||||||
v->x = xrot + pivot.x;
|
*v = (Vector3){ xrot, yrot, zrot };
|
||||||
v->y = yrot + pivot.y;
|
}
|
||||||
v->z = zrot + pivot.z;
|
|
||||||
|
// Rotate vector by axis and angle around the pivot point
|
||||||
|
void VectorRotateAround(Vector3 *v, Vector3 pivot, Vector3 axis, float angle)
|
||||||
|
{
|
||||||
|
*v = VectorSubtract(*v, pivot);
|
||||||
|
VectorRotate(v, axis, angle);
|
||||||
|
*v = VectorAdd(*v, pivot);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Negate provided vector (invert direction)
|
// Negate provided vector (invert direction)
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,8 @@ Vector3 VectorPerpendicular(Vector3 v); // Calculate one vector
|
||||||
float VectorDotProduct(Vector3 v1, Vector3 v2); // Calculate two vectors dot product
|
float VectorDotProduct(Vector3 v1, Vector3 v2); // Calculate two vectors dot product
|
||||||
float VectorLength(const Vector3 v); // Calculate vector lenght
|
float VectorLength(const Vector3 v); // Calculate vector lenght
|
||||||
void VectorScale(Vector3 *v, float scale); // Scale provided vector
|
void VectorScale(Vector3 *v, float scale); // Scale provided vector
|
||||||
void VectorRotate(Vector3 *v, Vector3 pivot, Vector3 axis, float angle); // Rotate vector by axis and angle around the pivot point
|
void VectorRotate(Vector3 *v, Vector3 axis, float angle); // Rotate vector by axis and angle from the global center
|
||||||
|
void VectorRotateAround(Vector3 *v, Vector3 pivot, Vector3 axis, float angle); // Rotate vector by axis and angle around the pivot point
|
||||||
void VectorNegate(Vector3 *v); // Negate provided vector (invert direction)
|
void VectorNegate(Vector3 *v); // Negate provided vector (invert direction)
|
||||||
void VectorNormalize(Vector3 *v); // Normalize provided vector
|
void VectorNormalize(Vector3 *v); // Normalize provided vector
|
||||||
float VectorDistance(Vector3 v1, Vector3 v2); // Calculate distance between two points
|
float VectorDistance(Vector3 v1, Vector3 v2); // Calculate distance between two points
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user