Hello, im learning playcanvas engine and trying to use physics.
I think that im loading ammo just fine, but rigidbodies doesnt work.
I feel like im missing some simple thing, but still cant find out by myself.
Heres the code im using.
function wasmSupported() {
try {
if (typeof WebAssembly === "object" && typeof WebAssembly.instantiate === "function") {
const module = new WebAssembly.Module(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));
if (module instanceof WebAssembly.Module)
return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
}
} catch (e) { }
return false;
}
function loadScriptAsync(url, doneCallback) {
var tag = document.createElement('script');
tag.onload = function () {
doneCallback();
};
tag.onerror = function () {
throw new Error('failed to load ' + url);
};
tag.async = true;
tag.src = url;
document.head.appendChild(tag);
}
function loadWasmModuleAsync(moduleName, jsUrl, binaryUrl, doneCallback) {
loadScriptAsync(jsUrl, function () {
var lib = window[moduleName];
window[moduleName + 'Lib'] = lib;
lib({
locateFile: function () {
return binaryUrl;
}
}).then(function (instance) {
window[moduleName] = instance;
console.log("ammo loaded?")
doneCallback();
});
});
}
function loadAmmo()
{
if(wasmSupported())
{
loadWasmModuleAsync('Ammo',"./ammo.wasm.js","./ammo.wasm.wasm", init)
}
}
loadAmmo()
function init()
{
const canvas = document.getElementById("canvas")
const app = new pc.Application(canvas)
app.systems.rigidbody.addBody
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW)
app.setCanvasResolution(pc.RESOLUTION_AUTO)
window.addEventListener('resize', () => app.resizeCanvas())
const bola = new pc.Entity('chao')
bola.addComponent('model',{
type: 'sphere'
})
bola.setPosition(0,2,0)
bola.addComponent("rigidbody",{
type : pc.BODYTYPE_DYNAMIC,
mass : 10
})
app.root.addChild(bola)
bola.rigidbody.activate()
console.log(bola.rigidbody.isActive())
const floor = new pc.Entity('chao')
floor.addComponent('model',{
type: 'box'
})
floor.addComponent("rigidbody")
app.root.addChild(floor)
const luz = new pc.Entity('light')
luz.addComponent('light')
app.root.addChild(luz)
const cam = new pc.Entity('camera')
cam.addComponent('camera',{
clearColor: new pc.Color(0.1,0.1,0.1)
})
app.root.addChild(cam)
cam.setPosition(0,1,5)
cam.lookAt(floor)
app.on('update', dt =>{
})
app.start()
}
Heres my directory.
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 05/02/2022 12:23 1895438 ammo.js
-a--- 05/02/2022 12:23 391789 ammo.wasm.js
-a--- 05/02/2022 12:23 659433 ammo.wasm.wasm
-a--- 05/02/2022 13:21 2497 fisica.js
-a--- 05/02/2022 12:41 733 game.js
-a--- 05/02/2022 12:58 482 index.html
-a--- 05/02/2022 01:22 141 jsconfig.json
-a--- 26/10/1985 05:15 1141086 playcanvas.d.ts
-a--- 26/10/1985 05:15 2296300 playcanvas.js
I get no errors on console and console.log("ammo loaded?")
, line 34, runs just fine so i think Ammo got loaded.
console.log(bola.rigidbody.isActive())
returns false and the ball doesnt falls.
I got the ammo stuff from here: https://github.com/playcanvas/engine/tree/dev/examples/src/lib/ammo