Pixel-Composer/scripts/d3d_rot3/d3d_rot3.gml

70 lines
1.6 KiB
Plaintext
Raw Normal View History

2023-08-14 19:22:04 +02:00
function __rot3(_x = 0, _y = 0, _z = 0) constructor {
x = _x;
y = _y;
z = _z;
static set = function(_x, _y, _z) {
2023-11-08 08:38:04 +01:00
INLINE
2023-08-14 19:22:04 +02:00
x = _x;
y = _y;
z = _z;
return self;
}
static toDirection = function() {
var _x = degtorad( x);
var _y = degtorad( y);
var _z = degtorad(-z);
var cosX = cos(_x);
var sinX = sin(_x);
var cosY = cos(_y);
var sinY = sin(_y);
var cosZ = cos(_z);
var sinZ = sin(_z);
2024-02-10 07:03:50 +01:00
var rotationMatrix = new __mat3([ cosY * cosZ, -cosX * sinZ + sinX * sinY * cosZ, sinX * sinZ + cosX * sinY * cosZ,
cosY * sinZ, cosX * cosZ + sinX * sinY * sinZ, -sinX * cosZ + cosX * sinY * sinZ,
-sinY, sinX * cosY, cosX * cosY ]);
2023-08-14 19:22:04 +02:00
var initialVector = new __vec3(1, 0, 0);
var rotatedVector = rotationMatrix.multiplyVector(initialVector);
rotatedVector.z *= -1;
return rotatedVector;
}
static lookAt = function(from, to, up = __vec3_up) {
var dir = to.subtract(from)._normalize();
var az = arctan2(dir.y, dir.x);
var ay = arcsin(dir.z);
var w0 = new __vec3( -dir.y, dir.x, 0);
var u0 = w0.cross(dir);
var ax = arctan2( w0.dot(up) / w0.length(), u0.dot(up) / u0.length() );
if(is_nan(ax)) ax = 0;
2023-08-14 19:22:04 +02:00
ax = radtodeg(ax);
ay = radtodeg(ay);
az = radtodeg(az);
set(ax, -ay, -az);
return self;
}
static equal = function(to) {
2023-11-08 08:38:04 +01:00
INLINE
2023-08-14 19:22:04 +02:00
return x == to.x && y == to.y && z == to.z;
}
static clone = function() {
2023-11-08 08:38:04 +01:00
INLINE
2023-08-14 19:22:04 +02:00
return new __rot3(x, y, z);
}
static toString = function() { return $"[__rot3] ({x}, {y}, {z})"; }
2023-08-15 19:35:31 +02:00
static toArray = function() { return [ x, y, z ]; }
2023-08-14 19:22:04 +02:00
}