Search in sources :

Example 6 with Vector3f

use of com.jme3.math.Vector3f in project jmonkeyengine by jMonkeyEngine.

the class Intersection method intersect.

//    private boolean axisTest(float a, float b, float fa, float fb, Vector3f v0, Vector3f v1, )
//    private boolean axisTestX01(float a, float b, float fa, float fb,
//                             Vector3f center, Vector3f ext,
//                             Vector3f v1, Vector3f v2, Vector3f v3){
//	float p0 = a * v0.y - b * v0.z;
//	float p2 = a * v2.y - b * v2.z;
//        if(p0 < p2){
//            min = p0;
//            max = p2;
//        } else {
//            min = p2;
//            max = p0;
//        }
//	float rad = fa * boxhalfsize.y + fb * boxhalfsize.z;
//	if(min > rad || max < -rad)
//            return false;
//    }
public static boolean intersect(BoundingBox bbox, Vector3f v1, Vector3f v2, Vector3f v3) {
    //  use separating axis theorem to test overlap between triangle and box
    //  need to test for overlap in these directions:
    //  1) the {x,y,z}-directions (actually, since we use the AABB of the triangle
    //     we do not even need to test these)
    //  2) normal of the triangle
    //  3) crossproduct(edge from tri, {x,y,z}-directin)
    //       this gives 3x3=9 more tests
    TempVars vars = TempVars.get();
    Vector3f tmp0 = vars.vect1, tmp1 = vars.vect2, tmp2 = vars.vect3;
    Vector3f e0 = vars.vect4, e1 = vars.vect5, e2 = vars.vect6;
    Vector3f center = bbox.getCenter();
    Vector3f extent = bbox.getExtent(null);
    //   float min,max,p0,p1,p2,rad,fex,fey,fez;
    //   float normal[3]
    // This is the fastest branch on Sun
    // move everything so that the boxcenter is in (0,0,0)
    v1.subtract(center, tmp0);
    v2.subtract(center, tmp1);
    v3.subtract(center, tmp2);
    // compute triangle edges
    // tri edge 0
    tmp1.subtract(tmp0, e0);
    // tri edge 1
    tmp2.subtract(tmp1, e1);
    // tri edge 2
    tmp0.subtract(tmp2, e2);
    // Bullet 3:
    //  test the 9 tests first (this was faster)
    float min, max;
    float p0, p1, p2, rad;
    float fex = FastMath.abs(e0.x);
    float fey = FastMath.abs(e0.y);
    float fez = FastMath.abs(e0.z);
    //AXISTEST_X01(e0[Z], e0[Y], fez, fey);
    p0 = e0.z * tmp0.y - e0.y * tmp0.z;
    p2 = e0.z * tmp2.y - e0.y * tmp2.z;
    min = min(p0, p2);
    max = max(p0, p2);
    rad = fez * extent.y + fey * extent.z;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    //   AXISTEST_Y02(e0[Z], e0[X], fez, fex);
    p0 = -e0.z * tmp0.x + e0.x * tmp0.z;
    p2 = -e0.z * tmp2.x + e0.x * tmp2.z;
    min = min(p0, p2);
    max = max(p0, p2);
    rad = fez * extent.x + fex * extent.z;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    // AXISTEST_Z12(e0[Y], e0[X], fey, fex);
    p1 = e0.y * tmp1.x - e0.x * tmp1.y;
    p2 = e0.y * tmp2.x - e0.x * tmp2.y;
    min = min(p1, p2);
    max = max(p1, p2);
    rad = fey * extent.x + fex * extent.y;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    fex = FastMath.abs(e1.x);
    fey = FastMath.abs(e1.y);
    fez = FastMath.abs(e1.z);
    //        AXISTEST_X01(e1[Z], e1[Y], fez, fey);
    p0 = e1.z * tmp0.y - e1.y * tmp0.z;
    p2 = e1.z * tmp2.y - e1.y * tmp2.z;
    min = min(p0, p2);
    max = max(p0, p2);
    rad = fez * extent.y + fey * extent.z;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    //   AXISTEST_Y02(e1[Z], e1[X], fez, fex);
    p0 = -e1.z * tmp0.x + e1.x * tmp0.z;
    p2 = -e1.z * tmp2.x + e1.x * tmp2.z;
    min = min(p0, p2);
    max = max(p0, p2);
    rad = fez * extent.x + fex * extent.z;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    // AXISTEST_Z0(e1[Y], e1[X], fey, fex);
    p0 = e1.y * tmp0.x - e1.x * tmp0.y;
    p1 = e1.y * tmp1.x - e1.x * tmp1.y;
    min = min(p0, p1);
    max = max(p0, p1);
    rad = fey * extent.x + fex * extent.y;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    //
    fex = FastMath.abs(e2.x);
    fey = FastMath.abs(e2.y);
    fez = FastMath.abs(e2.z);
    // AXISTEST_X2(e2[Z], e2[Y], fez, fey);
    p0 = e2.z * tmp0.y - e2.y * tmp0.z;
    p1 = e2.z * tmp1.y - e2.y * tmp1.z;
    min = min(p0, p1);
    max = max(p0, p1);
    rad = fez * extent.y + fey * extent.z;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    // AXISTEST_Y1(e2[Z], e2[X], fez, fex);
    p0 = -e2.z * tmp0.x + e2.x * tmp0.z;
    p1 = -e2.z * tmp1.x + e2.x * tmp1.z;
    min = min(p0, p1);
    max = max(p0, p1);
    rad = fez * extent.x + fex * extent.y;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    //   AXISTEST_Z12(e2[Y], e2[X], fey, fex);
    p1 = e2.y * tmp1.x - e2.x * tmp1.y;
    p2 = e2.y * tmp2.x - e2.x * tmp2.y;
    min = min(p1, p2);
    max = max(p1, p2);
    rad = fey * extent.x + fex * extent.y;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    //  Bullet 1:
    //  first test overlap in the {x,y,z}-directions
    //  find min, max of the triangle each direction, and test for overlap in
    //  that direction -- this is equivalent to testing a minimal AABB around
    //  the triangle against the AABB
    Vector3f minMax = vars.vect7;
    // test in X-direction
    findMinMax(tmp0.x, tmp1.x, tmp2.x, minMax);
    if (minMax.x > extent.x || minMax.y < -extent.x) {
        vars.release();
        return false;
    }
    // test in Y-direction
    findMinMax(tmp0.y, tmp1.y, tmp2.y, minMax);
    if (minMax.x > extent.y || minMax.y < -extent.y) {
        vars.release();
        return false;
    }
    // test in Z-direction
    findMinMax(tmp0.z, tmp1.z, tmp2.z, minMax);
    if (minMax.x > extent.z || minMax.y < -extent.z) {
        vars.release();
        return false;
    }
    //       // Bullet 2:
    //       //  test if the box intersects the plane of the triangle
    //       //  compute plane equation of triangle: normal * x + d = 0
    //        Vector3f normal = new Vector3f();
    //        e0.cross(e1, normal);
    Plane p = vars.plane;
    p.setPlanePoints(v1, v2, v3);
    if (bbox.whichSide(p) == Plane.Side.Negative) {
        vars.release();
        return false;
    }
    //
    //        if(!planeBoxOverlap(normal,v0,boxhalfsize)) return false;
    vars.release();
    return true;
/* box and triangle overlaps */
}
Also used : Plane(com.jme3.math.Plane) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 7 with Vector3f

