From 8557a6d8d505b404e1fa1e5cacba758a81266003 Mon Sep 17 00:00:00 2001 From: Constantine Tarasenkov Date: Tue, 3 Feb 2015 02:25:25 +0300 Subject: [PATCH 1/3] Adding VectorRotate() function --- src/raymath.c | 30 ++++++++++++++++++++++++++++++ src/raymath.h | 1 + 2 files changed, 31 insertions(+) diff --git a/src/raymath.c b/src/raymath.c index df098c6a3..194b23ff9 100644 --- a/src/raymath.c +++ b/src/raymath.c @@ -131,6 +131,36 @@ void VectorScale(Vector3 *v, float scale) v->z *= scale; } +// Rotate vector by axis and angle around the pivot point +void VectorRotate(Vector3 *v, Vector3 pivot, Vector3 axis, float angle) +{ + VectorNormalize(&axis); + float tx = v->x - pivot.x; + float ty = v->y - pivot.y; + float tz = v->z - pivot.z; + + float a = (float)(angle * (PI / 180.0)); + float sina = sin(a); + float cosa = cos(a); + float cosb = 1.0f - cosa; + + float xrot = tx * (axis.x * axis.x * cosb + cosa) + + ty * (axis.x * axis.y * cosb - axis.z * sina) + + tz * (axis.x * axis.z * cosb + axis.y * sina); + + float yrot = tx * (axis.y * axis.x * cosb + axis.z * sina) + + ty * (axis.y * axis.y * cosb + cosa) + + tz * (axis.y * axis.z * cosb - axis.x * sina); + + float zrot = tx * (axis.z * axis.x * cosb - axis.y * sina) + + ty * (axis.z * axis.y * cosb + axis.x * sina) + + tz * (axis.z * axis.z * cosb + cosa); + + v->x = xrot + pivot.x; + v->y = yrot + pivot.y; + v->z = zrot + pivot.z; +} + // Negate provided vector (invert direction) void VectorNegate(Vector3 *v) { diff --git a/src/raymath.h b/src/raymath.h index c8c1a26c3..d1b1af55d 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -86,6 +86,7 @@ Vector3 VectorPerpendicular(Vector3 v); // Calculate one vector float VectorDotProduct(Vector3 v1, Vector3 v2); // Calculate two vectors dot product float VectorLength(const Vector3 v); // Calculate vector lenght 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 VectorNegate(Vector3 *v); // Negate provided vector (invert direction) void VectorNormalize(Vector3 *v); // Normalize provided vector float VectorDistance(Vector3 v1, Vector3 v2); // Calculate distance between two points From 2e60b35c8e50df898a84ac4e26d59c24e56e9504 Mon Sep 17 00:00:00 2001 From: Constantine Tarasenkov Date: Tue, 3 Feb 2015 03:05:52 +0300 Subject: [PATCH 2/3] Unnecessary cast --- src/raymath.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/raymath.c b/src/raymath.c index 194b23ff9..037ce618d 100644 --- a/src/raymath.c +++ b/src/raymath.c @@ -139,7 +139,7 @@ void VectorRotate(Vector3 *v, Vector3 pivot, Vector3 axis, float angle) float ty = v->y - pivot.y; float tz = v->z - pivot.z; - float a = (float)(angle * (PI / 180.0)); + float a = angle * (PI / 180.0f); float sina = sin(a); float cosa = cos(a); float cosb = 1.0f - cosa; From 60cbb26f9fb066b4bbc72779628b77095d327b55 Mon Sep 17 00:00:00 2001 From: Constantine Tarasenkov Date: Tue, 3 Feb 2015 19:50:53 +0300 Subject: [PATCH 3/3] Rotation around pivot point is a specific case of more general rotation function --- src/raymath.c | 37 ++++++++++++++++++++----------------- src/raymath.h | 3 ++- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/raymath.c b/src/raymath.c index 037ce618d..762474abf 100644 --- a/src/raymath.c +++ b/src/raymath.c @@ -131,34 +131,37 @@ void VectorScale(Vector3 *v, float scale) v->z *= scale; } -// Rotate vector by axis and angle around the pivot point -void VectorRotate(Vector3 *v, Vector3 pivot, Vector3 axis, float angle) +// Rotate vector by axis and angle from the global center +void VectorRotate(Vector3 *v, Vector3 axis, float angle) { 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 sina = sin(a); float cosa = cos(a); float cosb = 1.0f - cosa; - float xrot = tx * (axis.x * axis.x * cosb + cosa) - + ty * (axis.x * axis.y * cosb - axis.z * sina) - + tz * (axis.x * axis.z * cosb + axis.y * sina); + float xrot = v->x * (axis.x * axis.x * cosb + cosa) + + v->y * (axis.x * axis.y * cosb - axis.z * sina) + + v->z * (axis.x * axis.z * cosb + axis.y * sina); + + float yrot = v->x * (axis.y * axis.x * cosb + axis.z * sina) + + v->y * (axis.y * axis.y * cosb + cosa) + + v->z * (axis.y * axis.z * cosb - axis.x * sina); - float yrot = tx * (axis.y * axis.x * cosb + axis.z * sina) - + ty * (axis.y * axis.y * cosb + cosa) - + tz * (axis.y * axis.z * cosb - axis.x * sina); + float zrot = v->x * (axis.z * axis.x * cosb - axis.y * sina) + + v->y * (axis.z * axis.y * cosb + axis.x * sina) + + v->z * (axis.z * axis.z * cosb + cosa); - float zrot = tx * (axis.z * axis.x * cosb - axis.y * sina) - + ty * (axis.z * axis.y * cosb + axis.x * sina) - + tz * (axis.z * axis.z * cosb + cosa); + *v = (Vector3){ xrot, yrot, zrot }; +} - v->x = xrot + pivot.x; - 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) diff --git a/src/raymath.h b/src/raymath.h index d1b1af55d..a22a71d51 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -86,7 +86,8 @@ Vector3 VectorPerpendicular(Vector3 v); // Calculate one vector float VectorDotProduct(Vector3 v1, Vector3 v2); // Calculate two vectors dot product float VectorLength(const Vector3 v); // Calculate vector lenght 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 VectorNormalize(Vector3 *v); // Normalize provided vector float VectorDistance(Vector3 v1, Vector3 v2); // Calculate distance between two points