| 1 | /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software |
| 2 | * |
| 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | * of this software and associated documentation files (the "Software"), to deal |
| 5 | * in the Software without restriction, including without limitation the rights |
| 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | * copies of the Software, and to permit persons to whom the Software is |
| 8 | * furnished to do so, subject to the following conditions: |
| 9 | * |
| 10 | * The above copyright notice and this permission notice shall be included in |
| 11 | * all copies or substantial portions of the Software. |
| 12 | * |
| 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | * SOFTWARE. |
| 20 | */ |
| 21 | |
| 22 | @class ChipmunkPointQueryInfo; |
| 23 | @class ChipmunkSegmentQueryInfo; |
| 24 | |
| 25 | |
| 26 | /// Abstract base class for collsion shape types. |
| 27 | @interface ChipmunkShape : NSObject <ChipmunkBaseObject> { |
| 28 | @private |
| 29 | id _userData; |
| 30 | } |
| 31 | |
| 32 | /// Get the ChipmunkShape object associciated with a cpShape pointer. |
| 33 | /// Undefined if the cpShape wasn't created using Objective-Chipmunk. |
| 34 | +(ChipmunkShape *)shapeFromCPShape:(cpShape *)shape; |
| 35 | |
| 36 | /// Returns a pointer to the underlying cpShape C struct. |
| 37 | @property(nonatomic, readonly) cpShape *shape; |
| 38 | |
| 39 | /// The ChipmunkBody that this shape is attached to. |
| 40 | @property(nonatomic, retain) ChipmunkBody *body; |
| 41 | |
| 42 | // TODO doc |
| 43 | @property(nonatomic, assign) cpFloat mass; |
| 44 | @property(nonatomic, assign) cpFloat density; |
| 45 | @property(nonatomic, readonly) cpFloat moment; |
| 46 | @property(nonatomic, readonly) cpFloat area; |
| 47 | @property(nonatomic, readonly) cpVect centerOfGravity; |
| 48 | |
| 49 | /// The axis-aligned bounding box for this shape. |
| 50 | @property(nonatomic, readonly) cpBB bb; |
| 51 | |
| 52 | /// Sensor shapes send collision callback messages, but don't create a collision response. |
| 53 | @property(nonatomic, assign) BOOL sensor; |
| 54 | |
| 55 | /// How bouncy this shape is. |
| 56 | @property(nonatomic, assign) cpFloat elasticity; |
| 57 | |
| 58 | /// How much friction this shape has. |
| 59 | @property(nonatomic, assign) cpFloat friction; |
| 60 | |
| 61 | /** |
| 62 | The velocity of the shape's surface. |
| 63 | This velocity is used in the collision response when calculating the friction only. |
| 64 | */ |
| 65 | @property(nonatomic, assign) cpVect surfaceVelocity; |
| 66 | |
| 67 | /** |
| 68 | An object reference used as a collision type identifier. This is used when defining collision handlers. |
| 69 | @attention Like most @c delegate properties this is a weak reference and does not call @c retain. |
| 70 | */ |
| 71 | @property(nonatomic, assign) cpCollisionType collisionType; |
| 72 | |
| 73 | /** |
| 74 | The collision filtering parameters of this shape. |
| 75 | */ |
| 76 | @property(nonatomic, assign) cpShapeFilter filter; |
| 77 | |
| 78 | /// Get the space the body is added to. |
| 79 | @property(nonatomic, readonly) ChipmunkSpace *space; |
| 80 | |
| 81 | /** |
| 82 | An object that this shape is associated with. You can use this get a reference to your game object or controller object from within callbacks. |
| 83 | @attention Like most @c delegate properties this is a weak reference and does not call @c retain. This prevents reference cycles from occuring. |
| 84 | */ |
| 85 | @property(nonatomic, assign) id userData; |
| 86 | |
| 87 | /// Update and cache the axis-aligned bounding box for this shape. |
| 88 | - (cpBB)cacheBB; |
| 89 | |
| 90 | - (ChipmunkPointQueryInfo *)pointQuery:(cpVect)point; |
| 91 | - (ChipmunkSegmentQueryInfo *)segmentQueryFrom:(cpVect)start to:(cpVect)end radius:(cpFloat)radius; |
| 92 | |
| 93 | @end |
| 94 | |
| 95 | |
| 96 | @interface ChipmunkPointQueryInfo : NSObject { |
| 97 | @private |
| 98 | cpPointQueryInfo _info; |
| 99 | } |
| 100 | |
| 101 | - (id)initWithInfo:(cpPointQueryInfo *)info; |
| 102 | |
| 103 | /// Returns a pointer to the underlying cpNearestPointQueryInfo C struct. |
| 104 | @property(nonatomic, readonly) cpPointQueryInfo *info; |
| 105 | |
| 106 | /// The ChipmunkShape found. |
| 107 | @property(nonatomic, readonly) ChipmunkShape *shape; |
| 108 | |
| 109 | /// The closest point on the surface of the shape to the point. |
| 110 | @property(nonatomic, readonly) cpVect point; |
| 111 | |
| 112 | /// The distance between the point and the surface of the shape. |
| 113 | /// Negative distances mean that the point is that depth inside the shape. |
| 114 | @property(nonatomic, readonly) cpFloat distance; |
| 115 | |
| 116 | /// The gradient of the signed distance function. |
| 117 | /// The same as info.point/info.dist, but accurate even for very small values of info.dist. |
| 118 | @property(nonatomic, readonly) cpVect gradient; |
| 119 | |
| 120 | @end |
| 121 | |
| 122 | |
| 123 | /// Holds collision information from segment queries. You should never need to create one. |
| 124 | @interface ChipmunkSegmentQueryInfo : NSObject { |
| 125 | @private |
| 126 | cpSegmentQueryInfo _info; |
| 127 | cpVect _start, _end; |
| 128 | } |
| 129 | |
| 130 | - (id)initWithInfo:(cpSegmentQueryInfo *)info start:(cpVect)start end:(cpVect)end; |
| 131 | |
| 132 | /// Returns a pointer to the underlying cpSegmentQueryInfo C struct. |
| 133 | @property(nonatomic, readonly) cpSegmentQueryInfo *info; |
| 134 | |
| 135 | /// The ChipmunkShape found. |
| 136 | @property(nonatomic, readonly) ChipmunkShape *shape; |
| 137 | |
| 138 | /// The percentage between the start and end points where the collision occurred. |
| 139 | @property(nonatomic, readonly) cpFloat t; |
| 140 | |
| 141 | /// The normal of the collision with the shape. |
| 142 | @property(nonatomic, readonly) cpVect normal; |
| 143 | |
| 144 | /// The point of the collision in absolute (world) coordinates. |
| 145 | @property(nonatomic, readonly) cpVect point; |
| 146 | |
| 147 | /// The distance from the start point where the collision occurred. |
| 148 | @property(nonatomic, readonly) cpFloat dist; |
| 149 | |
| 150 | /// The start point. |
| 151 | @property(nonatomic, readonly) cpVect start; |
| 152 | |
| 153 | /// The end point. |
| 154 | @property(nonatomic, readonly) cpVect end; |
| 155 | |
| 156 | @end |
| 157 | |
| 158 | |
| 159 | /// Holds collision information from segment queries. You should never need to create one. |
| 160 | @interface ChipmunkShapeQueryInfo : NSObject { |
| 161 | @private |
| 162 | ChipmunkShape *_shape; |
| 163 | cpContactPointSet _contactPoints; |
| 164 | } |
| 165 | |
| 166 | - (id)initWithShape:(ChipmunkShape *)shape andPoints:(cpContactPointSet *)set; |
| 167 | |
| 168 | @property(nonatomic, readonly) ChipmunkShape *shape; |
| 169 | @property(nonatomic, readonly) cpContactPointSet *contactPoints; |
| 170 | |
| 171 | @end |
| 172 | |
| 173 | |
| 174 | /// A perfect circle shape. |
| 175 | @interface ChipmunkCircleShape : ChipmunkShape |
| 176 | |
| 177 | /// Create an autoreleased circle shape with the given radius and offset from the center of gravity. |
| 178 | + (id)circleWithBody:(ChipmunkBody *)body radius:(cpFloat)radius offset:(cpVect)offset; |
| 179 | |
| 180 | /// Initialize a circle shape with the given radius and offset from the center of gravity. |
| 181 | - (id)initWithBody:(ChipmunkBody *)body radius:(cpFloat)radius offset:(cpVect)offset; |
| 182 | |
| 183 | /// The radius of the circle. |
| 184 | @property(nonatomic, readonly) cpFloat radius; |
| 185 | |
| 186 | /// The offset from the center of gravity. |
| 187 | @property(nonatomic, readonly) cpVect offset; |
| 188 | |
| 189 | @end |
| 190 | |
| 191 | |
| 192 | /// A beveled (rounded) segment shape. |
| 193 | @interface ChipmunkSegmentShape : ChipmunkShape |
| 194 | |
| 195 | /// Create an autoreleased segment shape with the given endpoints and radius. |
| 196 | + (id)segmentWithBody:(ChipmunkBody *)body from:(cpVect)a to:(cpVect)b radius:(cpFloat)radius; |
| 197 | |
| 198 | /// Initialize a segment shape with the given endpoints and radius. |
| 199 | - (id)initWithBody:(ChipmunkBody *)body from:(cpVect)a to:(cpVect)b radius:(cpFloat)radius; |
| 200 | |
| 201 | /// Let Chipmunk know about the geometry of adjacent segments to avoid colliding with endcaps. |
| 202 | - (void)setPrevNeighbor:(cpVect)prev nextNeighbor:(cpVect)next; |
| 203 | |
| 204 | /// The start of the segment shape. |
| 205 | @property(nonatomic, readonly) cpVect a; |
| 206 | |
| 207 | /// The end of the segment shape. |
| 208 | @property(nonatomic, readonly) cpVect b; |
| 209 | |
| 210 | /// The normal of the segment shape. |
| 211 | @property(nonatomic, readonly) cpVect normal; |
| 212 | |
| 213 | /// The beveling radius of the segment shape. |
| 214 | @property(nonatomic, readonly) cpFloat radius; |
| 215 | |
| 216 | @end |
| 217 | |
| 218 | |
| 219 | /// A convex polygon shape. |
| 220 | @interface ChipmunkPolyShape : ChipmunkShape |
| 221 | |
| 222 | /// Create an autoreleased polygon shape from the given vertexes after applying the transform and with the given rounding radius. |
| 223 | + (id)polyWithBody:(ChipmunkBody *)body count:(int)count verts:(const cpVect *)verts transform:(cpTransform)transform radius:(cpFloat)radius; |
| 224 | |
| 225 | /// Create an autoreleased box shape centered on the center of gravity. |
| 226 | + (id)boxWithBody:(ChipmunkBody *)body width:(cpFloat)width height:(cpFloat)height radius:(cpFloat)radius; |
| 227 | |
| 228 | /// Create an autoreleased box shape with the given bounding box in body local coordinates and rounding radius. |
| 229 | + (id)boxWithBody:(ChipmunkBody *)body bb:(cpBB)bb radius:(cpFloat)radius; |
| 230 | |
| 231 | /// Initialize a polygon shape from the given vertexes after applying the transform and with the given rounding radius. |
| 232 | - (id)initWithBody:(ChipmunkBody *)body count:(int)count verts:(const cpVect *)verts transform:(cpTransform)transform radius:(cpFloat)radius; |
| 233 | |
| 234 | /// Initialize a box shape centered on the center of gravity. |
| 235 | - (id)initBoxWithBody:(ChipmunkBody *)body width:(cpFloat)width height:(cpFloat)height radius:(cpFloat)radius; |
| 236 | |
| 237 | /// Initialize a box shape with the given bounding box in body local coordinates and rounding radius. |
| 238 | - (id)initBoxWithBody:(ChipmunkBody *)body bb:(cpBB)bb radius:(cpFloat)radius; |
| 239 | |
| 240 | /// The number of vertexes in this polygon. |
| 241 | @property(nonatomic, readonly) int count; |
| 242 | |
| 243 | /// Get the rounding radius of the polygon. |
| 244 | @property(nonatomic, readonly) cpFloat radius; |
| 245 | |
| 246 | /// Access the vertexes of this polygon. |
| 247 | - (cpVect)getVertex:(int)index; |
| 248 | |
| 249 | @end |