Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/ammo.worker-77fa871247ec9a48b70e.js.map

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion dist/ammo.worker-8c0cb5e7d97e9982bf3e.js.map

This file was deleted.

6 changes: 3 additions & 3 deletions dist/threeammo.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/threeammo.js.map

Large diffs are not rendered by default.

36 changes: 32 additions & 4 deletions src/ammo.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,41 @@ const tick = () => {
const body = bodies[uuid];
const index = indexes[uuid];
const matrix = matrices[uuid];
const isDynamic = body.type === TYPE.DYNAMIC;

// Only need to track first three syncs to deal with dynamic bodies which start out as such.
const isTrackingInitialSyncs = body.initialSyncCount < 2;

matrix.fromArray(objectMatricesFloatArray, index * BUFFER_CONFIG.BODY_DATA_SIZE + BUFFER_CONFIG.MATRIX_OFFSET);
body.updateShapes();

if (body.type === TYPE.DYNAMIC) {
body.syncFromPhysics();
// If body starts out as dynamic (ie its initial sync count is zero but it is marked as dynamic)
// wait a tick so host process can set its initial transform before physics starts driving it.
if (isTrackingInitialSyncs && isDynamic && body.initialSyncCount === 0) {
body.initialSyncCount++;
continue;
}

matrix.fromArray(objectMatricesFloatArray, index * BUFFER_CONFIG.BODY_DATA_SIZE + BUFFER_CONFIG.MATRIX_OFFSET);

if (isDynamic) {
if (body.initialSyncCount === 1) {
// Initial transform now set by host process for body which starts as dynamic. Initialize the body.
resetDynamicBody({ uuid });
} else {
// Dynamic body is now active and initialized, let physics engine drive its behavior.
body.syncFromPhysics();
}
} else {
body.syncToPhysics(false);
}

// Skip the work of incrementing the initialSyncCount unless we're still in the first 3 syncs.
//
// (Otherwise we don't care about it, since its only needed to initialize dynamic bodies that begin as such.)
if (isTrackingInitialSyncs) {
body.initialSyncCount++;
}

objectMatricesFloatArray.set(matrix.elements, index * BUFFER_CONFIG.BODY_DATA_SIZE + BUFFER_CONFIG.MATRIX_OFFSET);

objectMatricesFloatArray[
Expand Down Expand Up @@ -263,7 +288,10 @@ function resetDynamicBody({ uuid }) {
if (bodies[uuid]) {
const body = bodies[uuid];
const index = indexes[uuid];
matrices[uuid].fromArray(objectMatricesFloatArray, index * BUFFER_CONFIG.BODY_DATA_SIZE);
matrices[uuid].fromArray(
objectMatricesFloatArray,
index * BUFFER_CONFIG.BODY_DATA_SIZE + BUFFER_CONFIG.MATRIX_OFFSET
);
body.syncToPhysics(true);
body.physicsBody.getLinearVelocity().setValue(0, 0, 0);
body.physicsBody.getAngularVelocity().setValue(0, 0, 0);
Expand Down
1 change: 1 addition & 0 deletions src/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function Body(bodyConfig, matrix, world) {
this.matrix = matrix;
this.world = world;
this.shapes = [];
this.initialSyncCount = 0;

this._initBody();
}
Expand Down