1 /******************************************************************************* 2 * Copyright (c) 2013, Daniel Murphy 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * * Redistributions of source code must retain the above copyright notice, 8 * this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright notice, 10 * this list of conditions and the following disclaimer in the documentation 11 * and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 17 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 19 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 20 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 21 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 22 * POSSIBILITY OF SUCH DAMAGE. 23 ******************************************************************************/ 24 package org.jbox2d.collision.shapes; 25 26 27 import org.jbox2d.collision.AABB; 28 import org.jbox2d.collision.RayCastInput; 29 import org.jbox2d.collision.RayCastOutput; 30 import org.jbox2d.common.MathUtils; 31 import org.jbox2d.common.Rot; 32 import org.jbox2d.common.Settings; 33 import org.jbox2d.common.Transform; 34 import org.jbox2d.common.Vec2; 35 36 /** 37 * A chain shape is a free form sequence of line segments. The chain has two-sided collision, so you 38 * can use inside and outside collision. Therefore, you may use any winding order. Connectivity 39 * information is used to create smooth collisions. WARNING: The chain will not collide properly if 40 * there are self-intersections. 41 * 42 * @author Daniel 43 */ 44 public class ChainShape extends Shape { 45 46 public Vec2[] m_vertices; 47 public int m_count; 48 public final Vec2 m_prevVertex = new Vec2(), m_nextVertex = new Vec2(); 49 public boolean m_hasPrevVertex = false, m_hasNextVertex = false; 50 51 private final EdgeShape pool0 = new EdgeShape(); 52 53 public ChainShape() { 54 super(ShapeType.CHAIN); 55 m_vertices = null; 56 m_radius = Settings.polygonRadius; 57 m_count = 0; 58 } 59 60 @Override 61 public int getChildCount() { 62 return m_count - 1; 63 } 64 65 /** 66 * Get a child edge. 67 */ 68 public void getChildEdge(EdgeShape edge, int index) { 69 assert (0 <= index && index < m_count - 1); 70 edge.m_radius = m_radius; 71 72 final Vec2 v0 = m_vertices[index + 0]; 73 final Vec2 v1 = m_vertices[index + 1]; 74 edge.m_vertex1.x = v0.x; 75 edge.m_vertex1.y = v0.y; 76 edge.m_vertex2.x = v1.x; 77 edge.m_vertex2.y = v1.y; 78 79 if (index > 0) { 80 Vec2 v = m_vertices[index - 1]; 81 edge.m_vertex0.x = v.x; 82 edge.m_vertex0.y = v.y; 83 edge.m_hasVertex0 = true; 84 } else { 85 edge.m_vertex0.x = m_prevVertex.x; 86 edge.m_vertex0.y = m_prevVertex.y; 87 edge.m_hasVertex0 = m_hasPrevVertex; 88 } 89 90 if (index < m_count - 2) { 91 Vec2 v = m_vertices[index + 2]; 92 edge.m_vertex3.x = v.x; 93 edge.m_vertex3.y = v.y; 94 edge.m_hasVertex3 = true; 95 } else { 96 edge.m_vertex3.x = m_nextVertex.x; 97 edge.m_vertex3.y = m_nextVertex.y; 98 edge.m_hasVertex3 = m_hasNextVertex; 99 } 100 } 101 102 @Override 103 public float computeDistanceToOut(Transform xf, Vec2 p, int childIndex, Vec2 normalOut) { 104 final EdgeShape edge = pool0; 105 getChildEdge(edge, childIndex); 106 return edge.computeDistanceToOut(xf, p, 0, normalOut); 107 } 108 109 @Override 110 public boolean testPoint(Transform xf, Vec2 p) { 111 return false; 112 } 113 114 @Override 115 public boolean raycast(RayCastOutput output, RayCastInput input, Transform xf, int childIndex) { 116 assert (childIndex < m_count); 117 118 final EdgeShape edgeShape = pool0; 119 120 int i1 = childIndex; 121 int i2 = childIndex + 1; 122 if (i2 == m_count) { 123 i2 = 0; 124 } 125 Vec2 v = m_vertices[i1]; 126 edgeShape.m_vertex1.x = v.x; 127 edgeShape.m_vertex1.y = v.y; 128 Vec2 v1 = m_vertices[i2]; 129 edgeShape.m_vertex2.x = v1.x; 130 edgeShape.m_vertex2.y = v1.y; 131 132 return edgeShape.raycast(output, input, xf, 0); 133 } 134 135 @Override 136 public void computeAABB(AABB aabb, Transform xf, int childIndex) { 137 assert (childIndex < m_count); 138 final Vec2 lower = aabb.lowerBound; 139 final Vec2 upper = aabb.upperBound; 140 141 int i1 = childIndex; 142 int i2 = childIndex + 1; 143 if (i2 == m_count) { 144 i2 = 0; 145 } 146 147 final Vec2 vi1 = m_vertices[i1]; 148 final Vec2 vi2 = m_vertices[i2]; 149 final Rot xfq = xf.q; 150 final Vec2 xfp = xf.p; 151 float v1x = (xfq.c * vi1.x - xfq.s * vi1.y) + xfp.x; 152 float v1y = (xfq.s * vi1.x + xfq.c * vi1.y) + xfp.y; 153 float v2x = (xfq.c * vi2.x - xfq.s * vi2.y) + xfp.x; 154 float v2y = (xfq.s * vi2.x + xfq.c * vi2.y) + xfp.y; 155 156 lower.x = v1x < v2x ? v1x : v2x; 157 lower.y = v1y < v2y ? v1y : v2y; 158 upper.x = v1x > v2x ? v1x : v2x; 159 upper.y = v1y > v2y ? v1y : v2y; 160 } 161 162 @Override 163 public void computeMass(MassData massData, float density) { 164 massData.mass = 0.0f; 165 massData.center.setZero(); 166 massData.I = 0.0f; 167 } 168 169 @Override 170 public Shape clone() { 171 ChainShape clone = new ChainShape(); 172 clone.createChain(m_vertices, m_count); 173 clone.m_prevVertex.set(m_prevVertex); 174 clone.m_nextVertex.set(m_nextVertex); 175 clone.m_hasPrevVertex = m_hasPrevVertex; 176 clone.m_hasNextVertex = m_hasNextVertex; 177 return clone; 178 } 179 180 /** 181 * Create a loop. This automatically adjusts connectivity. 182 * 183 * @param vertices an array of vertices, these are copied 184 * @param count the vertex count 185 */ 186 public void createLoop(final Vec2[] vertices, int count) { 187 assert (m_vertices == null && m_count == 0); 188 assert (count >= 3); 189 m_count = count + 1; 190 m_vertices = new Vec2[m_count]; 191 for (int i = 1; i < count; i++) { 192 Vec2 v1 = vertices[i - 1]; 193 Vec2 v2 = vertices[i]; 194 // If the code crashes here, it means your vertices are too close together. 195 if (MathUtils.distanceSquared(v1, v2) < Settings.linearSlop * Settings.linearSlop) { 196 throw new RuntimeException("Vertices of chain shape are too close together"); 197 } 198 } 199 for (int i = 0; i < count; i++) { 200 m_vertices[i] = new Vec2(vertices[i]); 201 } 202 m_vertices[count] = new Vec2(m_vertices[0]); 203 m_prevVertex.set(m_vertices[m_count - 2]); 204 m_nextVertex.set(m_vertices[1]); 205 m_hasPrevVertex = true; 206 m_hasNextVertex = true; 207 } 208 209 /** 210 * Create a chain with isolated end vertices. 211 * 212 * @param vertices an array of vertices, these are copied 213 * @param count the vertex count 214 */ 215 public void createChain(final Vec2 vertices[], int count) { 216 assert (m_vertices == null && m_count == 0); 217 assert (count >= 2); 218 m_count = count; 219 m_vertices = new Vec2[m_count]; 220 for (int i = 1; i < m_count; i++) { 221 Vec2 v1 = vertices[i - 1]; 222 Vec2 v2 = vertices[i]; 223 // If the code crashes here, it means your vertices are too close together. 224 if (MathUtils.distanceSquared(v1, v2) < Settings.linearSlop * Settings.linearSlop) { 225 throw new RuntimeException("Vertices of chain shape are too close together"); 226 } 227 } 228 for (int i = 0; i < m_count; i++) { 229 m_vertices[i] = new Vec2(vertices[i]); 230 } 231 m_hasPrevVertex = false; 232 m_hasNextVertex = false; 233 234 m_prevVertex.setZero(); 235 m_nextVertex.setZero(); 236 } 237 238 /** 239 * Establish connectivity to a vertex that precedes the first vertex. Don't call this for loops. 240 * 241 * @param prevVertex 242 */ 243 public void setPrevVertex(final Vec2 prevVertex) { 244 m_prevVertex.set(prevVertex); 245 m_hasPrevVertex = true; 246 } 247 248 /** 249 * Establish connectivity to a vertex that follows the last vertex. Don't call this for loops. 250 * 251 * @param nextVertex 252 */ 253 public void setNextVertex(final Vec2 nextVertex) { 254 m_nextVertex.set(nextVertex); 255 m_hasNextVertex = true; 256 } 257 } 258