use of com.jme3.math.Vector3f in project jmonkeyengine by jMonkeyEngine.

the class DetailedProfilerState method initialize.

@Override
protected void initialize(Application app) {
    Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setColor("Color", new ColorRGBA(0, 0, 0, 0.5f));
    mat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
    Geometry darkenStats = new Geometry("StatsDarken", new Quad(PANEL_WIDTH, app.getCamera().getHeight()));
    darkenStats.setMaterial(mat);
    darkenStats.setLocalTranslation(0, -app.getCamera().getHeight(), -1);
    ui.attachChild(darkenStats);
    ui.setLocalTranslation(app.getCamera().getWidth() - PANEL_WIDTH, app.getCamera().getHeight(), 0);
    font = app.getAssetManager().loadFont("Interface/Fonts/Console.fnt");
    bigFont = app.getAssetManager().loadFont("Interface/Fonts/Default.fnt");
    prof.setRenderer(app.getRenderer());
    rootLine = new StatLineView("Frame");
    rootLine.attachTo(ui);
    BitmapText frameLabel = new BitmapText(bigFont);
    frameLabel.setText("Total Frame Time: ");
    ui.attachChild(frameLabel);
    frameLabel.setLocalTranslation(new Vector3f(PANEL_WIDTH / 2 - bigFont.getLineWidth(frameLabel.getText()), -PADDING, 0));
    BitmapText cpuLabel = new BitmapText(bigFont);
    cpuLabel.setText("CPU");
    ui.attachChild(cpuLabel);
    cpuLabel.setLocalTranslation(PANEL_WIDTH / 4 - bigFont.getLineWidth(cpuLabel.getText()) / 2, -PADDING - 30, 0);
    BitmapText gpuLabel = new BitmapText(bigFont);
    gpuLabel.setText("GPU");
    ui.attachChild(gpuLabel);
    gpuLabel.setLocalTranslation(3 * PANEL_WIDTH / 4 - bigFont.getLineWidth(gpuLabel.getText()) / 2, -PADDING - 30, 0);
    frameTimeValue = new BitmapText(bigFont);
    frameCpuTimeValue = new BitmapText(bigFont);
    frameGpuTimeValue = new BitmapText(bigFont);
    selectedField = new BitmapText(font);
    selectedField.setText("Selected: ");
    selectedField.setLocalTranslation(PANEL_WIDTH / 2, -PADDING - 75, 0);
    selectedField.setColor(ColorRGBA.Yellow);
    ui.attachChild(frameTimeValue);
    ui.attachChild(frameCpuTimeValue);
    ui.attachChild(frameGpuTimeValue);
    ui.attachChild(selectedField);
    hideInsignificantField = new BitmapText(font);
    hideInsignificantField.setText("O " + INSIGNIFICANT);
    hideInsignificantField.setLocalTranslation(PADDING, -PADDING - 75, 0);
    ui.attachChild(hideInsignificantField);
    final InputManager inputManager = app.getInputManager();
    if (inputManager != null) {
        inputManager.addMapping(TOGGLE_KEY, new KeyTrigger(KeyInput.KEY_F6));
        inputManager.addMapping(CLICK_KEY, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
        inputManager.addListener(new ActionListener() {

            @Override
            public void onAction(String name, boolean isPressed, float tpf) {
                if (name.equals(TOGGLE_KEY) && isPressed) {
                    setEnabled(!isEnabled());
                }
                if (isEnabled() && name.equals(CLICK_KEY) && isPressed) {
                    handleClick(inputManager.getCursorPosition());
                }
            }
        }, TOGGLE_KEY, CLICK_KEY);
    }
}
Also used : Quad(com.jme3.scene.shape.Quad) Material(com.jme3.material.Material) BitmapText(com.jme3.font.BitmapText)

Example 8 with Vector3f

use of com.jme3.math.Vector3f in project jmonkeyengine by jMonkeyEngine.

the class LegacyApplication method initCamera.

/**
     * Creates the camera to use for rendering. Default values are perspective
     * projection with 45° field of view, with near and far values 1 and 1000
     * units respectively.
     */
private void initCamera() {
    cam = new Camera(settings.getWidth(), settings.getHeight());
    cam.setFrustumPerspective(45f, (float) cam.getWidth() / cam.getHeight(), 1f, 1000f);
    cam.setLocation(new Vector3f(0f, 0f, 10f));
    cam.lookAt(new Vector3f(0f, 0f, 0f), Vector3f.UNIT_Y);
    renderManager = new RenderManager(renderer);
    //Remy - 09/14/2010 setted the timer in the renderManager
    renderManager.setTimer(timer);
    if (prof != null) {
        renderManager.setAppProfiler(prof);
    }
    viewPort = renderManager.createMainView("Default", cam);
    viewPort.setClearFlags(true, true, true);
    // Create a new cam for the gui
    Camera guiCam = new Camera(settings.getWidth(), settings.getHeight());
    guiViewPort = renderManager.createPostView("Gui Default", guiCam);
    guiViewPort.setClearFlags(false, false, false);
}
Also used : Vector3f(com.jme3.math.Vector3f) Camera(com.jme3.renderer.Camera) RenderManager(com.jme3.renderer.RenderManager)

Example 9 with Vector3f

use of com.jme3.math.Vector3f in project jmonkeyengine by jMonkeyEngine.

the class BIHNode method intersectWhere.

public final int intersectWhere(Ray r, Matrix4f worldMatrix, BIHTree tree, float sceneMin, float sceneMax, CollisionResults results) {
    TempVars vars = TempVars.get();
    ArrayList<BIHStackData> stack = vars.bihStack;
    stack.clear();
    //        float tHit = Float.POSITIVE_INFINITY;
    Vector3f o = vars.vect1.set(r.getOrigin());
    Vector3f d = vars.vect2.set(r.getDirection());
    Matrix4f inv = vars.tempMat4.set(worldMatrix).invertLocal();
    inv.mult(r.getOrigin(), r.getOrigin());
    // Fixes rotation collision bug
    inv.multNormal(r.getDirection(), r.getDirection());
    //        inv.multNormalAcross(r.getDirection(), r.getDirection());
    float[] origins = { r.getOrigin().x, r.getOrigin().y, r.getOrigin().z };
    float[] invDirections = { 1f / r.getDirection().x, 1f / r.getDirection().y, 1f / r.getDirection().z };
    r.getDirection().normalizeLocal();
    Vector3f v1 = vars.vect3, v2 = vars.vect4, v3 = vars.vect5;
    int cols = 0;
    stack.add(new BIHStackData(this, sceneMin, sceneMax));
    stackloop: while (stack.size() > 0) {
        BIHStackData data = stack.remove(stack.size() - 1);
        BIHNode node = data.node;
        float tMin = data.min, tMax = data.max;
        if (tMax < tMin) {
            continue;
        }
        leafloop: while (node.axis != 3) {
            // while node is not a leaf
            int a = node.axis;
            // find the origin and direction value for the given axis
            float origin = origins[a];
            float invDirection = invDirections[a];
            float tNearSplit, tFarSplit;
            BIHNode nearNode, farNode;
            tNearSplit = (node.leftPlane - origin) * invDirection;
            tFarSplit = (node.rightPlane - origin) * invDirection;
            nearNode = node.left;
            farNode = node.right;
            if (invDirection < 0) {
                float tmpSplit = tNearSplit;
                tNearSplit = tFarSplit;
                tFarSplit = tmpSplit;
                BIHNode tmpNode = nearNode;
                nearNode = farNode;
                farNode = tmpNode;
            }
            if (tMin > tNearSplit && tMax < tFarSplit) {
                continue stackloop;
            }
            if (tMin > tNearSplit) {
                tMin = max(tMin, tFarSplit);
                node = farNode;
            } else if (tMax < tFarSplit) {
                tMax = min(tMax, tNearSplit);
                node = nearNode;
            } else {
                stack.add(new BIHStackData(farNode, max(tMin, tFarSplit), tMax));
                tMax = min(tMax, tNearSplit);
                node = nearNode;
            }
        }
        // a leaf
        for (int i = node.leftIndex; i <= node.rightIndex; i++) {
            tree.getTriangle(i, v1, v2, v3);
            float t = r.intersects(v1, v2, v3);
            if (!Float.isInfinite(t)) {
                if (worldMatrix != null) {
                    worldMatrix.mult(v1, v1);
                    worldMatrix.mult(v2, v2);
                    worldMatrix.mult(v3, v3);
                    float t_world = new Ray(o, d).intersects(v1, v2, v3);
                    t = t_world;
                }
                Vector3f contactNormal = Triangle.computeTriangleNormal(v1, v2, v3, null);
                Vector3f contactPoint = new Vector3f(d).multLocal(t).addLocal(o);
                float worldSpaceDist = o.distance(contactPoint);
                CollisionResult cr = new CollisionResult(contactPoint, worldSpaceDist);
                cr.setContactNormal(contactNormal);
                cr.setTriangleIndex(tree.getTriangleIndex(i));
                results.addCollision(cr);
                cols++;
            }
        }
    }
    vars.release();
    r.setOrigin(o);
    r.setDirection(d);
    return cols;
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) Matrix4f(com.jme3.math.Matrix4f) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars) Ray(com.jme3.math.Ray)

