Java Code Examples for com.jme3.bounding.BoundingBox#collideWith()

The following examples show how to use com.jme3.bounding.BoundingBox#collideWith() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestRayCollision.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void main(String[] args){
    Ray r = new Ray(Vector3f.ZERO, Vector3f.UNIT_X);
    BoundingBox bbox = new BoundingBox(new Vector3f(5, 0, 0), 1, 1, 1);

    CollisionResults res = new CollisionResults();
    bbox.collideWith(r, res);

    System.out.println("Bounding:" +bbox);
    System.out.println("Ray: "+r);

    System.out.println("Num collisions: "+res.size());
    for (int i = 0; i < res.size(); i++){
        System.out.println("--- Collision #"+i+" ---");
        float dist = res.getCollision(i).getDistance();
        Vector3f pt = res.getCollision(i).getContactPoint();
        System.out.println("distance: "+dist);
        System.out.println("point: "+pt);
    }
}
 
Example 2
Source File: TestRayCollision.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void main(String[] args){
    Ray r = new Ray(Vector3f.ZERO, Vector3f.UNIT_X);
    BoundingBox bbox = new BoundingBox(new Vector3f(5, 0, 0), 1, 1, 1);

    CollisionResults res = new CollisionResults();
    bbox.collideWith(r, res);

    System.out.println("Bounding:" +bbox);
    System.out.println("Ray: "+r);

    System.out.println("Num collisions: "+res.size());
    for (int i = 0; i < res.size(); i++){
        System.out.println("--- Collision #"+i+" ---");
        float dist = res.getCollision(i).getDistance();
        Vector3f pt = res.getCollision(i).getContactPoint();
        System.out.println("distance: "+dist);
        System.out.println("point: "+pt);
    }
}
 
Example 3
Source File: TerrainPatch.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * This most definitely is not optimized.
 */
private int collideWithBoundingBox(BoundingBox bbox, CollisionResults results) {

    // test the four corners, for cases where the bbox dimensions are less than the terrain grid size, which is probably most of the time
    Vector3f topLeft = worldCoordinateToLocal(new Vector3f(bbox.getCenter().x-bbox.getXExtent(), 0, bbox.getCenter().z-bbox.getZExtent()));
    Vector3f topRight = worldCoordinateToLocal(new Vector3f(bbox.getCenter().x+bbox.getXExtent(), 0, bbox.getCenter().z-bbox.getZExtent()));
    Vector3f bottomLeft = worldCoordinateToLocal(new Vector3f(bbox.getCenter().x-bbox.getXExtent(), 0, bbox.getCenter().z+bbox.getZExtent()));
    Vector3f bottomRight = worldCoordinateToLocal(new Vector3f(bbox.getCenter().x+bbox.getXExtent(), 0, bbox.getCenter().z+bbox.getZExtent()));

    Triangle t = getTriangle(topLeft.x, topLeft.z);
    if (t != null && bbox.collideWith(t, results) > 0)
        return 1;
    t = getTriangle(topRight.x, topRight.z);
    if (t != null && bbox.collideWith(t, results) > 0)
        return 1;
    t = getTriangle(bottomLeft.x, bottomLeft.z);
    if (t != null && bbox.collideWith(t, results) > 0)
        return 1;
    t = getTriangle(bottomRight.x, bottomRight.z);
    if (t != null && bbox.collideWith(t, results) > 0)
        return 1;

    // box is larger than the points on the terrain, so test against the points
    for (float z=topLeft.z; z<bottomLeft.z; z+=1) {
        for (float x=topLeft.x; x<topRight.x; x+=1) {

            if (x < 0 || z < 0 || x >= size || z >= size)
                continue;
            t = getTriangle(x,z);
            if (t != null && bbox.collideWith(t, results) > 0)
                return 1;
        }
    }

    return 0;
}
 
Example 4
Source File: TerrainPatch.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * This most definitely is not optimized.
 */
private int collideWithBoundingBox(BoundingBox bbox, CollisionResults results) {
    
    // test the four corners, for cases where the bbox dimensions are less than the terrain grid size, which is probably most of the time
    Vector3f topLeft = worldCoordinateToLocal(new Vector3f(bbox.getCenter().x-bbox.getXExtent(), 0, bbox.getCenter().z-bbox.getZExtent()));
    Vector3f topRight = worldCoordinateToLocal(new Vector3f(bbox.getCenter().x+bbox.getXExtent(), 0, bbox.getCenter().z-bbox.getZExtent()));
    Vector3f bottomLeft = worldCoordinateToLocal(new Vector3f(bbox.getCenter().x-bbox.getXExtent(), 0, bbox.getCenter().z+bbox.getZExtent()));
    Vector3f bottomRight = worldCoordinateToLocal(new Vector3f(bbox.getCenter().x+bbox.getXExtent(), 0, bbox.getCenter().z+bbox.getZExtent()));

    Triangle t = getTriangle(topLeft.x, topLeft.z);
    if (t != null && bbox.collideWith(t, results) > 0)
        return 1;
    t = getTriangle(topRight.x, topRight.z);
    if (t != null && bbox.collideWith(t, results) > 0)
        return 1;
    t = getTriangle(bottomLeft.x, bottomLeft.z);
    if (t != null && bbox.collideWith(t, results) > 0)
        return 1;
    t = getTriangle(bottomRight.x, bottomRight.z);
    if (t != null && bbox.collideWith(t, results) > 0)
        return 1;
    
    // box is larger than the points on the terrain, so test against the points
    for (float z=topLeft.z; z<bottomLeft.z; z+=1) {
        for (float x=topLeft.x; x<topRight.x; x+=1) {
            
            if (x < 0 || z < 0 || x >= size || z >= size)
                continue;
            t = getTriangle(x,z);
            if (t != null && bbox.collideWith(t, results) > 0)
                return 1;
        }
    }

    return 0;
}