Java Code Examples for com.jme3.collision.CollisionResult#getGeometry()

The following examples show how to use com.jme3.collision.CollisionResult#getGeometry() . 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: BresenhamTerrainPicker.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * This method adds the found Collision to an existing collisionResult.
 * @param results The results to add this collision to
 * @param patch The TerrainPatch which collided
 * @param intersection The actual intersection position
 * @param hit The hit triangle
 * @param distance The distance at which the hit occurred
 * @return Whether the collision was accepted to the list or whether it has been deduplicated
 */
private boolean addCollision(CollisionResults results, TerrainPatch patch, Vector3f intersection, Triangle hit, float distance) {
    CollisionResult cr = new CollisionResult(intersection.clone(), distance);
    cr.setGeometry(patch);
    cr.setContactNormal(hit.getNormal());
    cr.setTriangleIndex(hit.getIndex()); // this will probably always be 0

    for (int i = 0; i < results.size(); i++) {
        CollisionResult compare = results.getCollision(i);
        if (compare.getDistance() == cr.getDistance() && compare.getGeometry() == cr.getGeometry() &&
            compare.getContactPoint().equals(cr.getContactPoint()) &&
            compare.getContactNormal().equals(cr.getContactNormal())) {
                return false; // Collision already available, deduplicate.
        }
    }

    results.addCollision(cr);
    return true;
}
 
Example 2
Source File: TestObjGroupsLoading.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void simpleUpdate(final float tpf) {
    
    // ray to the center of the screen from the camera
    Ray ray = new Ray(cam.getLocation(), cam.getDirection());
    
    // find object at the center of the screen

    final CollisionResults results = new CollisionResults();
    rootNode.collideWith(ray, results);
    
    CollisionResult result = results.getClosestCollision();
    if (result == null) {
        pointerDisplay.setText("");
    } else {
        // display pointed geometry and it's parents names
        StringBuilder sb = new StringBuilder();
        for (Spatial node = result.getGeometry(); node != null; node = node.getParent()) {
            if (sb.length() > 0) {
                sb.append(" < ");
            }
            sb.append(node.getName());
        }
        pointerDisplay.setText(sb);
    }
}
 
Example 3
Source File: AbstractTransformControl.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
@JmeThread
protected void detectPickedAxis(@NotNull final EditorTransformSupport editorControl,
                                @NotNull final CollisionResult collisionResult) {

    final Geometry geometry = collisionResult.getGeometry();
    final String geometryName = geometry.getName();

    if (geometryName.contains(getNodeX())) {
        editorControl.setPickedAxis(PickedAxis.X);
    } else if (geometryName.contains(getNodeY())) {
        editorControl.setPickedAxis(PickedAxis.Y);
    } else if (geometryName.contains(getNodeZ())) {
        editorControl.setPickedAxis(PickedAxis.Z);
    }
}
 
Example 4
Source File: SelectionState.java    From Lemur with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void update( float tpf ) {
    super.update(tpf);

    long time = System.nanoTime();
    if( time - lastSample < sampleFrequency )
        return;
    lastSample = time;

    Vector2f cursor = getApplication().getInputManager().getCursorPosition();
    CollisionResults collisions = getCollisions(cursor);

    for( CollisionResult cr : collisions ) {
        Geometry geom = cr.getGeometry();
        if( geom == selected ) {
            // If the hover is already the selection then
            // don't bother changinge
            if( geom == hover ) {
                return;
            }
        }
        if( isIgnored(geom) ) {
            continue;
        }
        setHover(geom);
        return;
    }
    
    // Else clear the hover
    setHover(null);
}
 
Example 5
Source File: PointUtil.java    From OpenRTS with MIT License 5 votes vote down vote up
public static Geometry getPointedGeometry(Node n, Ray r) {
	CollisionResult collision = getCollision(n, r);
	if (collision == null) {
		return null;
	}
	return collision.getGeometry();
}
 
Example 6
Source File: SceneEditTool.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Given the mouse coordinates, pick the geometry that is closest to the camera.
 * @param jmeRootNode to pick from
 * @return the selected spatial, or null if nothing
 */
