Hi there.
I am trying to use three.js to write a program in which I can import a model file (likely .obj), turn it into a cloth/softbody and interact with it using my mouse (for example to pull at the cloth).
I have used the obj-importer and THREE.x successfully so far. Now I would like to turn the .obj into a softbody using ammo.js for the physics part. However, I am having trouble with the initialization: I either get a TypeError for my first instance calling on Ammo
Ammo.btSoftBodyRigidBodyCollisionConfiguration is a undefined
TypeError: Ammo.btSoftBodyRigidBodyCollisionConfiguration is not a constructor
or, when I try to initialize ammo.js in an asynchronous way using
Ammo().then((lib) => {
Ammo = lib;
init()
});
the ouput is
Ammo: object
TypeError: Ammo is not a function
I have downloaded ammo.js using
npm install github:kripken/ammo.js
and I use vite for the localhost.
I feel like Iâm missing something, but Iâve looked at example codes and searched around both the forum and stackoverflow and not really found a solution. Any help would be greatly appreciated.
I use the following code:
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ammo.js Softbody-OBJ</title>
<link rel="icon" href="images/favicon.ico" type="image/x-icon">
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<script type="module" src="./node_modules/ammo.js/builds/ammo.wasm.js"></script>
<script type="module" src="./src/main.js"></script>
</body>
</html>
main.js:
import * as THREE from 'three';
import * as Ammo from '../node_modules/ammo.js/builds/ammo.wasm.js';
let scene, camera, renderer, controls, softBody, dynamicsWorld, softBodyMesh;
Ammo().then((lib) => {
Ammo = lib;
init()
});
async function init() {
//THREE.scene setup
// Set up Ammo.js physics
console.log('Ammo.btSoftBodyRigidBodyCollisionConfiguration is a ', Ammo.btSoftBodyRigidBodyCollisionConfiguration);
var collisionConfiguration = new Ammo.btSoftBodyRigidBodyCollisionConfiguration();
var dispatcher = new Ammo.btCollisionDispatcher(collisionConfiguration);
var broadphase = new Ammo.btDbvtBroadphase();
var solver = new Ammo.btSequentialImpulsevarraintSolver();
var softBodySolver = new Ammo.btDefaultSoftBodySolver();
...
}