destroy() correctly
sancelot opened this issue · 0 comments
sancelot commented
I enhanced the api to use btCollisionObject, this works fine for the moment.
But I need to give a new scene each time I am calling collide detection.
This imply cleanly deallocating .
I used this code to create the scene and the next one to deallocate.
unfortunately , memory is filling and this is very boring. I don't manage to find what remains allocated
allocation
const geometry = mesh.geometry.clone();
const vertices = geometry.getAttribute("position");
const indices = geometry.index;
const trimesh = new BulletObject.ammoInjection.btTriangleMesh();
const bta = new BulletObject.ammoInjection.btVector3()
const btb = new BulletObject.ammoInjection.btVector3()
const btc = new BulletObject.ammoInjection.btVector3()
let va = new THREE.Vector3()
let vb = new THREE.Vector3()
let vc = new THREE.Vector3()
// --------------------------------------------------------
for (let j = 0; j < indices.itemSize * indices.count; j += 3) {
const ai = indices.array[j] * 3
const bi = indices.array[j + 1] * 3
const ci = indices.array[j + 2] * 3
va.set(vertices.array[ai], vertices.array[ai + 1], vertices.array[ai + 2])
vb.set(vertices.array[bi], vertices.array[bi + 1], vertices.array[bi + 2])
vc.set(vertices.array[ci], vertices.array[ci + 1], vertices.array[ci + 2])
va = mesh.localToWorld(va)
vb = mesh.localToWorld(vb)
vc = mesh.localToWorld(vc)
}
bta.setValue(va.x, va.y, va.z)
btb.setValue(vb.x, vb.y, vb.z)
btc.setValue(vc.x, vc.y, vc.z)
trimesh.addTriangle(bta, btb, btc, false)
}
//----------------------------------------------------------------------------------------
const shape = new BulletObject.ammoInjection.btConvexTriangleMeshShape(trimesh);
let m = new BulletObject.ammoInjection.btCollisionObject();
m.setCollisionShape(shape);
m.threeObject = mesh
BulletObject._physicsWorld.addCollisionObject(m);
this.shapes.push(shape)
this.collide_object.push(m)
geometry.dispose()
deallocation
for (let i = 0; i < this.collide_object.length; i++) {
//this.collide_object[i].threeObject = null
BulletObject._physicsWorld.removeCollisionObject(this.collide_object[i]);
BulletObject.ammoInjection.destroy(this.collide_object[i])
}
for (let i = 0; i < this.shapes.length; i++) {
BulletObject.ammoInjection.destroy(this.shapes[i])
}
this.collide_object = [];
this.shapes = [];
}