Skip to content

Commit

Permalink
OimoPhysics updates and house cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
pschroen committed Apr 23, 2024
1 parent 0de5815 commit c3f543f
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ npm i three saharan/OimoPhysics#v1.2.3 @alienkitty/alien.js
import { OimoPhysics } from '@alienkitty/alien.js/three/oimophysics';

const physics = new OimoPhysics();
console.log(physics.world.getGravity());
console.log(physics.getGravity());
```

And the `@alienkitty/alien.js/ogl` entry point for [OGL](https://github.com/oframe/ogl) material programs.
Expand Down
6 changes: 3 additions & 3 deletions examples/three/3d_physics_instancing.html
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
floor.geometry.setDrawRange(0, 0); // Avoid rendering geometry
this.add(floor);

physics.add(floor, { density: 0 });
physics.add(floor, { density: 0, autoSleep: false });
}

// Public methods
Expand Down Expand Up @@ -452,7 +452,7 @@
const view = this.view;

const vector3 = new Vector3();
const gravity = this.physics.world.getGravity();
const gravity = this.physics.getGravity();

const physicsOptions = {
Off: false,
Expand Down Expand Up @@ -496,7 +496,7 @@
value: -gravity.y,
callback: value => {
gravity.y = -value;
physics.world.setGravity(gravity);
physics.setGravity(gravity);
}
},
{
Expand Down
56 changes: 37 additions & 19 deletions examples/three/3d_physics_instancing_thread.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
this.physics.setPosition(name, position);
};

setOrientation = (name, orientation) => {
this.physics.setOrientation(name, orientation);
};

setLinearVelocity = (name, linearVelocity) => {
this.physics.setLinearVelocity(name, linearVelocity);
};
Expand Down Expand Up @@ -111,6 +115,10 @@
this.physics.setPosition(name, position);
};

setOrientation = ({ name, orientation }) => {
this.physics.setOrientation(name, orientation);
};

setLinearVelocity = ({ name, linearVelocity }) => {
this.physics.setLinearVelocity(name, linearVelocity);
};
Expand Down Expand Up @@ -139,7 +147,7 @@
['../../build/alien.three.js', 'EventEmitter', 'OimoPhysicsBuffer', 'Vector3', 'delayedCall', 'ticker']
],
classes: [ScenePhysics],
controller: [ScenePhysicsThread, 'init', 'update', 'setGravity', 'setPosition', 'setLinearVelocity', 'setAngularVelocity']
controller: [ScenePhysicsThread, 'init', 'update', 'setGravity', 'setPosition', 'setOrientation', 'setLinearVelocity', 'setAngularVelocity']
});

this.thread.init({ shapes: this.shapes });
Expand Down Expand Up @@ -177,6 +185,12 @@
this.thread.setPosition({ name, position });
};

setOrientation = (object, orientation, index) => {
const { name } = this.getObjectBody(object, index);

this.thread.setOrientation({ name, orientation });
};

setLinearVelocity = (object, linearVelocity, index) => {
const { name } = this.getObjectBody(object, index);

Expand Down Expand Up @@ -233,6 +247,12 @@

this.position.y = -2.5;

// Physics
this.name = 'floor';
this.size = new Vector3(100, 5, 100);
this.density = 0;
this.autoSleep = false;

this.initReflector();
}

Expand All @@ -247,7 +267,7 @@
async initMesh() {
const { physics, loadTexture } = WorldController;

const geometry = new PlaneGeometry(100, 100);
const geometry = new PlaneGeometry(this.size.x, this.size.z);

const map = await loadTexture('../assets/textures/waterdudv.jpg');
map.wrapS = RepeatWrapping;
Expand Down Expand Up @@ -351,11 +371,11 @@
this.add(mesh);

// Physics mesh
const floor = new Mesh(new BoxGeometry(100, 5, 100));
const floor = new Mesh(new BoxGeometry(this.size.x, this.size.y, this.size.z));
floor.geometry.setDrawRange(0, 0); // Avoid rendering geometry
this.add(floor);

physics.add(floor, { density: 0 });
physics.add(floor);
}

// Public methods
Expand Down Expand Up @@ -649,24 +669,22 @@
callback: value => {
SceneController.enabled = physicsOptions[value];

if (!SceneController.enabled) {
// Reset
vector3.set(0, 0, 0);
const array = vector3.toArray();
// Reset
vector3.set(0, 0, 0);
const array = vector3.toArray();

for (let i = 0, l = view.boxes.count; i < l; i++) {
physics.setLinearVelocity(view.boxes, array, i);
physics.setAngularVelocity(view.boxes, array, i);
}

for (let i = 0, l = view.spheres.count; i < l; i++) {
physics.setLinearVelocity(view.spheres, array, i);
physics.setAngularVelocity(view.spheres, array, i);
}
for (let i = 0, l = view.boxes.count; i < l; i++) {
physics.setLinearVelocity(view.boxes, array, i);
physics.setAngularVelocity(view.boxes, array, i);
}

// Flush the last frame
physics.update();
for (let i = 0, l = view.spheres.count; i < l; i++) {
physics.setLinearVelocity(view.spheres, array, i);
physics.setAngularVelocity(view.spheres, array, i);
}

// Flush the last frame
physics.update();
}
},
{
Expand Down
21 changes: 17 additions & 4 deletions examples/three/3d_physics_picking.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,13 @@
const mesh = new Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;

// Layers
mesh.layers.enable(layers.picking);

this.add(mesh);

// Physics
physics.add(mesh, { autoSleep: false });
}

Expand All @@ -107,6 +111,7 @@
constructor() {
super();

// Physics
this.force = new Vector3();
this.contact = false;
}
Expand Down Expand Up @@ -200,9 +205,13 @@
const mesh = new Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;

// Layers
mesh.layers.enable(layers.picking);

this.add(mesh);

// Physics
physics.add(mesh, {
autoSleep: false,
contactCallback: this.onContact
Expand Down Expand Up @@ -370,7 +379,7 @@
floor.geometry.setDrawRange(0, 0); // Avoid rendering geometry
this.add(floor);

physics.add(floor, { density: 0 });
physics.add(floor, { density: 0, autoSleep: false });
}

// Public methods
Expand Down Expand Up @@ -536,7 +545,7 @@
const physics = this.physics;

const vector3 = new Vector3();
const gravity = this.physics.world.getGravity();
const gravity = this.physics.getGravity();

const physicsOptions = {
Off: false,
Expand All @@ -561,6 +570,10 @@
vector3.set(0, 0, 0);

physics.objects.forEach(object => {
const { position, quaternion } = object;

physics.setPosition(object, position);
physics.setOrientation(object, quaternion);
physics.setLinearVelocity(object, vector3);
physics.setAngularVelocity(object, vector3);
});
Expand All @@ -575,7 +588,7 @@
value: -gravity.y,
callback: value => {
gravity.y = -value;
physics.world.setGravity(gravity);
physics.setGravity(gravity);
}
},
{
Expand Down Expand Up @@ -856,7 +869,7 @@

this.dragPlane = new Mesh(quad, material);
this.dragPlane.scale.multiplyScalar(200);
this.dragPlane.layers.enable(layers.picking);
this.dragPlane.layers.set(layers.picking);
}

static addListeners() {
Expand Down
53 changes: 39 additions & 14 deletions examples/three/3d_physics_picking_thread.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@
this.physics.setPosition(name, position);
};

setOrientation = (name, orientation) => {
this.physics.setOrientation(name, orientation);
};

setLinearVelocity = (name, linearVelocity) => {
this.physics.setLinearVelocity(name, linearVelocity);
};
Expand Down Expand Up @@ -174,6 +178,10 @@
this.physics.setPosition(name, position);
};

setOrientation = ({ name, orientation }) => {
this.physics.setOrientation(name, orientation);
};

setLinearVelocity = ({ name, linearVelocity }) => {
this.physics.setLinearVelocity(name, linearVelocity);
};
Expand Down Expand Up @@ -207,7 +215,7 @@
['../../build/alien.three.js', 'EventEmitter', 'OimoPhysicsBuffer', 'Vector3', 'delayedCall', 'ticker']
],
classes: [ScenePhysics],
controller: [ScenePhysicsThread, 'init', 'update', 'add', 'remove', 'setGravity', 'setPosition', 'setLinearVelocity', 'setAngularVelocity']
controller: [ScenePhysicsThread, 'init', 'update', 'add', 'remove', 'setGravity', 'setPosition', 'setOrientation', 'setLinearVelocity', 'setAngularVelocity']
});

this.thread.init({ shapes: this.shapes });
Expand Down Expand Up @@ -256,6 +264,10 @@
this.thread.setPosition({ name, position });
};

setOrientation = (name, orientation) => {
this.thread.setOrientation({ name, orientation });
};

setLinearVelocity = (name, linearVelocity) => {
this.thread.setLinearVelocity({ name, linearVelocity });
};
Expand All @@ -273,6 +285,7 @@

this.position.z = 1;

// Physics
this.name = 'ball';
this.radius = 0.075;
this.autoSleep = false;
Expand Down Expand Up @@ -339,9 +352,13 @@
const mesh = new Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;

// Layers
mesh.layers.enable(layers.picking);

this.add(mesh);

// Physics
physics.add(mesh);
}

Expand All @@ -354,6 +371,7 @@
constructor() {
super();

// Physics
this.name = 'cube';
this.size = new Vector3(0.5, 0.5, 0.5);
this.autoSleep = false;
Expand Down Expand Up @@ -448,9 +466,13 @@
const mesh = new Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;

// Layers
mesh.layers.enable(layers.picking);

this.add(mesh);

// Physics
physics.add(mesh);
}

Expand All @@ -467,9 +489,11 @@

this.position.y = -3.04;

// Physics
this.name = 'floor';
this.size = new Vector3(100, 5, 100);
this.density = 0;
this.autoSleep = false;

this.initReflector();
}
Expand Down Expand Up @@ -587,7 +611,7 @@
this.add(mesh);

// Physics mesh
const floor = new Mesh(new BoxGeometry(100, 5, 100));
const floor = new Mesh(new BoxGeometry(this.size.x, this.size.y, this.size.z));
floor.geometry.setDrawRange(0, 0); // Avoid rendering geometry
this.add(floor);

Expand Down Expand Up @@ -785,20 +809,21 @@
callback: value => {
SceneController.enabled = physicsOptions[value];

if (!SceneController.enabled) {
// Reset
vector3.set(0, 0, 0);
const array = vector3.toArray();
// Reset
vector3.set(0, 0, 0);
const array = vector3.toArray();

physics.setLinearVelocity('cube', array);
physics.setAngularVelocity('cube', array);
physics.objects.forEach(object => {
const { name, position, quaternion } = object;

physics.setLinearVelocity('ball', array);
physics.setAngularVelocity('ball', array);
physics.setPosition(name, position.toArray());
physics.setOrientation(name, quaternion.toArray());
physics.setLinearVelocity(name, array);
physics.setAngularVelocity(name, array);
});

// Flush the last frame
physics.update();
}
// Flush the last frame
physics.update();
}
},
{
Expand Down Expand Up @@ -1088,7 +1113,7 @@

this.dragPlane = new Mesh(quad, material);
this.dragPlane.scale.multiplyScalar(200);
this.dragPlane.layers.enable(layers.picking);
this.dragPlane.layers.set(layers.picking);
}

static addListeners() {
Expand Down
2 changes: 1 addition & 1 deletion examples/three/test_oimophysics.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import { OimoPhysics } from '@alienkitty/alien.js/three/oimophysics';

const physics = new OimoPhysics();
console.log(physics.world.getGravity());
console.log(physics.getGravity());
</script>
</head>
<body>
Expand Down
Loading

0 comments on commit c3f543f

Please sign in to comment.