|
| 1 | +package org.andresoviedo.app.model3D.collision; |
| 2 | + |
| 3 | +import android.opengl.GLU; |
| 4 | +import android.util.Log; |
| 5 | + |
| 6 | +import org.andresoviedo.app.model3D.entities.BoundingBox; |
| 7 | +import org.andresoviedo.app.model3D.model.Object3DData; |
| 8 | +import org.andresoviedo.app.model3D.view.ModelRenderer; |
| 9 | +import org.andresoviedo.app.util.math.Math3DUtils; |
| 10 | + |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +/** |
| 14 | + * Class that encapsulates all the logic for the collision detection algorithm. |
| 15 | + * |
| 16 | + * @author andresoviedo |
| 17 | + */ |
| 18 | +public class CollisionDetection { |
| 19 | + |
| 20 | + /** |
| 21 | + * Get the nearest object intersected by the specified window coordinates |
| 22 | + * |
| 23 | + * @param objects the list of objects to test |
| 24 | + * @param mRenderer the model renderer to unproject window coordinates to world coordinates |
| 25 | + * @param windowX the window x coordinate |
| 26 | + * @param windowY the window y coordinate |
| 27 | + * @return the nearest object intersected by the specified coordinates or null |
| 28 | + */ |
| 29 | + public static Object3DData getIntersection(List<Object3DData> objects, ModelRenderer mRenderer, float windowX, float windowY) { |
| 30 | + float[] nearHit = unproject(mRenderer, windowX, windowY, 0); |
| 31 | + float[] farHit = unproject(mRenderer, windowX, windowY, 1); |
| 32 | + return getIntersection(objects, nearHit, farHit); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Get the nearest object intersected by the specified ray or null if no object is intersected |
| 37 | + * |
| 38 | + * @param objects the list of objects to test |
| 39 | + * @param p1 the ray start point |
| 40 | + * @param p2 the ray end point |
| 41 | + * @return the object intersected by the specified ray |
| 42 | + */ |
| 43 | + public static Object3DData getIntersection(List<Object3DData> objects, float[] p1, float[] p2) { |
| 44 | + float[] direction = Math3DUtils.substract(p2, p1); |
| 45 | + Math3DUtils.normalize(direction); |
| 46 | + float min = Float.MAX_VALUE; |
| 47 | + Object3DData ret = null; |
| 48 | + for (Object3DData obj : objects) { |
| 49 | + if (obj.getId().startsWith("Line")) continue; |
| 50 | + BoundingBox box = obj.getBoundingBox(); |
| 51 | + float[] intersection = getBoxIntersection(p1, direction, box); |
| 52 | + if (intersection[0] > 0 && intersection[0] < intersection[1] && intersection[0] < min) { |
| 53 | + min = intersection[0]; |
| 54 | + ret = obj; |
| 55 | + } |
| 56 | + } |
| 57 | + if (ret != null) { |
| 58 | + Log.i("CollisionDetection", "Collision detected '" + ret.getId() + "' distance: " + min); |
| 59 | + } |
| 60 | + return ret; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get the entry and exit point of the ray intersecting the nearest object or null if no object is intersected |
| 65 | + * |
| 66 | + * @param objects list of objects to test |
| 67 | + * @param p1 ray start point |
| 68 | + * @param p2 ray end point |
| 69 | + * @return the entry and exit point of the ray intersecting the nearest object |
| 70 | + */ |
| 71 | + public static float[] getIntersectionPoint(List<Object3DData> objects, float[] p1, float[] p2) { |
| 72 | + float[] direction = Math3DUtils.substract(p2, p1); |
| 73 | + Math3DUtils.normalize(direction); |
| 74 | + float min = Float.MAX_VALUE; |
| 75 | + float[] intersection2 = null; |
| 76 | + Object3DData ret = null; |
| 77 | + for (Object3DData obj : objects) { |
| 78 | + if (obj.getId().startsWith("Line")) continue; |
| 79 | + BoundingBox box = obj.getBoundingBox(); |
| 80 | + float[] intersection = getBoxIntersection(p1, direction, box); |
| 81 | + if (intersection[0] > 0 && intersection[0] < intersection[1] && intersection[0] < min) { |
| 82 | + min = intersection[0]; |
| 83 | + ret = obj; |
| 84 | + intersection2 = intersection; |
| 85 | + } |
| 86 | + } |
| 87 | + if (ret != null) { |
| 88 | + Log.i("CollisionDetection", "Collision detected '" + ret.getId() + "' distance: " + min); |
| 89 | + return new float[]{p1[0] + direction[0] * min, p1[1] + direction[1] * min, p1[2] + direction[2] * min}; |
| 90 | + } |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Return true if the specified ray intersects the bounding box |
| 96 | + * |
| 97 | + * @param origin origin of the ray |
| 98 | + * @param dir direction of the ray |
| 99 | + * @param b bounding box |
| 100 | + * @return true if the specified ray intersects the bounding box, false otherwise |
| 101 | + */ |
| 102 | + public static boolean isBoxIntersection(float[] origin, float[] dir, BoundingBox b) { |
| 103 | + float[] intersection = getBoxIntersection(origin, dir, b); |
| 104 | + return intersection[0] > 0 && intersection[0] < intersection[1]; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Get the intersection points of the near and far plane for the specified ray and bounding box |
| 109 | + * |
| 110 | + * @param origin the ray origin |
| 111 | + * @param dir the ray direction |
| 112 | + * @param b the bounding box |
| 113 | + * @return the intersection points of the near and far plane |
| 114 | + */ |
| 115 | + public static float[] getBoxIntersection(float[] origin, float[] dir, BoundingBox b) { |
| 116 | + float[] tMin = Math3DUtils.divide(Math3DUtils.substract(b.getCurrentMin(), origin), dir); |
| 117 | + float[] tMax = Math3DUtils.divide(Math3DUtils.substract(b.getCurrentMax(), origin), dir); |
| 118 | + float[] t1 = Math3DUtils.min(tMin, tMax); |
| 119 | + float[] t2 = Math3DUtils.max(tMin, tMax); |
| 120 | + float tNear = Math.max(Math.max(t1[0], t1[1]), t1[2]); |
| 121 | + float tFar = Math.min(Math.min(t2[0], t2[1]), t2[2]); |
| 122 | + return new float[]{tNear, tFar}; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Get the corresponding near and far vertex for the specified window coordinates |
| 127 | + * |
| 128 | + * @param mRenderer |
| 129 | + * @param rx |
| 130 | + * @param ry |
| 131 | + * @param rz |
| 132 | + * @return the corresponding near and far vertex for the specified window coordinates |
| 133 | + */ |
| 134 | + public static float[] unproject(ModelRenderer mRenderer, float rx, float ry, float rz) { |
| 135 | + float[] xyzw = {0, 0, 0, 0}; |
| 136 | + ry = (float) mRenderer.getHeight() - ry; |
| 137 | + int[] viewport = {0, 0, mRenderer.getWidth(), mRenderer.getHeight()}; |
| 138 | + GLU.gluUnProject(rx, ry, rz, mRenderer.getModelViewMatrix(), 0, mRenderer.getModelProjectionMatrix(), 0, |
| 139 | + viewport, 0, xyzw, 0); |
| 140 | + xyzw[0] /= xyzw[3]; |
| 141 | + xyzw[1] /= xyzw[3]; |
| 142 | + xyzw[2] /= xyzw[3]; |
| 143 | + xyzw[3] = 1; |
| 144 | + return xyzw; |
| 145 | + } |
| 146 | +} |
0 commit comments