@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.
115 lines • 4.14 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Vector3 } from "three";
import { serializable } from "../engine/engine_serialization.js";
import { Behaviour } from "./Component.js";
import { Rigidbody } from "./RigidBody.js";
/**
* Base class for physics joints that connect two {@link Rigidbody} components.
* Joints constrain how two bodies can move relative to each other.
*
* The joint is created between:
* - The {@link Rigidbody} on this GameObject (automatically found)
* - The {@link connectedBody} Rigidbody you specify
*
* @summary Connect two Rigidbodies with physics constraints
* @category Physics
* @group Components
* @see {@link FixedJoint} for rigid connections
* @see {@link HingeJoint} for rotating connections
* @see {@link Rigidbody} for physics bodies
*/
export class Joint extends Behaviour {
/** The other Rigidbody to connect to */
connectedBody;
get rigidBody() {
return this._rigidBody;
}
_rigidBody = null;
onEnable() {
if (!this._rigidBody)
this._rigidBody = this.gameObject.getComponent(Rigidbody);
if (this.rigidBody && this.connectedBody)
this.startCoroutine(this.create());
}
*create() {
yield;
if (this.rigidBody && this.connectedBody && this.activeAndEnabled) {
this.createJoint(this.rigidBody, this.connectedBody);
}
}
}
__decorate([
serializable(Rigidbody)
], Joint.prototype, "connectedBody", void 0);
/**
* FixedJoint locks two {@link Rigidbody} components together, making them move as one rigid unit.
* The bodies maintain their relative position and rotation at the time the joint is created.
*
* Use this for:
* - Attaching objects together permanently
* - Creating compound rigid bodies
* - Welding broken pieces back together
*
* @example Attach a weapon to a character
* ```ts
* const joint = weapon.addComponent(FixedJoint);
* joint.connectedBody = characterRigidbody;
* ```
*
* @summary Lock two Rigidbodies together rigidly
* @category Physics
* @group Components
* @see {@link Joint} base class
* @see {@link HingeJoint} for rotating connections
*/
export class FixedJoint extends Joint {
createJoint(self, other) {
this.context.physics.engine?.addFixedJoint(self, other);
}
}
/**
* HingeJoint connects two {@link Rigidbody} components with a rotating constraint,
* like a door hinge or wheel axle. Bodies can only rotate around the specified axis.
*
* Use this for:
* - Doors and gates
* - Wheels and axles
* - Pendulums
* - Any rotating mechanical connection
*
* @example Create a door hinge
* ```ts
* const hinge = door.addComponent(HingeJoint);
* hinge.connectedBody = doorFrameRigidbody;
* hinge.anchor = new Vector3(0, 0, 0); // Hinge position
* hinge.axis = new Vector3(0, 1, 0); // Rotate around Y axis
* ```
*
* @summary Connect two Rigidbodies with a rotating hinge
* @category Physics
* @group Components
* @see {@link Joint} base class
* @see {@link FixedJoint} for rigid connections
*/
export class HingeJoint extends Joint {
/** Local position of the hinge pivot point */
anchor;
/** Axis of rotation for the hinge (e.g., Vector3(0,1,0) for vertical axis) */
axis;
createJoint(self, other) {
if (this.axis && this.anchor)
this.context.physics.engine?.addHingeJoint(self, other, this.anchor, this.axis);
}
}
__decorate([
serializable(Vector3)
], HingeJoint.prototype, "anchor", void 0);
__decorate([
serializable(Vector3)
], HingeJoint.prototype, "axis", void 0);
//# sourceMappingURL=Joints.js.map