schteppe / cannon.js

A lightweight 3D physics engine written in JavaScript.
http://schteppe.github.com/cannon.js
MIT License
4.69k stars 712 forks source link

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

Open auto3000 opened 10 years ago

auto3000 commented 10 years ago

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 commented 10 years ago

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 commented 8 years ago

Has this been resolved I am still having this issue.

VikasCodeWizard commented 4 years ago

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.