Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

destroy() correctly #358

Open
sancelot opened this issue Feb 15, 2021 · 1 comment
Open

destroy() correctly #358

sancelot opened this issue Feb 15, 2021 · 1 comment

Comments

@sancelot
Copy link

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 = [];
        }
@suddenly4
Copy link

suddenly4 commented Dec 18, 2024

Ammo.destroy( your object ammo);
for example:

for (let i = 0, l = faces.length; i < l; i++) {
let a = faces[i].a;
let b = faces[i].b;
let c = faces[i].c;
let v1 = new Ammo.btVector3(vertices[a].x, vertices[a].y, vertices[a].z);
let v2 = new Ammo.btVector3(vertices[b].x, vertices[b].y, vertices[b].z);
let v3 = new Ammo.btVector3(vertices[c].x, vertices[c].y, vertices[c].z);
someShape.addTriangle(v1, v2, v3, true);
Ammo.destroy(v1);
Ammo.destroy(v2);
Ammo.destroy(v3);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants