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 to import and look at an obj. 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 followed the three.js installation on the three.js website and downloaded ammo.js using npm install github:kripken/ammo.js
. I use vite for the localhost.
I feel like I’m missing something with the ammo.js import, but I’ve looked at ammo.js example codes, searched around both the three.js-forum and stackoverflow, asked ChatGPT 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;
console.log('Ammo:', typeof Ammo);
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();
// ...
}
ecberger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.