question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Examples: include example code of importing a HCAD convex hull from a GLTF file

See original GitHub issue

Hello, Thanks for maintaining cannon!

I have an error when I create ConvexPolyhedron, I must miss something

I have a few errors when creating a ConvexPolyhedron Body : cannon-es.js:2247 .faceNormals[0] = Vec3(0,0,-0.9999999999999999) looks like it points into the shape? The vertices follow. Make sure they are ordered CCW around the normal, using the right-hand rule.

My process :

  • Loading a mesh with a buffer geometry from an HCAD convex decomposition
  • I retrieve the faces & vertex to create the cannon body

Code :

CreateConvexPolyhedron(geometry, scaleVec) {
        if (!geometry.vertices) {
            geometry = new THREE.Geometry()
                .fromBufferGeometry(geometry)
                .scale(scaleVec.x, scaleVec.y, scaleVec.z)
            geometry.mergeVertices()
            geometry.computeBoundingSphere()
            geometry.computeFaceNormals()
        }
        const points = [],
            faces = []
        points = geometry.vertices.map(function (v) {
            return new CANNON.Vec3(v.x, v.y, v.z)
        })

        faces = geometry.faces.map(function (f) {
            return [f.a, f.b, f.c]
        })
        return new CANNON.ConvexPolyhedron({ vertices: points, faces })
    }

Help would be more than welcome 😃

Thanks

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
felixmariottocommented, Feb 13, 2021

I’ve got this bug with the generated body though, do you think it’s related to the way I’m making the convex polyhedron ? Bodies constructed from primitives behave normally ( they don’t jump wildly when colliding ). In this scene the sphere, the box and the convex polyhedron have a mass of 1 and use the same material.

https://user-images.githubusercontent.com/46470486/107856840-4b3efb80-6e2b-11eb-8bd9-6de604ac52db.mp4

2reactions
felixmariottocommented, Feb 13, 2021

@marcofugaro

I came up with this :

const hullBody = new CANNON.Body({
	mass,
	material
});

glb.scene.traverse( (obj) => {

	if ( obj.geometry ) {

		const posAttrib = obj.geometry.attributes.position;
		const vertices = [];

		for ( let i=0 ; i<posAttrib.count ; i++ ) {

			vertices.push(
				new CANNON.Vec3(
					posAttrib.getX( i ),
					posAttrib.getY( i ),
					posAttrib.getZ( i )
				)
			);

		}

		//

		const normAttrib = obj.geometry.attributes.normal;
		const normals = [];

		for ( let i=0 ; i<normAttrib.count ; i++ ) {

			normals.push(
				new CANNON.Vec3(
					normAttrib.getX( i ),
					normAttrib.getY( i ),
					normAttrib.getZ( i )
				)
			);

		}

		//

		const index = obj.geometry.index;
		const faces = [];

		for ( let i=0 ; i<index.count ; i+=3 ) {

			faces.push([
				index.array[ i ],
				index.array[ i + 1 ],
				index.array[ i + 2 ]
			]);

		}

		// Construct polyhedron

		const hullPart = new CANNON.ConvexPolyhedron({ vertices, faces, normals })

		// Add to compound

		hullBody.addShape( hullPart );

	}

});

No idea if it’s optimal, I’m just starting with cannon.js. Thank you for maintaining this fork btw !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prototype GLTF importer and convex hull collision detection
It's designed to convert GLTF files to HMD as part of resource processing. A 3D convex hull generator and GJK/EPA collision detection algorithm....
Read more >
Convex Hull using Divide and Conquer Algorithm
A convex hull is the smallest convex polygon containing all the given points. Recommended: Please solve it on “PRACTICE” first, before moving ...
Read more >
How to properly import and setup physics objects - GLTf
I'm trying to import a mesh from a GLTF file and turn it into a rigid body - physics object. I have the...
Read more >
cannon-es - bytemeta
Examples : include example code of importing a HCAD convex hull from a GLTF file. patreeceeo. patreeceeo OPEN · Updated 1 year ago ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found