public boolean checkCollision(int collisionIndex, PickResult[] resultArray) { // System.out.println(resultArray); if (resultArray == null || resultArray.length == 0 || resultArray[0] == null) return false; /* * We use the user data on the nodes to ignore the case of the * collisionObject having collided with itself! The user data also gives * us a good mechanism for reporting the collisions. */ for (int n = 0; n < resultArray.length; n++) { // System.out.println("something "+resultArray[n]); int numIntersections = resultArray[n].numIntersections(); for (int i = 0; i < numIntersections; i++) { PickIntersection pi = resultArray[n].getIntersection(i); if (pi.getDistance() < 1) { // System.out.println(resultArray[n]); // int numNodes = resultArray[n]. // for (int j = 0; ) Node node = resultArray[n].getObject(); System.out.println(node); if (node == PlayFrame.COLLISION_WALLS[PlayFrame.WALL_FRONT]) { // System.out.println("front"); incrementVector[collisionIndex].z = -incrementVector[collisionIndex].z; } else if (node == PlayFrame.COLLISION_WALLS[PlayFrame.WALL_BACK]) { // System.out.println("back"); incrementVector[collisionIndex].z = -incrementVector[collisionIndex].z; } else if (node == PlayFrame.COLLISION_WALLS[PlayFrame.WALL_LEFT]) { // System.out.println("left"); incrementVector[collisionIndex].x = -incrementVector[collisionIndex].x; } else if (node == PlayFrame.COLLISION_WALLS[PlayFrame.WALL_RIGHT]) { // System.out.println("right"); incrementVector[collisionIndex].x = -incrementVector[collisionIndex].x; } else if (collisionIndex >= 3 && "Me".equals(node.getUserData())) { incrementVector[collisionIndex].negate(); } else if (collisionIndex < 3 && "Enemy".equals(node.getUserData())) { incrementVector[collisionIndex].negate(); } return true; } } } return false; }
private MyPickResult getMouseIntersection2(MouseEvent e) { MyPickResult myResult = new MyPickResult(); Point3d closestObjectPos = null; Node closestObject = null; pickCanvas2.setShapeLocation(e); PickResult[] allResults = null; try { allResults = pickCanvas2.pickAllSorted(); } catch (AssertionError ex) { } ; if (allResults != null) { for (PickResult result : allResults) { Shape3D s = (Shape3D) result.getNode(PickResult.SHAPE3D); if (s != null) { if (closestObjectPos != null) { break; } try { PickIntersection intersection = result.getClosestIntersection(cameraPos); closestObjectPos = intersection.getPointCoordinatesVW(); closestObject = s; } catch (NullPointerException ex) { } ; } } myResult.setPoint(closestObjectPos); myResult.setNode(closestObject); } return myResult; }
/** Handles highlighting for edges */ public void highlight(boolean on, Object parameter) { // Should always be a pick result since those Shape3D do not appear in // the JTree if (parameter instanceof PickResult) { PickResult result = (PickResult) parameter; result.setFirstIntersectOnly(true); PickIntersection pi = result.getIntersection(0); // indices of the picked line // should always be line at this point // Indices are set to vertex indices, as this is not an Index // Geometry object int[] idx = pi.getPrimitiveCoordinateIndices(); Point3d point3d = pi.getPointCoordinates(); double[] point = new double[3]; point3d.get(point); FloatBuffer coords = (FloatBuffer) (pi.getGeometryArray()).getCoordRefBuffer().getBuffer(); float[] pt1 = new float[3]; float[] pt2 = new float[3]; coords.position(idx[0] * 3); coords.get(pt1); coords.position(idx[1] * 3); coords.get(pt2); int[] gpt1 = getGridCoordinate(pt1); int[] gpt2 = getGridCoordinate(pt2); int dim = 0; // lines are parallel to one of the axis => only one coordinate // changes if (gpt1[0] != gpt2[0]) dim = 0; else if (gpt1[1] != gpt2[1]) dim = 1; else if (gpt1[2] != gpt2[2]) dim = 2; else System.err.println("Error: edge is not parallel to one of the axis"); // use gpt1 and gpt2 as a variables for the new point => destroy // previous content gpt1[dim] = (int) Math.floor( gpt1[dim] + (point[dim] - pt1[dim]) * (gpt2[dim] - gpt1[dim]) / (pt2[dim] - pt1[dim])); System.out.println( "Edge end 0 vertex grid coordinates = (" + gpt1[0] + ", " + gpt1[1] + ", " + gpt1[2] + ")"); gpt2[dim] = gpt1[dim] + 1; System.out.println( "Edge end 1 vertex grid coordinates = (" + gpt2[0] + ", " + gpt2[1] + ", " + gpt2[2] + ")"); System.out.println("pi.getGeometryArray()=" + pi.getGeometryArray()); System.out.println( "pi.getGeometryArray().getUserData()=" + pi.getGeometryArray().getUserData()); // handle wire case Object userData = pi.getGeometryArray().getUserData(); if (userData != null && userData instanceof int[]) { int[] info = (int[]) userData; if (info[0] < 0) { float[] color = getColorForOrder(info[0], on ? 2 : 0); idx = pi.getPrimitiveColorIndices(); FloatBuffer colors = (FloatBuffer) (pi.getGeometryArray()).getColorRefBuffer().getBuffer(); colors.position(idx[0] * 3); colors.put(color); colors.position(idx[1] * 3); colors.put(color); } toggleSelectedEdge(on, new SelectionEdge(gpt1, gpt2, info[0], info[1])); } } // event propagation System.out.println(System.currentTimeMillis() + " end of highlight"); }
/* * (non-Javadoc) * * @see syn3d.base.ActiveNode#highlight(boolean, java.lang.Object) */ public void highlight(boolean on, Object parameter) { System.out.println("Total memory: " + Runtime.getRuntime().totalMemory()); System.out.println("Free memory: " + Runtime.getRuntime().freeMemory()); System.out.println(System.currentTimeMillis() + " starting highlight with " + parameter); if (parameter instanceof PickResult) { PickResult result = (PickResult) parameter; result.setFirstIntersectOnly(true); PickIntersection pi = result.getIntersection(0); // indices of the picked quad // Indices are set to vertex indices, as this is not an Index // Geometry object // => easy to find the plate index from this int[] idx = pi.getPrimitiveCoordinateIndices(); int plateNum = idx[0] / 4; Plate p = plates[plateNum]; Point3d point3d = pi.getPointCoordinates(); point3d.get(point); FloatBuffer coords = (FloatBuffer) ((NioQuadArray) (shape.getGeometry())).getCoordRefBuffer().getBuffer(); for (int i = 0; i < idx.length; ++i) { coords.position(idx[i] * 3); coords.get(vertices[i]); } int d1 = 0, d2 = 0; if (p instanceof PlateX) { d1 = 1; d2 = 2; } else if (p instanceof PlateY) { d1 = 0; d2 = 2; } else if (p instanceof PlateZ) { d1 = 0; d2 = 1; } int u = (int) Math.floor( (point[d1] - vertices[0][d1]) * (p.max1 - p.min1) / (vertices[3][d1] - vertices[0][d1])); int v = (int) Math.floor( (point[d2] - vertices[0][d2]) * (p.max2 - p.min2) / (vertices[1][d2] - vertices[0][d2])); int quadIdx = v * (p.max1 - p.min1) + u; u += p.min1; v += p.min2; System.out.println( (on ? "" : "de") + "selected quad " + quadIdx + " in plate " + plateNum + " in group "); System.out.println("Grid positions for the quad (x,y,z) indices:"); int[] pos = p.getXYZGridIndices(u, v); System.out.println("vertex1 = (" + pos[0] + ", " + pos[1] + ", " + pos[2] + ")"); pos = p.getXYZGridIndices(u, v + 1); System.out.println("vertex2 = (" + pos[0] + ", " + pos[1] + ", " + pos[2] + ")"); pos = p.getXYZGridIndices(u + 1, v + 1); System.out.println("vertex3 = (" + pos[0] + ", " + pos[1] + ", " + pos[2] + ")"); pos = p.getXYZGridIndices(u + 1, v); System.out.println("vertex4 = (" + pos[0] + ", " + pos[1] + ", " + pos[2] + ")"); float[] color = getColorForOrder(groupIdx, on ? 1 : 0); for (int i = 0; i < idx.length; ++i) { colors.position(idx[i] * 3); colors.put(color); } toggleSelectedQuad(on, new SelectionQuad(p, u, v, groupIdx)); // Use event propagation, but don't call // setAppearanceForHighlight FloatBuffer tmp = colors; colors = null; colors = tmp; } System.out.println(System.currentTimeMillis() + " end of highlight"); }
private MyPickResult getMouseIntersection(MouseEvent e, boolean withSnap) { MyPickResult myResult = new MyPickResult(); Point3d closestObjectPos = null; Point3d closestMarkPos = null; Node closestObject = null; Node closestMark = null; // 0: closest object; 1: closest mark pickCanvas.setShapeLocation(e); PickResult[] allResults = null; try { allResults = pickCanvas.pickAllSorted(); } catch (AssertionError ex) { } if (allResults != null) { for (PickResult result : allResults) { Shape3D s = (Shape3D) result.getNode(PickResult.SHAPE3D); if (s != null) { // only pick a Shape3D object if (closestObjectPos != null && closestMarkPos != null) { break; } try { PickIntersection intersection = result.getClosestIntersection(cameraPos); if (!(s.getParent().getParent().getParent() instanceof Mark) && !((s.getParent().getParent().getParent() instanceof Marker))) { // markdot/markline/label -> TG -> BG -> Mark/Marker try { closestObjectPos = intersection.getPointCoordinatesVW(); closestObject = s; } catch (NullPointerException ex) { } } else if ((s.getParent().getParent().getParent() instanceof Mark)) { try { closestMarkPos = intersection.getPointCoordinatesVW(); closestMark = s; } catch (NullPointerException ex) { } } } catch (NullPointerException ex) { } } } Point3d resultPos = null; Node resultNode = null; if (closestMark != null) { resultPos = closestMarkPos; // priority of result -> mark resultNode = closestMark; } else if (closestObject != null) { resultPos = closestObjectPos; resultNode = closestObject; } if (withSnap) { // snap resultPos to correct position // start trying to if cursor are inside object bounding sphere if ((closestObjectPos != null && bs.intersect(closestObjectPos)) || closestMarkPos != null && bs.intersect(closestMarkPos)) { Point3d closestObjectPos2 = null; Point3d closestMarkPos2 = null; Node closestObject2 = null; Node closestMark2 = null; // initiate snap bounds if hasn't been initiated before // "snappable" targets to be considered are the ones inside threshold if (cursorBound == null) { cursorBound = new BoundingSphere(resultPos, CURSOR_THRESHOLD); } if (thresholdBound == null) { thresholdBound = new BoundingSphere(resultPos, SNAP_THRESHOLD); } // update the bounds // only if the cursor is no longer inside current cursor bounds if (!cursorBound.intersect(resultPos)) { cursorBound.setCenter(resultPos); thresholdBound.setCenter(resultPos); } // snap to nearest point in the list of snap targets double nearestDistance = FARTHEST; double distance; Point3d nearestTarget = null; // snap to vertex. all vertexes listed in snapList if (snapList.size() > 0) { for (Point3d snapTarget : snapList) { if (thresholdBound.intersect(snapTarget)) { distance = snapTarget.distance(resultPos); if (distance < nearestDistance) { nearestDistance = distance; nearestTarget = snapTarget; } if (nearestTarget != null) { resultPos.set(nearestTarget); // snap! // pick the snapped node Vector3d camToSnapped = new Vector3d(); camToSnapped.x = resultPos.x - cameraPos.x; camToSnapped.y = resultPos.y - cameraPos.y; camToSnapped.z = resultPos.z - cameraPos.z; pickCanvas.setShapeRay(cameraPos, camToSnapped); PickResult[] allResultsSnapped = null; try { allResultsSnapped = pickCanvas.pickAllSorted(); } catch (AssertionError ex) { } if (allResultsSnapped != null) { for (PickResult result : allResultsSnapped) { Shape3D s = (Shape3D) result.getNode(PickResult.SHAPE3D); if (s != null) { // only pick a Shape3D object if (closestObjectPos2 != null && closestMarkPos2 != null) { break; } try { PickIntersection intersection = result.getClosestIntersection(cameraPos); if (!(s.getParent().getParent().getParent() instanceof Mark) && !((s.getParent().getParent().getParent() instanceof Marker))) { // markdot/markline/label -> TG -> BG -> Mark/Marker try { closestObjectPos2 = intersection.getPointCoordinatesVW(); closestObject2 = s; } catch (NullPointerException ex) { } } else if ((s.getParent().getParent().getParent() instanceof Mark)) { try { closestMarkPos2 = intersection.getPointCoordinatesVW(); closestMark2 = s; } catch (NullPointerException ex) { } } } catch (NullPointerException ex) { } } } if (closestMark2 != null) { resultPos = closestMarkPos2; // priority of result -> mark resultNode = closestMark2; } else if (closestObject2 != null) { resultPos = closestObjectPos2; resultNode = closestObject2; } } } } } } } } myResult.setPoint(resultPos); myResult.setNode(resultNode); } return myResult; }