Quick and Dirty Physics in AltspaceVR SDK
Physics is the butter to VR’s bread. When playing around with AltspaceVR’s SDK, I wondered —
Can I hack physics into the altspaceVR SDK?
Turns out you can…sort of.
How was this accomplished?
By hacking the Altspace SDK code to force Physijs integration — I would first recommend taking a look at the physijs wiki. You can see an example of integration here.
First, we must setup Physijs’s worker scripts inside the SDK’s Simulation function:
altspace.utilities.Simulation = function (config) {
Physijs.scripts.ammo = ‘../dist/ammo.js’;
Physijs.scripts.worker = ‘../dist/physijs_worker.js’;
...
...
Then, we replace Altspace’s default scene variable with a Physijs scene:
var scene = new Physijs.Scene({ reportsize: 50, fixedTimeStep: 1 / 60 });
scene.setGravity(new THREE.Vector3( 0, -200, 0 ))
Finally, we must call the simulation in every frame of loop():
function loop() {
scene.simulate(); //physijs simulation
window.requestAnimationFrame(loop);
if (scene.updateAllBehaviors) {
scene.updateAllBehaviors();
renderer.render(scene, camera);
}
I know you’re super excited, but there’s one catch I should mention:
I haven’t figured out yet to get cursor events to work with the physics objects. In fact, I’m not even sure where they are defined in the SDK. If you have an ideas on how to solve this, let me know in the comments.
In case you want to give a go, the source code for the project in the GIF can be found here: https://github.com/william-bratches/altspace-vr-physics