Search in sources :

Example 16 with Ray

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

the class BetterCharacterControl method checkOnGround.

/**
     * This checks if the character is on the ground by doing a ray test.
     */
protected void checkOnGround() {
    TempVars vars = TempVars.get();
    Vector3f location = vars.vect1;
    Vector3f rayVector = vars.vect2;
    float height = getFinalHeight();
    location.set(localUp).multLocal(height).addLocal(this.location);
    rayVector.set(localUp).multLocal(-height - 0.1f).addLocal(location);
    List<PhysicsRayTestResult> results = space.rayTest(location, rayVector);
    vars.release();
    for (PhysicsRayTestResult physicsRayTestResult : results) {
        if (!physicsRayTestResult.getCollisionObject().equals(rigidBody)) {
            onGround = true;
            return;
        }
    }
    onGround = false;
}
Also used : PhysicsRayTestResult(com.jme3.bullet.collision.PhysicsRayTestResult) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 17 with Ray

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

the class BoundingBox method intersects.

/**
     * determines if this bounding box intersects with a given oriented bounding
     * box.
     * 
     * @see com.jme.bounding.BoundingVolume#intersectsOrientedBoundingBox(com.jme.bounding.OrientedBoundingBox)
     */
//    public boolean intersectsOrientedBoundingBox(OrientedBoundingBox obb) {
//        return obb.intersectsBoundingBox(this);
//    }
/**
     * determines if this bounding box intersects with a given ray object. If an
     * intersection has occurred, true is returned, otherwise false is returned.
     * 
     * @see BoundingVolume#intersects(com.jme3.math.Ray) 
     */
public boolean intersects(Ray ray) {
    assert Vector3f.isValidVector(center);
    float rhs;
    TempVars vars = TempVars.get();
    Vector3f diff = ray.origin.subtract(getCenter(vars.vect2), vars.vect1);
    final float[] fWdU = vars.fWdU;
    final float[] fAWdU = vars.fAWdU;
    final float[] fDdU = vars.fDdU;
    final float[] fADdU = vars.fADdU;
    final float[] fAWxDdU = vars.fAWxDdU;
    fWdU[0] = ray.getDirection().dot(Vector3f.UNIT_X);
    fAWdU[0] = FastMath.abs(fWdU[0]);
    fDdU[0] = diff.dot(Vector3f.UNIT_X);
    fADdU[0] = FastMath.abs(fDdU[0]);
    if (fADdU[0] > xExtent && fDdU[0] * fWdU[0] >= 0.0) {
        vars.release();
        return false;
    }
    fWdU[1] = ray.getDirection().dot(Vector3f.UNIT_Y);
    fAWdU[1] = FastMath.abs(fWdU[1]);
    fDdU[1] = diff.dot(Vector3f.UNIT_Y);
    fADdU[1] = FastMath.abs(fDdU[1]);
    if (fADdU[1] > yExtent && fDdU[1] * fWdU[1] >= 0.0) {
        vars.release();
        return false;
    }
    fWdU[2] = ray.getDirection().dot(Vector3f.UNIT_Z);
    fAWdU[2] = FastMath.abs(fWdU[2]);
    fDdU[2] = diff.dot(Vector3f.UNIT_Z);
    fADdU[2] = FastMath.abs(fDdU[2]);
    if (fADdU[2] > zExtent && fDdU[2] * fWdU[2] >= 0.0) {
        vars.release();
        return false;
    }
    Vector3f wCrossD = ray.getDirection().cross(diff, vars.vect2);
    fAWxDdU[0] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_X));
    rhs = yExtent * fAWdU[2] + zExtent * fAWdU[1];
    if (fAWxDdU[0] > rhs) {
        vars.release();
        return false;
    }
    fAWxDdU[1] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_Y));
    rhs = xExtent * fAWdU[2] + zExtent * fAWdU[0];
    if (fAWxDdU[1] > rhs) {
        vars.release();
        return false;
    }
    fAWxDdU[2] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_Z));
    rhs = xExtent * fAWdU[1] + yExtent * fAWdU[0];
    if (fAWxDdU[2] > rhs) {
        vars.release();
        return false;
    }
    vars.release();
    return true;
}
Also used : TempVars(com.jme3.util.TempVars)

Example 18 with Ray

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

the class BoundingBox method collideWithRay.