protected Spatial pickWorldSpatial(Camera cam, Vector2f mouseLoc, JmeNode jmeRootNode) {
    Node rootNode = jmeRootNode.getLookup().lookup(Node.class);
    CollisionResult cr = pick(cam, mouseLoc, rootNode);
    if (cr != null) {
        return cr.getGeometry();
    } else {
        return null;
    }
}
 
Example 7
Source File: AbstractSceneEditor3DPart.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
/**
 * Update editing nodes.
 */
@JmeThread
private void updatePaintingNodes() {

    if (!isPaintingMode()) {
        return;
    }

    final Node cursorNode = getCursorNode();
    final PaintingControl control = PaintingUtils.getPaintingControl(cursorNode);
    final Spatial paintedModel = PaintingUtils.getPaintedModel(control);
    if (paintedModel == null) {
        return;
    }

    final CollisionResults collisions = GeomUtils.getCollisionsFromCursor(paintedModel, getCamera());
    if (collisions.size() < 1) {
        return;
    }

    CollisionResult result = null;

    for (final CollisionResult collision : collisions) {

        final Geometry geometry = collision.getGeometry();
        final Object parent = NodeUtils.findParent(geometry, spatial ->
                spatial.getUserData(KEY_IGNORE_RAY_CAST) == Boolean.TRUE);

        if (parent == null) {
            result = collision;
            break;
        }
    }

    if (result == null) {
        result = collisions.getClosestCollision();
    }

    final Vector3f contactPoint = result.getContactPoint();
    final Vector3f contactNormal = result.getContactNormal();

    final LocalObjects local = LocalObjects.get();
    final Quaternion rotation = local.nextRotation();
    rotation.lookAt(contactNormal, Vector3f.UNIT_Y);

    cursorNode.setLocalRotation(rotation);
    cursorNode.setLocalTranslation(contactPoint);
}
 
Example 8
Source File: SceneEditTool.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Pick a part of the axis marker. The result is a Vector3f that represents
 * what part of the axis was selected.
 * For example if  (1,0,0) is returned, then the X-axis pole was selected.
 * If (0,1,1) is returned, then the Y-Z plane was selected.
 * 
 * @return null if it did not intersect the marker
 */
protected Vector3f pickAxisMarker(Camera cam, Vector2f mouseLoc, AxisMarkerPickType pickType) {
    if (axisMarker == null) {
        return null;
    }

    CollisionResult cr = pick(cam, mouseLoc, axisMarker);
    if (cr == null || cr.getGeometry() == null) {
        return null;
    }

    if (pickType == AxisMarkerPickType.planeOnly) {
        if ("quadXY".equals(cr.getGeometry().getName())) {
            return QUAD_XY;
        } else if ("quadXZ".equals(cr.getGeometry().getName())) {
            return QUAD_XZ;
        } else if ("quadYZ".equals(cr.getGeometry().getName())) {
            return QUAD_YZ;
        }
    } else if (pickType == AxisMarkerPickType.axisOnly) {
        if ("arrowX".equals(cr.getGeometry().getName())) {
            return ARROW_X;
        } else if ("arrowY".equals(cr.getGeometry().getName())) {
            return ARROW_Y;
        } else if ("arrowZ".equals(cr.getGeometry().getName())) {
            return ARROW_Z;
        }
    } else if (pickType == AxisMarkerPickType.axisAndPlane) {
        if ("arrowX".equals(cr.getGeometry().getName())) {
            return ARROW_X;
        } else if ("arrowY".equals(cr.getGeometry().getName())) {
            return ARROW_Y;
        } else if ("arrowZ".equals(cr.getGeometry().getName())) {
            return ARROW_Z;
        } else if ("quadXY".equals(cr.getGeometry().getName())) {
            return QUAD_XY;
        } else if ("quadXZ".equals(cr.getGeometry().getName())) {
            return QUAD_XZ;
        } else if ("quadYZ".equals(cr.getGeometry().getName())) {
            return QUAD_YZ;
        }
    }
    return null;
}