
Rotate Around Point
Downloads: 41
Download Description
Rotates an object around a point
//
// RotateAroundPoint.ts
// Rotates an object around a point
// Written by @Fusedthreed – https://FusedThreeD.com
//
@customNode()
export class RotateAroundPoint extends BasicScriptNode {
@input()
transform: APJS.Transform;
@input()
center: APJS.Vector3f;
@input()
radius: number = 32;
@input()
stepDegreesPerFrame: number = 1;
@input()
use3D: boolean = true;
@input()
axis: APJS.Vector3f = new APJS.Vector3f(0, 1, 0);
@input()
pixelsPerUnit: number = 32;
@input()
flip: boolean;
@input()
flipSpeed: number = 5;
@input()
startAngleDegrees: number = 0;
@input()
reset:boolean = false;
private angle: number = 0;
private flipAngle: number = 0;
execute(): void {
if(this.reset === true)
{
this.angle =0;
this.flipAngle = 0;
return;
}
this.angle += this.stepDegreesPerFrame;
const totalAngle = this.startAngleDegrees + this.angle;
const radians = totalAngle * Math.PI / 180;
const radiusWorld = this.radius / this.pixelsPerUnit;
const newPos = new APJS.Vector3f(0, 0, 0);
if (this.use3D) {
if (this.axis.y === 1) {
newPos.x = this.center.x + Math.cos(radians) * radiusWorld;
newPos.y = this.center.y;
newPos.z = this.center.z + Math.sin(radians) * radiusWorld;
} else if (this.axis.x === 1) {
newPos.x = this.center.x;
newPos.y = this.center.y + Math.cos(radians) * radiusWorld;
newPos.z = this.center.z + Math.sin(radians) * radiusWorld;
} else if (this.axis.z === 1) {
newPos.x = this.center.x + Math.cos(radians) * radiusWorld;
newPos.y = this.center.y + Math.sin(radians) * radiusWorld;
newPos.z = this.center.z;
}
} else {
newPos.x = this.center.x + Math.cos(radians) * radiusWorld;
newPos.y = this.center.y + Math.sin(radians) * radiusWorld;
newPos.z = this.center.z;
}
this.transform.localPosition = newPos;
let finalAngle = totalAngle;
if (this.flip) {
this.flipAngle += this.flipSpeed;
finalAngle += this.flipAngle;
}
const finalRadians = finalAngle * Math.PI / 180;
const half = finalRadians * 0.5;
this.transform.localRotation = new APJS.Quaternionf(
0,
0,
Math.sin(half),
Math.cos(half)
);
}
}