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

Heightfield issue: Make sure they are ordered CCW around the normal, using the right hand rule. #149

Open
auto3000 opened this issue Sep 20, 2014 · 3 comments

Comments

@auto3000
Copy link

Hello,

I give a try to CANNON.Heightfield but I meet an issue during step(). I can see the javascript console log below in red, I suspect this message below is important. Issue seen on Cannon.js 0.6.0 (git extract from a couple of days).

.faceNormals[1] = Vec3(0,0,-1) looks like it points into the shape? The vertices follow. Make sure they are ordered CCW around the normal, using the right hand rule. (23:54:00:901 | error)
at ConvexPolyhedron.computeNormals (www/js/libs/cannon.js:7125:21)
at Heightfield.getConvexTrianglePillar (www/js/libs/cannon.js:8437:12)
at Narrowphase.(anonymous function).Narrowphase.convexHeightfield (www/js/libs/cannon.js:10590:21)
at Narrowphase.(anonymous function).Narrowphase.boxHeightfield (www/js/libs/cannon.js:10525:10)
at Narrowphase.getContacts (www/js/libs/cannon.js:9439:34)
at World.internalStep (www/js/libs/cannon.js:11243:22)
at World.step (www/js/libs/cannon.js:11088:14)
at xxx (www/js/main.js:178:17)
.vertices[5] = Vec3(-0.25,0.75,5.5) (23:54:00:902 | warning)
at www/js/libs/cannon.js:7127
.vertices[4] = Vec3(0.75,-0.25,5.5) (23:54:00:903 | warning)
at www/js/libs/cannon.js:7127
.vertices[3] = Vec3(-0.25,-0.25,5.5) (23:54:00:903 | warning)
at www/js/libs/cannon.js:7127

Right, it seems (-0.25,0.75,5.5) (0.75,-0.25,5.5) (-0.25,-0.25,5.5) are not in CCW order (right handed). Please find my (simplified) code below:

var grid_size = 10;
var matrix = new Array(2*grid_size);
for( var i=0; i<2*grid_size; i++)
{
    matrix[i] = new Array(2*grid_size);
    for( var j=0; j<2*grid_size; j++ )
    {
        matrix[i][j] =  i - j;
    } 
}

var hfShape = new CANNON.Heightfield(matrix, { elementSize:1 });
var hfBody = new CANNON.Body({mass: 0});
hfBody.addShape(hfShape);

The issue disappears if I guarantee Y heights are smaller (fix code: matrix[i][j] = i - j - 10;)

@schteppe
Copy link
Owner

I've seen this error before and I'll fix it later on. For now, you could keep your height values small and move the shape up instead.

@ghost
Copy link

ghost commented Jun 21, 2016

Has this been resolved I am still having this issue.

@VikasCodeWizard
Copy link

VikasCodeWizard commented May 9, 2020

If this issue comes with any other cannon body then check scale value of body. for me when it was negative bcoz the object in blender was in -ve x, y &z. which caused the issue. I fixed by setting scale value to positive.
what I can conclude is if face normals direction is inside in a closed box then it intersects other faces, which is not correct direction of normals for objects as face normal direction should be in opposite direction. For example in cube all face normal should point in direction from center of each face away from cube center otherwise the cube object is not possible according to cannon js.

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

3 participants