UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

112 lines (111 loc) 4.12 kB
"use strict"; import { CorePhysicsAttribute, physicsAttribNameLive, PhysicsRBDRadiusAttribute, PhysicsRBDHeightAttribute } from "../PhysicsAttribute"; import { _getRBDFromObject } from "../PhysicsRBD"; import { touchRBDProperty } from "../../reactivity/RBDPropertyReactivity"; import { coreObjectClassFactory } from "../../geometry/CoreObjectFactory"; export var RBDCommonProperty = /* @__PURE__ */ ((RBDCommonProperty2) => { RBDCommonProperty2["RADIUS"] = "radius"; RBDCommonProperty2["HEIGHT"] = "height"; return RBDCommonProperty2; })(RBDCommonProperty || {}); const attributeHeightLive = physicsAttribNameLive(PhysicsRBDHeightAttribute.HEIGHT); const attributeRadiusLive = physicsAttribNameLive(PhysicsRBDRadiusAttribute.RADIUS); export function currentHeight(object, collider) { const coreObjectClass = coreObjectClassFactory(object); let _currentHeight = coreObjectClass.attribValue(object, attributeHeightLive); if (_currentHeight == null) { const shape = collider.shape; _currentHeight = shape.halfHeight * 2; coreObjectClass.setAttribute(object, attributeHeightLive, _currentHeight); } return _currentHeight; } export function currentRadius(object, collider) { const coreObjectClass = coreObjectClassFactory(object); let _currentRadius = coreObjectClass.attribValue(object, attributeRadiusLive); if (_currentRadius == null) { const shape = collider.shape; _currentRadius = shape.radius; coreObjectClass.setAttribute(object, attributeRadiusLive, _currentRadius); } return _currentRadius; } export function getPhysicsRBDHeight(expectedType, object) { const body = _getRBDFromObject(object); if (!body) { console.warn("no rbd found"); return; } const colliderType = CorePhysicsAttribute.getColliderType(object); if (colliderType == null || colliderType != expectedType) { return; } const collider = body.collider(0); if (!collider) { return; } return currentHeight(object, collider); } export function getPhysicsRBDRadius(expectedType, object) { const body = _getRBDFromObject(object); if (!body) { console.warn("no rbd found"); return; } const colliderType = CorePhysicsAttribute.getColliderType(object); if (colliderType == null) { console.warn("no colliderType found"); return; } if (colliderType != expectedType) { console.warn(`colliderType '${colliderType}' not the expected one ('${expectedType}')`); return; } const collider = body.collider(0); if (!collider) { console.warn("no collider found"); return; } return currentRadius(object, collider); } export function setPhysicsRBDHeightRadiusProperty(expectedType, object, targetRadius, targetHeight, lerp, updateObjectMatrix) { const body = _getRBDFromObject(object); if (!body) { console.warn("no rbd found"); return; } const colliderType = CorePhysicsAttribute.getColliderType(object); if (colliderType == null || colliderType != expectedType) { return; } const collidersCount = body.numColliders(); const originalHeightAttrib = CorePhysicsAttribute.getHeight(object); const originalRadiusAttrib = CorePhysicsAttribute.getRadius(object); for (let i = 0; i < collidersCount; i++) { const collider = body.collider(i); if (!collider) { return; } if (lerp < 1) { targetHeight = lerp * targetHeight + (1 - lerp) * currentHeight(object, collider); targetRadius = lerp * targetRadius + (1 - lerp) * currentRadius(object, collider); } collider.setHalfHeight(targetHeight * 0.5); collider.setRadius(targetRadius); const coreObjectClass = coreObjectClassFactory(object); coreObjectClass.setAttribute(object, attributeHeightLive, targetHeight); coreObjectClass.setAttribute(object, attributeRadiusLive, targetRadius); touchRBDProperty(object, "height" /* HEIGHT */); touchRBDProperty(object, "radius" /* RADIUS */); const scaleXZ = targetRadius / originalRadiusAttrib; object.scale.set(scaleXZ, targetHeight / originalHeightAttrib, scaleXZ); if (updateObjectMatrix) { object.updateMatrix(); } } }