Example 10 with Vector3f

use of com.jme3.math.Vector3f in project jmonkeyengine by jMonkeyEngine.

the class BIHNode method intersectBrute.

public final int intersectBrute(Ray r, Matrix4f worldMatrix, BIHTree tree, float sceneMin, float sceneMax, CollisionResults results) {
    float tHit = Float.POSITIVE_INFINITY;
    TempVars vars = TempVars.get();
    Vector3f v1 = vars.vect1, v2 = vars.vect2, v3 = vars.vect3;
    int cols = 0;
    ArrayList<BIHStackData> stack = vars.bihStack;
    stack.clear();
    stack.add(new BIHStackData(this, 0, 0));
    stackloop: while (stack.size() > 0) {
        BIHStackData data = stack.remove(stack.size() - 1);
        BIHNode node = data.node;
        leafloop: while (node.axis != 3) {
            // while node is not a leaf
            BIHNode nearNode, farNode;
            nearNode = node.left;
            farNode = node.right;
            stack.add(new BIHStackData(farNode, 0, 0));
            node = nearNode;
        }
        // a leaf
        for (int i = node.leftIndex; i <= node.rightIndex; i++) {
            tree.getTriangle(i, v1, v2, v3);
            if (worldMatrix != null) {
                worldMatrix.mult(v1, v1);
                worldMatrix.mult(v2, v2);
                worldMatrix.mult(v3, v3);
            }
            float t = r.intersects(v1, v2, v3);
            if (t < tHit) {
                tHit = t;
                Vector3f contactPoint = new Vector3f(r.direction).multLocal(tHit).addLocal(r.origin);
                CollisionResult cr = new CollisionResult(contactPoint, tHit);
                cr.setTriangleIndex(tree.getTriangleIndex(i));
                results.addCollision(cr);
                cols++;
            }
        }
    }
    vars.release();
    return cols;
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Aggregations

Vector3f (com.jme3.math.Vector3f)536 Material (com.jme3.material.Material)126 Geometry (com.jme3.scene.Geometry)118 DirectionalLight (com.jme3.light.DirectionalLight)95 Quaternion (com.jme3.math.Quaternion)94 TempVars (com.jme3.util.TempVars)67 ColorRGBA (com.jme3.math.ColorRGBA)64 Node (com.jme3.scene.Node)63 Spatial (com.jme3.scene.Spatial)57 Box (com.jme3.scene.shape.Box)57 Sphere (com.jme3.scene.shape.Sphere)51 Texture (com.jme3.texture.Texture)41 KeyTrigger (com.jme3.input.controls.KeyTrigger)36 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)35 Vector2f (com.jme3.math.Vector2f)35 FilterPostProcessor (com.jme3.post.FilterPostProcessor)34 FloatBuffer (java.nio.FloatBuffer)34 InputCapsule (com.jme3.export.InputCapsule)33 BoundingBox (com.jme3.bounding.BoundingBox)30 AmbientLight (com.jme3.light.AmbientLight)30