Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
Feature/ammo driver (#125)
Browse files Browse the repository at this point in the history
* WIP implementation of ammo-driver.js

* adding support for convex hulls and trimeshes

* Add support for all (most) shape types

* emit events when objects begin/end collision

* basic implementation of all ammo constraint types. expose collisionFlags in ammo-body. expose debugDrawMode in system.

* minor fixes

* fix package and package-lock

* update package.json and package-lock.json; udpate ammo.html example to latest

* point back to latest ammo.js; update package-lock to newer npm format

* pull changes over from mozillareality#hubs/ammo_driver branch

* Revert "pull changes over from mozillareality#hubs/ammo_driver branch"

This reverts commit 72ec427.

* latest WIP. Fixes for properly centering mass, convex hull and trimesh generation, dynamic scaling

* support dynamic scaling

* fix package lock

* fix constraints!

* enable manually specifying halfExtents; fix checks for when type is dynamic so that kinematic objects behave properly (fixes issue where scaled kinematic objects had strange behavior with constraints)

* numerous fixes/updates including: fix for cursor scale affecting constraints strangely; fix for how halfExtents/bounding boxes are being calculated; object center of mass is now set properly; overall less usage of variable references and simplification of various areas of code

* fix constraint rotations (use btGeneric6DofConstraint instead of btFixedConstraint).

* enable usage of ammo-body without a mesh and allow setting of orientation and position offsets.

* rename detail property in collide/collide-end events

* enable changing collision filters; enable switching between kinematic, dynamic and static

* fix gravity for that start out as kinematic but change to dynamic later

* re-add Ammo cleanup

* typo

* fix issues with how transforms in trimeshes were being handled

* update default values for maxSubSteps and fixedTimeStep to the default values of Bullet; revert back to default timestep values in system.js

* actually go back to 10 for maxSubSteps

* fix package.json

* change how debug draw is initialized so that it can be changed dynamically at runtime.

* fix race condition with setting debug param in url

* fix how initializePolyhedralFeatures is called in tick so that it properly handles when debug is toggled on/off

* fix how recentering works and make it optional; refactor initBody to be more efficient;

* prettier pass on ammo-body, ammo-constraint, and ammo-driver

* fix issue with certain trimeshes not being generated correctly

* fix package.json

* update three-to-ammo

* fix issue where kinematic bodies could still be nudged around by dynamic objects; update three-to-ammo

* point to specific sha of three-to-ammo

* fix logic bug which prevented collide-end from ever firing

* performance pass: try to use activationState correctly; check if pos/rot has actually updated before updating bullet motionState; activate object if pos/rot has changed; reduce fixedTimeStep back to 60;

* expose linear/angularSleepingThreshold; move auto updating of scale and generation of shapes to beforeStep

* remove dead code

* fix issue causing eventListeners to be removed incorrectly; optimize eventing loop slightly

* replace some "let" with "const"

* various cleanup from PR review

* move activation states and collision flags into constants; change getVelocity to return the actual velocity vector instead of the scalar length; remove concept of "recenter" (let application handle it themselves using ammo-shape "offset" attribute).

* handle when collisionFlags are explicitly updated

* move shape, body, and constraint types into constants as well; add to the ammo example use of "autoGenerateShape: false"

* update to latest three-to-ammo (siginificantly more efficient shape creation)

* upgrade three-to-ammo (bug fixes)

* wip update to support refactored three-to-ammo

* update three-to-ammo (WIP branch that has the "fit" refactor)

* change ACTIVATION_STATE constants to be strings

* allow updating of activationState in update

* fix shape removal; remove ammo.js as a dependency because it's technically a peer dependency but you can also include it via script tag. latest build of ammo.js will now be located at https://mixedreality.mozilla.org/ammo.js/builds/ammo.wasm.js

* fix bug with removing shapes after the body has already been deleted. update three-to-ammo and ammo-debug-drawer packages

* reduce garbage generation by switching to using maps during collision detection; rename `addCollideEventListener` to `EmitCollisionEvents`

* fix bugs: collisionFilters not being updated correctly, static bodies not getting updated if moved around; enhancements: possible perf improvements by setting `this.physicsWorld.setForceUpdateAllAabbs(false)`

* small fix to how lock constraint limits are set; small memory optimization to collision event logic

* skip the first update that occurs in ammo-body so that various properties don't get incorrectly set twice

* update three-to-ammo with new version that supports calling updateMatrices() when available (e.g. Hubs)

* get rid of more memory allocations

* avoid iterations over maps and sets in ammo-driver step to reduce more garbage generation

* bump three-to-ammo

* Update to latest three-to-ammo
- adds support for HACD and VHACD shape types
- removes compound fit

* fix broken velocity tests

* fix tests again

* Simplify API a bit by replacing collisionFlags property with disableCollision property; explicitly expose maxSubSteps and fixedTimeStep in system for configuring the ammo-driver. revert ITERATIONS constant back to 10.

* Add documentaion for ammo-driver

* fix formatting

* add additional documentation for ammo-body activateStates

* fix table formatting

* documentation typo

* add missing System Configuration section (somehow got lost in copy pasting)

* Elaborate a bit more on collision filtering.

* fix link

* fix formatting, again

* typo

* more cleanup

* handle per-rigidBody gravity correcty. Allow gravity, damping, sleepthresholds, and angularFactor to be updated dynamically.

* fix typo where using just "data" instead of "this.data"

* fix updating gravity and activation states
  • Loading branch information
InfiniteLee authored and donmccurdy committed May 20, 2019
1 parent cbe574c commit 60b907f
Show file tree
Hide file tree
Showing 18 changed files with 2,044 additions and 138 deletions.
339 changes: 339 additions & 0 deletions AmmoDriver.md

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/donmccurdy/aframe-physics-system/master/LICENSE)
[![Build Status](https://travis-ci.org/donmccurdy/aframe-physics-system.svg?branch=master)](https://travis-ci.org/donmccurdy/aframe-physics-system)

Components for A-Frame physics integration, built on [CANNON.js](http://schteppe.github.io/cannon.js/).
Components for A-Frame physics integration.
Supports [CANNON.js](http://schteppe.github.io/cannon.js/) and [Ammo.js](https://github.com/kripken/ammo.js/)


## New Features

Ammo.js driver support has been added. Please see [Ammo Driver](/AmmoDriver.md) for documentation. CANNON.js support may be deprecated in the future.

## Contents

Expand Down
166 changes: 166 additions & 0 deletions examples/ammo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<!DOCTYPE html>
<html>
<head>
<title>Examples • AmmoDriver</title>
<meta name="description" content="Hello, WebVR! - A-Frame" />
<script src="https://aframe.io/releases/0.9.1/aframe.min.js"></script>
<script src="https://mixedreality.mozilla.org/ammo.js/builds/ammo.wasm.js"></script>
<script src="../bundle.js"></script>
<script>
AFRAME.registerComponent("bounce", {
init: function() {
this.direction = 1;
this.position = new THREE.Vector3();
this.position.copy(this.el.object3D.position);
setTimeout(() => {
this.ready = true;
}, 3000);
},

tick: function() {
if (!this.ready) return;
var position = this.el.object3D.position.y;
if (position <= 0) {
this.direction = 1;
} else if (position >= 5) {
this.direction = -1;
}
this.el.object3D.position.set(this.position.x, position + 0.05 * this.direction, this.position.z);
}
});
AFRAME.registerComponent("changescale", {
init: function() {
this.direction = 1;
setTimeout(() => {
this.ready = true;
}, 3000);
},

tick: function() {
if (!this.ready) return;
var scale = this.el.object3D.scale;
if (scale.x <= 0.05) {
this.direction = 1;
} else if (scale.x >= 1) {
this.direction = -1;
}
this.el.object3D.scale.set(
scale.x + this.direction * 0.01,
scale.y + this.direction * 0.01,
scale.z + this.direction * 0.01
);
}
});
</script>
</head>
<body>
<a-scene physics="driver: ammo; debug: true; gravity: -9.8; debugDrawMode: 1">
<a-camera near="0.001"></a-camera>
<a-box
position="-1 5 -5"
rotation="0 45 0"
color="#4CC3D9"
shadow
ammo-body
ammo-shape="type: box; halfExtents: 0.6 0.6 0.6; fit: manual;"
></a-box>
<a-box
id="target"
position="1 3.75 -4"
rotation="0 45 0"
color="purple"
shadow
ammo-body
ammo-shape="type: box;"
></a-box>
<a-sphere
position="0 10 -10"
radius="1.25"
color="#EF2D5E"
shadow
ammo-body
ammo-shape="type: sphere;"
></a-sphere>
<a-cone
position="0 3.75 -4"
radius-bottom="1.25"
color="green"
shadow
bounce
ammo-body="type: kinematic; addCollideEventListener: true; disableCollision: true;"
ammo-shape="type: cone;"
ammo-constraint="target: #target;"
></a-cone>
<a-torus
position="-1 3.75 -7"
radius="1.25"
scale="0.5 0.5 0.5"
color="red"
shadow
ammo-body
ammo-shape="type: capsule; cylinderAxis: z;"
changescale
></a-torus>
<a-torus-knot
position="0 3.75 -5"
radius="1.25"
scale="0.5 0.5 0.5"
color="blue"
shadow
ammo-body="addCollideEventListener: false;"
ammo-shape
></a-torus-knot>
<a-cylinder
segments-height="1"
segments-radial="10"
position="1 4.0 -5"
radius="0.5"
height="1.5"
color="#FFC65D"
shadow
ammo-body
ammo-shape="type: cylinder"
></a-cylinder>
<a-plane
position="0 2 -4"
rotation="90 0 0"
width="1"
height="1"
color="#7BC8A4"
shadow
ammo-body="type: static; mass: 0;"
ammo-shape="type: box;"
></a-plane>
<a-torus-knot
position="0 0 -7"
radius="1.25"
scale="5 0.1 5"
rotation="0 90 0"
color="#7BC8A4"
shadow
ammo-body="type: static; mass: 0;"
ammo-shape="type: mesh;"
></a-torus-knot>
<!-- HACD and VHACD are slow -->
<!-- <a-torus-knot
position="-2 1.5 -1"
rotation="0 90 90"
scale="0.5 0.5 0.5"
color="red"
shadow
ammo-body
ammo-shape="type: hacd;"
></a-torus-knot>
<a-torus-knot
position="2 1.5 -1"
rotation="0 90 90"
scale="0.5 0.5 0.5"
color="blue"
shadow
ammo-body
ammo-shape="type: vhacd;"
></a-torus-knot> -->
<a-sky color="#000000"></a-sky>
</a-scene>
</body>
</html>
48 changes: 48 additions & 0 deletions examples/cannon.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html>
<head>
<title>Examples • CannonDriver</title>
<meta name="description" content="Hello, WebVR! - A-Frame">
<script src="https://aframe.io/releases/0.8.2/aframe.min.js"></script>
<script src="../bundle.js"></script>
<script>
AFRAME.registerComponent('bounce', {
init: function() {
this.direction = 1;
this.position = new THREE.Vector3();
this.position.copy(this.el.object3D.position);
setTimeout(() => {
this.ready = true;
}, 3000);
},

tick: function() {
if (!this.ready) return;
var position = this.el.object3D.position.y;
if (position <= 0) {
this.direction = 1;
} else if (position >= 5) {
this.direction = -1;
}
this.el.object3D.position.set(this.position.x, position + 0.05 * this.direction, this.position.z);
}
});
</script>
</head>
<body>
<a-scene physics="debug: false; gravity: -9.8;">
<a-camera near=0.001></a-camera>
<a-box position="-1 5 -5" rotation="0 45 0" color="#4CC3D9" shadow body="shape: box;"></a-box>
<a-box id="target" position="1 3.75 -4" rotation="0 45 0" color="purple" shadow body="shape: box;"></a-box>
<a-sphere position="0 10 -10" radius="1.25" color="#EF2D5E" shadow body="shape: sphere;"></a-sphere>
<a-cone position="0 3.75 -4" radius-bottom="1.25" color="green" shadow bounce body="type: static; mass: shape: cone; addCollideEventListener: true; collisionFlags: 4" constraint="target: #target;"></a-cone>
<a-torus position="-1 3.75 -7" radius="1.25" scale="0.5 0.5 0.5" color="red" shadow body="shape: capsule; cylinderAxis: z;"></a-torus>
<a-torus-knot position="0 3.75 -5" radius="1.25" scale="0.5 0.5 0.5" color="blue" shadow body="addCollideEventListener: false;"></a-torus-knot>
<a-cylinder segments-height="1" segments-radial="10" position="1 4.0 -5" radius="0.5" height="1.5" color="#FFC65D" shadow body="shape: cylinder"></a-cylinder>
<a-plane position="0 2 -4" rotation="90 0 0" width="1" height="1" color="#7BC8A4" shadow body="type: static; mass: 0; shape: box;"></a-plane>
<a-torus-knot position="0 0 -7" radius="1.25" scale="5 0.1 5" rotation="0 90 0" color="#7BC8A4" shadow body="type: static; mass: 0; shape: box;"></a-torus-knot>
<a-sky color="#ECECEC"></a-sky>
</a-scene>
</body>
</html>

5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
var CANNON = require('cannon');

require('./src/components/math');
require('./src/components/body/ammo-body');
require('./src/components/body/body');
require('./src/components/body/dynamic-body');
require('./src/components/body/static-body');
require('./src/components/shape/shape')
require('./src/components/shape/shape');
require('./src/components/shape/ammo-shape')
require('./src/components/ammo-constraint');
require('./src/components/constraint');
require('./src/components/spring');
require('./src/system');
Expand Down
Loading

0 comments on commit 60b907f

Please sign in to comment.