vujadin / BabylonHx

Port of Babylon.js 3D engine to Haxe.
http:/paradoxplay.com/babylonhx
Apache License 2.0
188 stars 43 forks source link

Endless loop when camera is inside of a Mesh #50

Closed mightymarcus closed 8 years ago

mightymarcus commented 9 years ago

If the camera (freecamera) is inside of a Mesh, it often results in an endless loop (freezing). I did not notice this on earlier versions. I tried with box and sphere.

Both cpp target and html5.

mightymarcus commented 9 years ago

Ok, i found the error. It's not because the camera is inside the mesh, but sometimes when the mouse cursor picks the mesh the intersects function is called with positions => null. Checking for null and breaking the loop helps.

SubMesh.hx Line 166

inline public function intersects(ray:Ray, positions:Array<Vector3>, indices:Array<Int>, fastCheck:Bool = false):IntersectionInfo {
        var intersectInfo:IntersectionInfo = null;

        // Triangles test
        var index:Int = this.indexStart;
        while (index < this.indexStart + this.indexCount) {

            if (positions == null) break;  <---- BYPASS ERROR

            var p0 = positions[indices[index]];
            var p1 = positions[indices[index + 1]];
            var p2 = positions[indices[index + 2]];

            var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);

            if (currentIntersectInfo != null) {
                if(currentIntersectInfo.distance < 0 ) continue;
                if (fastCheck || intersectInfo == null || currentIntersectInfo.distance < intersectInfo.distance) {
                    intersectInfo = currentIntersectInfo;
                    intersectInfo.faceId = Std.int(index / 3);

                    if (fastCheck) {
                        break;
                    }
                }
            }

            index += 3;
        }

        return intersectInfo;
    }
mightymarcus commented 9 years ago

Btw. commenting out the pickingInfo functions in Scene.hx onMouseMove etc. gives much better performance. I have 200 Boxes falling down, and when the mouse pointer moves over just one of them its getting to be a slideshow.

vujadin commented 9 years ago

Hi, you should move that if() out of while loop as it shouldn't enter the loop if positions is null. another (temp) solution would be to set for your meshes isPickable = false (or make if false for them all in AbstractMesh class, right now its true), it will prevent intersects() function to be called. At least until I fix the problem.

mightymarcus commented 8 years ago

Good to know, thanks. Makes sense.