UNPKG

oxygen-core

Version:

Oxygen game engine (Xenon Core for browsers)

911 lines (776 loc) 38.5 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base data-ice="baseUrl" href="../../../"> <title data-ice="title">src/components/PhysicsBody.js | oxygen-core</title> <link type="text/css" rel="stylesheet" href="css/style.css"> <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> <script src="script/prettify/prettify.js"></script> <script src="script/manual.js"></script> <meta name="description" content="Oxygen game engine (Xenon Core for browsers)"><meta property="twitter:card" content="summary"><meta property="twitter:title" content="oxygen-core"><meta property="twitter:description" content="Oxygen game engine (Xenon Core for browsers)"></head> <body class="layout-container" data-ice="rootContainer"> <header> <a href="./">Home</a> <a href="identifiers.html">Reference</a> <a href="source.html">Source</a> <div class="search-box"> <span> <img src="./image/search.png"> <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> </span> <ul class="search-result"></ul> </div> <a style="position:relative; top:3px;" href="https://github.com/PsichiX/Oxygen"><img width="20px" src="./image/github.png"></a></header> <nav class="navigation" data-ice="nav"><div> <ul> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/index.js~EventsController.html">EventsController</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-lazyInitialization">lazyInitialization</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#asset-loaders">asset-loaders</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/AssemblyAsset.js~AssemblyAsset.html">AssemblyAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/AtlasAsset.js~AtlasAsset.html">AtlasAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/BinaryAsset.js~BinaryAsset.html">BinaryAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/FontAsset.js~FontAsset.html">FontAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/ImageAsset.js~ImageAsset.html">ImageAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/JSONAsset.js~JSONAsset.html">JSONAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/MusicAsset.js~MusicAsset.html">MusicAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/PackAsset.js~PackAsset.html">PackAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/ParticleSystemAsset.js~ParticleSystemAsset.html">ParticleSystemAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/PostprocessRackEffectAsset.js~PostprocessRackEffectAsset.html">PostprocessRackEffectAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/SVGAsset.js~SVGAsset.html">SVGAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/SceneAsset.js~SceneAsset.html">SceneAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/SetAsset.js~SetAsset.html">SetAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/ShaderAsset.js~ShaderAsset.html">ShaderAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/SkeletonAsset.js~SkeletonAsset.html">SkeletonAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/SoundAsset.js~SoundAsset.html">SoundAsset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asset-loaders/TextAsset.js~TextAsset.html">TextAsset</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#components">components</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/AtlasSprite.js~AtlasSprite.html">AtlasSprite</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Camera.js~Camera.html">Camera</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Camera.js~PostprocessPass.html">PostprocessPass</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Camera2D.js~Camera2D.html">Camera2D</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/CameraDirector2D.js~CameraDirector2D.html">CameraDirector2D</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/CircleShape.js~CircleShape.html">CircleShape</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Container.js~Container.html">Container</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/DeferredRenderer.js~DeferredPipeline.html">DeferredPipeline</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/DeferredRenderer.js~DeferredRenderer.html">DeferredRenderer</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/GestureListener.js~GestureListener.html">GestureListener</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/InputHandler.js~InputHandler.html">InputHandler</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/MultipassRenderer.js~MultipassPipeline.html">MultipassPipeline</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/MultipassRenderer.js~MultipassRenderer.html">MultipassRenderer</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Particles.js~Particles.html">Particles</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/PhysicsBody.js~PhysicsBody.html">PhysicsBody</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/PhysicsWorld.js~PhysicsWorld.html">PhysicsWorld</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/PolygonShape.js~PolygonShape.html">PolygonShape</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/PostprocessRack.js~PostprocessRack.html">PostprocessRack</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/PostprocessRack.js~PostprocessRackPass.html">PostprocessRackPass</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/PostprocessRack.js~PostprocessRackRawEffectPass.html">PostprocessRackRawEffectPass</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/PrefabInstance.js~PrefabInstance.html">PrefabInstance</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/RectangleRenderer.js~RectangleRenderer.html">RectangleRenderer</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/RectangleShape.js~RectangleShape.html">RectangleShape</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Script.js~Script.html">Script</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Shape.js~Shape.html">Shape</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Skeleton.js~Skeleton.html">Skeleton</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/SortedActions.js~SortedActions.html">SortedActions</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/Sprite.js~Sprite.html">Sprite</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/TextRenderer.js~TextRenderer.html">TextRenderer</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/UiLayout.js~UiLayout.html">UiLayout</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/UiSprite.js~UiSprite.html">UiSprite</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/components/VerticesRenderer.js~VerticesRenderer.html">VerticesRenderer</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#systems">systems</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/AssemblySystem.js~AssemblySystem.html">AssemblySystem</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/AudioSystem.js~AudioSystem.html">AudioSystem</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/InputSystem.js~InputSystem.html">InputSystem</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/RenderSystem.js~Command.html">Command</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/RenderSystem.js~Pipeline.html">Pipeline</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/RenderSystem.js~RenderFullscreenCommand.html">RenderFullscreenCommand</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/RenderSystem.js~RenderSystem.html">RenderSystem</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/RenderSystem.js~RenderTargetWrapper.html">RenderTargetWrapper</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/StorageSystem.js~StorageSystem.html">StorageSystem</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/System.js~System.html">System</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#systems-assetsystem">systems/AssetSystem</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/AssetSystem/Asset.js~Asset.html">Asset</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/AssetSystem/index.js~AssetSystem.html">AssetSystem</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#systems-entitysystem">systems/EntitySystem</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/EntitySystem/Component.js~Component.html">Component</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/EntitySystem/Entity.js~Entity.html">Entity</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/systems/EntitySystem/index.js~EntitySystem.html">EntitySystem</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#utils">utils</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/utils/Events.js~Events.html">Events</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-angleDifference">angleDifference</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-bezierCubic">bezierCubic</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-convertGlobalPointToLocalPoint">convertGlobalPointToLocalPoint</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-convertLocalPointToGlobalPoint">convertLocalPointToGlobalPoint</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-findMapKeyOfValue">findMapKeyOfValue</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getMipmapScale">getMipmapScale</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getPOT">getPOT</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isGlobalPointInGlobalBoundingBox">isGlobalPointInGlobalBoundingBox</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isLocalPointInLocalBoundingBox">isLocalPointInLocalBoundingBox</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isPOT">isPOT</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-propsEnumStringify">propsEnumStringify</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-stringToRGBA">stringToRGBA</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-waitForSeconds">waitForSeconds</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Box2D">Box2D</a></span></span></li> </ul> </div> </nav> <div class="content" data-ice="content"><h1 data-ice="title">src/components/PhysicsBody.js</h1> <pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import Script from &apos;./Script&apos;; import { b2Vec2, b2BodyDef, b2FixtureDef, b2CircleShape, b2PolygonShape, b2Body, b2Fixture } from &apos;../utils/box2d&apos;; const ShapeType = { CIRCLE: &apos;circle&apos;, POLYGON: &apos;polygon&apos; }; const BodyType = { STATIC: &apos;static&apos;, DYNAMIC: &apos;dynamic&apos;, KINEMATIC: &apos;kinematic&apos; }; export default class PhysicsBody extends Script { static factory() { return new PhysicsBody(); } static get propsTypes() { return { ...Script.propsTypes, applyPositionTo: &apos;string_null&apos;, applyRotationTo: &apos;string_null&apos;, applyPositionFrom: &apos;string_null&apos;, applyRotationFrom: &apos;string_null&apos;, bodyType: &apos;enum(static, dynamic, kinematic)&apos;, radius: &apos;number&apos;, vertices: &apos;array(number)&apos;, shapeType: &apos;enum(circle, polygon)&apos;, density: &apos;number&apos;, friction: &apos;number&apos;, restitution: &apos;number&apos;, linearVelocity: &apos;vec2&apos;, angularVelocity: &apos;number&apos;, linearDamping: &apos;number&apos;, angularDamping: &apos;number&apos;, fixedRotation: &apos;boolean&apos;, bullet: &apos;boolean&apos;, sensor: &apos;boolean&apos;, coordsScale: &apos;number&apos;, listenForContacts: &apos;boolean&apos; }; } static get ShapeType() { return ShapeType; } get applyPositionTo() { return this._applyPositionTo; } set applyPositionTo(value) { if (!value) { this._applyPositionTo = null; this._applyPositionToEntity = null; return; } if (typeof value !== &apos;string&apos;) { throw new Error(&apos;`value` is not type of String!&apos;); } const { entity } = this; this._applyPositionTo = value; if (!!entity) { this._applyPositionToEntity = entity.findEntity(value); if (!this._applyPositionToEntity) { throw new Error(`Cannot find entity: ${value}`); } } } get applyRotationTo() { return this._applyRotationTo; } set applyRotationTo(value) { if (!value) { this._applyRotationTo = null; this._applyRotationToEntity = null; return; } if (typeof value !== &apos;string&apos;) { throw new Error(&apos;`value` is not type of String!&apos;); } const { entity } = this; this._applyRotationTo = value; if (!!entity) { this._applyRotationToEntity = entity.findEntity(value); if (!this._applyRotationToEntity) { throw new Error(`Cannot find entity: ${value}`); } } } get applyPositionFrom() { return this._applyPositionFrom; } set applyPositionFrom(value) { if (!value) { this._applyPositionFrom = null; this._applyPositionFromEntity = null; return; } if (typeof value !== &apos;string&apos;) { throw new Error(&apos;`value` is not type of String!&apos;); } const { entity } = this; this._applyPositionFrom = value; if (!!entity) { this._applyPositionFromEntity = entity.findEntity(value); if (!this._applyPositionFromEntity) { throw new Error(`Cannot find entity: ${value}`); } } } get applyRotationFrom() { return this._applyRotationFrom; } set applyRotationFrom(value) { if (!value) { this._applyRotationFrom = null; this._applyRotationFromEntity = null; return; } if (typeof value !== &apos;string&apos;) { throw new Error(&apos;`value` is not type of String!&apos;); } const { entity } = this; this._applyRotationFrom = value; if (!!entity) { this._applyRotationFromEntity = entity.findEntity(value); if (!this._applyRotationFromEntity) { throw new Error(`Cannot find entity: ${value}`); } } } get bodyType() { return this._bodyType; } set bodyType(value) { if (typeof value !== &apos;string&apos;) { throw new Error(&apos;`value` is not type of String!&apos;); } this._bodyType = value; this._rebuild = true; } get radius() { return this._radius; } set radius(value) { if (typeof value !== &apos;number&apos;) { throw new Error(&apos;`value` is not type of Number!&apos;); } this._radius = value; } get vertices() { return this._vertices; } set vertices(value) { if (!value) { throw new Error(&apos;`value` cannot be null!&apos;); } if (value instanceof Array) { value = new Float32Array(value); } if (!(value instanceof Float32Array)) { throw new Error(&apos;`value` is not type of either Array or Float32Array!&apos;); } if (value.length % 2 !== 0) { throw new Error(&apos;`value` has size undividable by 2!&apos;); } this._vertices = value; } get shapeType() { return this._shapeType; } set shapeType(value) { if (typeof value !== &apos;string&apos;) { throw new Error(&apos;`value` is not type of String!&apos;); } this._shapeType = value; this._rebuild = true; } get density() { const { _fixture } = this; return !!_fixture ? _fixture.GetDensity() : this._fixtureDef.density; } set density(value) { if (typeof value !== &apos;number&apos;) { throw new Error(&apos;`value` is not type of Number!&apos;); } const { _fixture, _body, density } = this; if (!!_fixture) { _fixture.SetDensity(value); } else { this._fixtureDef.density = value; } if (!!_body &amp;&amp; density !== value) { _body.ResetMassData(); } } get friction() { const { _fixture } = this; return !!_fixture ? _fixture.GetFriction() : this._fixtureDef.friction; } set friction(value) { if (typeof value !== &apos;number&apos;) { throw new Error(&apos;`value` is not type of Number!&apos;); } const { _fixture } = this; if (!!_fixture) { _fixture.SetFriction(value); } else { this._fixtureDef.friction = value; } } get restitution() { const { _fixture } = this; return !!_fixture ? _fixture.GetRestitution() : this._fixtureDef.restitution; } set restitution(value) { if (typeof value !== &apos;number&apos;) { throw new Error(&apos;`value` is not type of Number!&apos;); } const { _fixture } = this; if (!!_fixture) { _fixture.SetRestitution(value); } else { this._fixtureDef.restitution = value; } } get linearVelocity() { const { _body } = this; return !!_body ? _body.GetLinearVelocity() : this._bodyDef.linearVelocity; } set linearVelocity(value) { if (value instanceof Array || value instanceof Float32Array) { value = new b2Vec2(value[0] || 0, value[1] || 0); } else { throw new Error(&apos;`value` is not type of either b2Vec2, Array or Float32Array!&apos;); } const { _body } = this; if (!!_body) { _body.SetLinearVelocity(value); } else { this._bodyDef.linearVelocity = value; } } get linearVelocityLength() { return this.linearVelocity.Length(); } get angularVelocity() { const { _body } = this; return !!_body ? _body.GetAngularVelocity() : this._bodyDef.angularVelocity; } set angularVelocity(value) { if (typeof value !== &apos;number&apos;) { throw new Error(&apos;`value` is not type of Number!&apos;); } const { _body } = this; if (!!_body) { _body.SetAngularVelocity(value); } else { this._bodyDef.angularVelocity = value; } } get linearDamping() { const { _body } = this; return !!_body ? _body.GetLinearDamping() : this._bodyDef.linearDamping; } set linearDamping(value) { if (typeof value !== &apos;number&apos;) { throw new Error(&apos;`value` is not type of Number!&apos;); } const { _body } = this; if (!!_body) { _body.SetLinearDamping(value); } else { this._bodyDef.linearDamping = value; } } get angularDamping() { const { _body } = this; return !!_body ? _body.GetAngularDamping() : this._bodyDef.angularDamping; } set angularDamping(value) { if (typeof value !== &apos;number&apos;) { throw new Error(&apos;`value` is not type of Number!&apos;); } const { _body } = this; if (!!_body) { _body.SetAngularDamping(value); } else { this._bodyDef.angularDamping = value; } } get fixedRotation() { const { _body } = this; return !!_body ? _body.IsFixedRotation() : this._bodyDef.fixedRotation; } set fixedRotation(value) { if (typeof value !== &apos;boolean&apos;) { throw new Error(&apos;`value` is not type of Boolean!&apos;); } const { _body } = this; if (!!_body) { _body.SetFixedRotation(value); } else { this._bodyDef.fixedRotation = value; } } get bullet() { const { _body } = this; return !!_body ? _body.IsBullet() : this._bodyDef.bullet; } set bullet(value) { if (typeof value !== &apos;boolean&apos;) { throw new Error(&apos;`value` is not type of Boolean!&apos;); } const { _body } = this; if (!!_body) { _body.SetBullet(value); } else { this._bodyDef.bullet = value; } } get sensor() { const { _fixture } = this; return !!_fixture ? _fixture.IsSensor() : this._fixtureDef.isSensor; } set sensor(value) { if (typeof value !== &apos;boolean&apos;) { throw new Error(&apos;`value` is not type of Boolean!&apos;); } const { _fixture } = this; if (!!_fixture) { _fixture.SetSensor(value); } else { this._fixtureDef.isSensor = value; } } get coordsScale() { return this._coordsScale; } set coordsScale(value) { if (typeof value !== &apos;number&apos;) { throw new Error(&apos;`value` is not type of Number!&apos;); } this._coordsScale = value; } get listenForContacts() { return this._listenForContacts; } set listenForContacts(value) { if (typeof value !== &apos;boolean&apos;) { throw new Error(&apos;`value` is not type of Boolean!&apos;); } this._listenForContacts = value; } get body() { return this._body; } get world() { return this._world; } get fixture() { return this._fixture; } get mass() { const { _body } = this; return !!_body ? _body.GetMass() : 0; } constructor() { super(); this._applyPositionTo = null; this._applyPositionToEntity = null; this._applyRotationTo = null; this._applyRotationToEntity = null; this._applyPositionFrom = null; this._applyPositionFromEntity = null; this._applyRotationFrom = null; this._applyRotationFromEntity = null; this._bodyType = BodyType.STATIC; this._radius = 1; this._coordsScale = 1; this._listenForContacts = false; this._vertices = null; this._shapeType = ShapeType.CIRCLE; this._bodyDef = new b2BodyDef(); this._fixtureDef = new b2FixtureDef(); this._world = null; this._body = null; this._fixture = null; this._shape = null; this._rebuild = true; } dispose() { super.dispose(); this._destroyBody(); this._applyPositionToEntity = null; this._applyRotationToEntity = null; this._applyPositionFromEntity = null; this._applyRotationFromEntity = null; this._bodyDef = null; this._fixtureDef = null; this._vertices = null; this._world = null; this._body = null; this._fixture = null; this._shape = null; } rebuildBody() { this._rebuild = true; } applyForce(forceX, forceY, pointX, pointY, localSpace = false) { if (typeof forceX !== &apos;number&apos;) { throw new Error(&apos;`forceX` is not type of Number!&apos;); } if (typeof forceY !== &apos;number&apos;) { throw new Error(&apos;`forceY` is not type of Number!&apos;); } if (typeof pointX !== &apos;number&apos;) { throw new Error(&apos;`pointX` is not type of Number!&apos;); } if (typeof pointY !== &apos;number&apos;) { throw new Error(&apos;`pointY` is not type of Number!&apos;); } if (typeof localSpace !== &apos;boolean&apos;) { throw new Error(&apos;`localSpace` is not type of Boolean!&apos;); } const { _body } = this; if (!!_body) { if (localSpace) { const localPoint = _body.GetLocalPoint(new b2Vec2(pointX, pointY)); _body.ApplyForce( new b2Vec2(forceX, forceY), new b2Vec2(localPoint.x, localPoint.y) ); } else { _body.ApplyForce( new b2Vec2(forceX, forceY), new b2Vec2(pointX, pointY) ); } } } applyTorque(value) { if (typeof value !== &apos;number&apos;) { throw new Error(&apos;`value` is not type of Number!&apos;); } const { _body } = this; if (!!_body) { _body.ApplyTorque(value); } } applyImpulse(forceX, forceY, pointX, pointY, localSpace = false) { if (typeof forceX !== &apos;number&apos;) { throw new Error(&apos;`forceX` is not type of Number!&apos;); } if (typeof forceY !== &apos;number&apos;) { throw new Error(&apos;`forceY` is not type of Number!&apos;); } if (typeof pointX !== &apos;number&apos;) { throw new Error(&apos;`pointX` is not type of Number!&apos;); } if (typeof pointY !== &apos;number&apos;) { throw new Error(&apos;`pointY` is not type of Number!&apos;); } if (typeof localSpace !== &apos;boolean&apos;) { throw new Error(&apos;`localSpace` is not type of Boolean!&apos;); } const { _body } = this; if (!!_body) { if (localSpace) { const localPoint = _body.GetLocalPoint(new b2Vec2(pointX, pointY)); _body.ApplyImpulse( new b2Vec2(forceX, forceY), new b2Vec2(localPoint.x, localPoint.y) ); } else { _body.ApplyImpulse( new b2Vec2(forceX, forceY), new b2Vec2(pointX, pointY) ); } } } wakeUp() { const { _body } = this; if (!!_body) { _body.SetAwake(true); } } sleep() { const { _body } = this; if (!!_body) { _body.SetAwake(false); } } onAttach() { this.applyPositionTo = this.applyPositionTo; this.applyRotationTo = this.applyRotationTo; this.applyPositionFrom = this.applyPositionFrom; this.applyRotationFrom = this.applyRotationFrom; this._createBody(); } onDetach() { this._destroyBody(); } onPropertySerialize(name, value) { if (name === &apos;vertices&apos;) { if (!value) { return; } return [ ...value ]; } if (name === &apos;linearVelocity&apos;) { if (!value) { return; } return [ value.x, value.y ]; } else { return super.onPropertySerialize(name, value); } } onUpdate(deltaTime) { if (this._rebuild) { this._destroyBody(); this._createBody(); this._rebuild = false; } const { _body } = this; if (!_body) { return; } const { _applyPositionToEntity, _applyRotationToEntity, _applyPositionFromEntity, _applyRotationFromEntity, _coordsScale } = this; if (!!_applyPositionToEntity) { const position = _body.GetPosition(); _applyPositionToEntity.setPosition( position.x * _coordsScale, position.y * _coordsScale ); } else if (!!_applyPositionFromEntity) { const position = _applyPositionFromEntity.position; _body.SetPosition(new b2Vec2( position[0] / _coordsScale, position[1] / _coordsScale )); } if (!!_applyRotationToEntity) { _applyRotationToEntity.setRotation(_body.GetAngle()); } else if (!!_applyRotationFromEntity) { _body.SetAngle(_applyRotationFromEntity.getRotation()); } } _findParentWorld() { let entity = this.entity; while (!!entity) { const world = entity.getComponent(&apos;PhysicsWorld&apos;); if (!!world) { return world; } entity = entity.parent; } return null; } _createBody() { const world = this._findParentWorld(); if (!world) { throw new Error(&apos;Cannot find PhysicsWorld component in parents chain!&apos;); } const { _bodyType, _shapeType, _bodyDef, _fixtureDef, _coordsScale, _vertices, entity } = this; const { position } = entity; if (_bodyType === BodyType.STATIC) { _bodyDef.type = b2Body.b2_staticBody; } else if (_bodyType === BodyType.DYNAMIC) { _bodyDef.type = b2Body.b2_dynamicBody; } else if (_bodyType === BodyType.KINEMATIC) { _bodyDef.type = b2Body.b2_kinematicBody; } else { console.warn(`Wrong body type: ${_bodyType}`); return; } if (_shapeType === ShapeType.POLYGON) { this._shape = new b2PolygonShape(); const vertices = []; for (let i = 0, c = _vertices.length; i &lt; c; i += 2) { vertices.push(new b2Vec2(_vertices[i], _vertices[i + 1])); } this._shape.SetAsVector(vertices); } else if (_shapeType === ShapeType.CIRCLE) { this._shape = new b2CircleShape(this._radius); } else { console.warn(`Wrong shape type: ${_shapeType}`); return; } const scale = _coordsScale &gt; 0 ? 1 / _coordsScale : 0; _bodyDef.position = new b2Vec2( position[0] * scale, position[1] * scale ); _bodyDef.angle = entity.getRotation(); _fixtureDef.shape = this._shape; const body = this._body = world.world.CreateBody(_bodyDef); if (!body) { throw new Error( `Could not create body for PhysicsBody of entity: ${entity.name}` ); } body.SetUserData(this); const fixture = this._fixture = body.CreateFixture(_fixtureDef); fixture.SetUserData(this); this._world = world; } _destroyBody() { const { _bodyDef, _fixtureDef, _body, _fixture } = this; if (!!_fixture) { _fixtureDef.shape = null; _fixtureDef.density = _fixture.GetDensity(); _fixtureDef.friction = _fixture.GetFriction(); _fixtureDef.restitution = _fixture.GetRestitution(); _fixtureDef.isSensor = _fixture.IsSensor(); } if (!!_body) { _bodyDef.type = _body.GetType(); _bodyDef.linearVelocity = _body.GetLinearVelocity(); _bodyDef.linearDamping = _body.GetLinearDamping(); _bodyDef.angularVelocity = _body.GetAngularVelocity(); _bodyDef.angularDamping = _body.GetAngularDamping(); _bodyDef.fixedRotation = _body.IsFixedRotation(); _bodyDef.bullet = _body.IsBullet(); _body.DestroyFixture(_fixture); _body.GetWorld().DestroyBody(_body); } this._fixture = null; this._body = null; this._world = null; } } </code></pre> </div> <footer class="footer"> Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.1.0)</span><img src="./image/esdoc-logo-mini-black.png"></a> </footer> <script src="script/search_index.js"></script> <script src="script/search.js"></script> <script src="script/pretty-print.js"></script> <script src="script/inherited-summary.js"></script> <script src="script/test-summary.js"></script> <script src="script/inner-link.js"></script> <script src="script/patch-for-local.js"></script> </body> </html>