private int collideWithRay(Ray ray) {
    TempVars vars = TempVars.get();
    try {
        Vector3f diff = vars.vect1.set(ray.origin).subtractLocal(center);
        Vector3f direction = vars.vect2.set(ray.direction);
        //float[] t = {0f, Float.POSITIVE_INFINITY};
        // use one of the tempvars arrays
        float[] t = vars.fWdU;
        t[0] = 0;
        t[1] = Float.POSITIVE_INFINITY;
        float saveT0 = t[0], saveT1 = t[1];
        boolean notEntirelyClipped = clip(+direction.x, -diff.x - xExtent, t) && clip(-direction.x, +diff.x - xExtent, t) && clip(+direction.y, -diff.y - yExtent, t) && clip(-direction.y, +diff.y - yExtent, t) && clip(+direction.z, -diff.z - zExtent, t) && clip(-direction.z, +diff.z - zExtent, t);
        if (notEntirelyClipped && (t[0] != saveT0 || t[1] != saveT1)) {
            if (t[1] > t[0])
                return 2;
            else
                return 1;
        }
        return 0;
    } finally {
        vars.release();
    }
}
Also used : TempVars(com.jme3.util.TempVars)

Example 19 with Ray

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

the class BoundingSphere method intersects.

/*
     * (non-Javadoc)
     *
     * @see com.jme.bounding.BoundingVolume#intersectsOrientedBoundingBox(com.jme.bounding.OrientedBoundingBox)
     */
//    public boolean intersectsOrientedBoundingBox(OrientedBoundingBox obb) {
//        return obb.intersectsSphere(this);
//    }
/*
     * (non-Javadoc)
     *
     * @see com.jme.bounding.BoundingVolume#intersects(com.jme.math.Ray)
     */
public boolean intersects(Ray ray) {
    assert Vector3f.isValidVector(center);
    TempVars vars = TempVars.get();
    Vector3f diff = vars.vect1.set(ray.getOrigin()).subtractLocal(center);
    float radiusSquared = getRadius() * getRadius();
    float a = diff.dot(diff) - radiusSquared;
    if (a <= 0.0) {
        vars.release();
        // in sphere
        return true;
    }
    // outside sphere
    float b = ray.getDirection().dot(diff);
    vars.release();
    if (b >= 0.0) {
        return false;
    }
    return b * b >= a;
}
Also used : TempVars(com.jme3.util.TempVars)

Example 20 with Ray

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

the class RayTrace method update.

public void update() {
    int w = image.getWidth();
    int h = image.getHeight();
    float wr = (float) cam.getWidth() / image.getWidth();
    float hr = (float) cam.getHeight() / image.getHeight();
    scene.updateGeometricState();
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            Vector2f v = new Vector2f(x * wr, y * hr);
            Vector3f pos = cam.getWorldCoordinates(v, 0.0f);
            Vector3f dir = cam.getWorldCoordinates(v, 0.3f);
            dir.subtractLocal(pos).normalizeLocal();
            Ray r = new Ray(pos, dir);
            results.clear();
            scene.collideWith(r, results);
            if (results.size() > 0) {
                image.setRGB(x, h - y - 1, 0xFFFFFFFF);
            } else {
                image.setRGB(x, h - y - 1, 0xFF000000);
            }
        }
    }
    label.repaint();
}
Also used : Vector2f(com.jme3.math.Vector2f) Vector3f(com.jme3.math.Vector3f) Ray(com.jme3.math.Ray)

Aggregations

Vector3f (com.jme3.math.Vector3f)13 TempVars (com.jme3.util.TempVars)12 CollisionResult (com.jme3.collision.CollisionResult)10 CollisionResults (com.jme3.collision.CollisionResults)8 Ray (com.jme3.math.Ray)8 BoundingBox (com.jme3.bounding.BoundingBox)3 Vector2f (com.jme3.math.Vector2f)3 PhysicsRayTestResult (com.jme3.bullet.collision.PhysicsRayTestResult)2 UnsupportedCollisionException (com.jme3.collision.UnsupportedCollisionException)2 Spatial (com.jme3.scene.Spatial)2 Matrix4f (com.jme3.math.Matrix4f)1 Quaternion (com.jme3.math.Quaternion)1 Triangle (com.jme3.math.Triangle)1 VertexBuffer (com.jme3.scene.VertexBuffer)1 TerrainPatch (com.jme3.terrain.geomipmap.TerrainPatch)1 Direction (com.jme3.terrain.geomipmap.picking.BresenhamYUpGridTracer.Direction)1 TerrainPickData (com.jme3.terrain.geomipmap.picking.TerrainPickData)1 FloatBuffer (java.nio.FloatBuffer)1 IntBuffer (java.nio.IntBuffer)1 ShortBuffer (java.nio.ShortBuffer)1