@needle-tools/engine
Version:
Needle Engine is a web-based runtime for 3D apps. It runs on your machine for development with great integrations into editors like Unity or Blender - and can be deployed onto any device! It is flexible, extensible and networking and XR are built-in.
53 lines (39 loc) • 1.48 kB
text/typescript
import { Vector3 } from "three";
import { serializable } from "../engine/engine_serialization.js";
import { Behaviour } from "./Component.js";
import { Rigidbody } from "./RigidBody.js";
export abstract class Joint extends Behaviour {
connectedBody?: Rigidbody;
get rigidBody(): Rigidbody | null {
return this._rigidBody;
}
private _rigidBody: Rigidbody | null = null;
onEnable() {
if (!this._rigidBody) this._rigidBody = this.gameObject.getComponent(Rigidbody);
if (this.rigidBody && this.connectedBody)
this.startCoroutine(this.create());
}
private *create() {
yield;
if (this.rigidBody && this.connectedBody && this.activeAndEnabled) {
this.createJoint(this.rigidBody, this.connectedBody)
}
}
protected abstract createJoint(self: Rigidbody, other: Rigidbody);
}
export class FixedJoint extends Joint {
protected createJoint(self: Rigidbody, other: Rigidbody) {
this.context.physics.engine?.addFixedJoint(self, other);
}
}
export class HingeJoint extends Joint {
anchor?: Vector3;
axis?: Vector3;
protected createJoint(self: Rigidbody, other: Rigidbody) {
if (this.axis && this.anchor)
this.context.physics.engine?.addHingeJoint(self, other, this.anchor, this.axis);
}
}