UNPKG

@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.

57 lines 2.38 kB
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 { serializable } from "../../engine/engine_serialization_decorator.js"; import { Behaviour } from "../Component.js"; import { Rigidbody } from "../RigidBody.js"; /** * Used to attract Rigidbodies towards the position of this component. * Add Rigidbodies to the `targets` array to have them be attracted. * You can use negative strength values to create a repulsion effect. * * @example Attractor component attracting a Rigidbody * ```ts * const attractor = object.addComponent(Attractor); * attractor.strength = 5; // positive value to attract * attractor.radius = 10; // only attract within 10 units * attractor.targets.push(rigidbody); // add the Rigidbody to be attracted * @summary Attract Rigidbodies towards the position of this component * @category Physics * @group Components */ export class Attractor extends Behaviour { strength = 1; radius = 2; targets = []; update() { const wp = this.gameObject.worldPosition; const factor = -this.strength * this.context.time.deltaTime; this.targets?.forEach(t => { if (!t) return; const dir = t.gameObject.worldPosition.sub(wp); const length = dir.length(); if (length > this.radius) return; let _factor = factor; if (length > 1) _factor /= length * length; else _factor /= Math.max(.05, length); t.applyImpulse(dir.multiplyScalar(_factor)); }); } } __decorate([ serializable() ], Attractor.prototype, "strength", void 0); __decorate([ serializable() ], Attractor.prototype, "radius", void 0); __decorate([ serializable(Rigidbody) ], Attractor.prototype, "targets", void 0); //# sourceMappingURL=Attractor.js.map