mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-11-22 07:23:00 +00:00
initial WIP 3d point rotation algorithm
Thanks to GitHub Copilot for the implementation! Just need to fully check it actually works, and then we can port it to Lua and Vector3.
This commit is contained in:
parent
53609ac83e
commit
8372a12f48
1 changed files with 57 additions and 0 deletions
57
rotate.js
Executable file
57
rotate.js
Executable file
|
@ -0,0 +1,57 @@
|
||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rotate a given point around an origin point in 3d space.
|
||||||
|
* @source GitHub Copilot
|
||||||
|
* @warning Not completely tested! Pending a thorough evaluation.
|
||||||
|
* @param {Vector3} origin The origin point to rotate around
|
||||||
|
* @param {Vector3} point The point to rotate.
|
||||||
|
* @param {Number} x Rotate this much around the X axis (yz plane), in radians.
|
||||||
|
* @param {Number} y Rotate this much around the Y axis (xz plane), in radians.
|
||||||
|
* @param {Number} z Rotate this much around the Z axis (xy plane), in radians.
|
||||||
|
* @return {Vector3} The rotated point.
|
||||||
|
*/
|
||||||
|
function rotate_point_3d(origin, point, x, y, z)
|
||||||
|
{
|
||||||
|
point[0] = point[0] - origin[0];
|
||||||
|
point[1] = point[1] - origin[1];
|
||||||
|
point[2] = point[2] - origin[2];
|
||||||
|
|
||||||
|
// rotate around x
|
||||||
|
var x1 = point[0];
|
||||||
|
var y1 = point[1] * Math.cos(x) - point[2] * Math.sin(x);
|
||||||
|
var z1 = point[1] * Math.sin(x) + point[2] * Math.cos(x);
|
||||||
|
|
||||||
|
// rotate around y
|
||||||
|
var x2 = x1 * Math.cos(y) + z1 * Math.sin(y);
|
||||||
|
var y2 = y1;
|
||||||
|
var z2 = -x1 * Math.sin(y) + z1 * Math.cos(y);
|
||||||
|
|
||||||
|
// rotate around z
|
||||||
|
var x3 = x2 * Math.cos(z) - y2 * Math.sin(z);
|
||||||
|
var y3 = x2 * Math.sin(z) + y2 * Math.cos(z);
|
||||||
|
var z3 = z2;
|
||||||
|
|
||||||
|
return [x3, y3, z3];
|
||||||
|
}
|
||||||
|
|
||||||
|
function deg_to_rad(deg)
|
||||||
|
{
|
||||||
|
return deg * Math.PI / 180;
|
||||||
|
}
|
||||||
|
|
||||||
|
function point_to_string(point, decimal_places=3)
|
||||||
|
{
|
||||||
|
return "(x " + point[0].toFixed(decimal_places) + ", y " + point[1].toFixed(decimal_places) + ", z " + point[2].toFixed(decimal_places) + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
console.log(point_to_string(rotate_point_3d(
|
||||||
|
[ 0, 0, 0 ],
|
||||||
|
[ 5, 0, 0 ],
|
||||||
|
deg_to_rad(0), // rotate around x axis = yz plane
|
||||||
|
deg_to_rad(90), // rotate around y axis = xz plane
|
||||||
|
deg_to_rad(0) // rotate around z axis = xy plane
|
||||||
|
)));
|
Loading…
Reference in a new issue