1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
22 @class ChipmunkPointQueryInfo;
23 @class ChipmunkSegmentQueryInfo;
26 /// Abstract base class for collsion shape types.
27 @interface ChipmunkShape : NSObject <ChipmunkBaseObject> {
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;
36 /// Returns a pointer to the underlying cpShape C struct.
37 @property(nonatomic, readonly) cpShape *shape;
39 /// The ChipmunkBody that this shape is attached to.
40 @property(nonatomic, retain) ChipmunkBody *body;
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;
49 /// The axis-aligned bounding box for this shape.
50 @property(nonatomic, readonly) cpBB bb;
52 /// Sensor shapes send collision callback messages, but don't create a collision response.
53 @property(nonatomic, assign) BOOL sensor;
55 /// How bouncy this shape is.
56 @property(nonatomic, assign) cpFloat elasticity;
58 /// How much friction this shape has.
59 @property(nonatomic, assign) cpFloat friction;
62 The velocity of the shape's surface.
63 This velocity is used in the collision response when calculating the friction only.
65 @property(nonatomic, assign) cpVect surfaceVelocity;
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.
71 @property(nonatomic, assign) cpCollisionType collisionType;
74 The collision filtering parameters of this shape.
76 @property(nonatomic, assign) cpShapeFilter filter;
78 /// Get the space the body is added to.
79 @property(nonatomic, readonly) ChipmunkSpace *space;
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.
85 @property(nonatomic, assign) id userData;
87 /// Update and cache the axis-aligned bounding box for this shape.
90 - (ChipmunkPointQueryInfo *)pointQuery:(cpVect)point;
91 - (ChipmunkSegmentQueryInfo *)segmentQueryFrom:(cpVect)start to:(cpVect)end radius:(cpFloat)radius;
96 @interface ChipmunkPointQueryInfo : NSObject {
98 cpPointQueryInfo _info;
101 - (id)initWithInfo:(cpPointQueryInfo *)info;
103 /// Returns a pointer to the underlying cpNearestPointQueryInfo C struct.
104 @property(nonatomic, readonly) cpPointQueryInfo *info;
106 /// The ChipmunkShape found.
107 @property(nonatomic, readonly) ChipmunkShape *shape;
109 /// The closest point on the surface of the shape to the point.
110 @property(nonatomic, readonly) cpVect point;
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;
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;
123 /// Holds collision information from segment queries. You should never need to create one.
124 @interface ChipmunkSegmentQueryInfo : NSObject {
126 cpSegmentQueryInfo _info;
130 - (id)initWithInfo:(cpSegmentQueryInfo *)info start:(cpVect)start end:(cpVect)end;
132 /// Returns a pointer to the underlying cpSegmentQueryInfo C struct.
133 @property(nonatomic, readonly) cpSegmentQueryInfo *info;
135 /// The ChipmunkShape found.
136 @property(nonatomic, readonly) ChipmunkShape *shape;
138 /// The percentage between the start and end points where the collision occurred.
139 @property(nonatomic, readonly) cpFloat t;
141 /// The normal of the collision with the shape.
142 @property(nonatomic, readonly) cpVect normal;
144 /// The point of the collision in absolute (world) coordinates.
145 @property(nonatomic, readonly) cpVect point;
147 /// The distance from the start point where the collision occurred.
148 @property(nonatomic, readonly) cpFloat dist;
151 @property(nonatomic, readonly) cpVect start;
154 @property(nonatomic, readonly) cpVect end;
159 /// Holds collision information from segment queries. You should never need to create one.
160 @interface ChipmunkShapeQueryInfo : NSObject {
162 ChipmunkShape *_shape;
163 cpContactPointSet _contactPoints;
166 - (id)initWithShape:(ChipmunkShape *)shape andPoints:(cpContactPointSet *)set;
168 @property(nonatomic, readonly) ChipmunkShape *shape;
169 @property(nonatomic, readonly) cpContactPointSet *contactPoints;
174 /// A perfect circle shape.
175 @interface ChipmunkCircleShape : ChipmunkShape
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;
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;
183 /// The radius of the circle.
184 @property(nonatomic, readonly) cpFloat radius;
186 /// The offset from the center of gravity.
187 @property(nonatomic, readonly) cpVect offset;
192 /// A beveled (rounded) segment shape.
193 @interface ChipmunkSegmentShape : ChipmunkShape
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;
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;
201 /// Let Chipmunk know about the geometry of adjacent segments to avoid colliding with endcaps.
202 - (void)setPrevNeighbor:(cpVect)prev nextNeighbor:(cpVect)next;
204 /// The start of the segment shape.
205 @property(nonatomic, readonly) cpVect a;
207 /// The end of the segment shape.
208 @property(nonatomic, readonly) cpVect b;
210 /// The normal of the segment shape.
211 @property(nonatomic, readonly) cpVect normal;
213 /// The beveling radius of the segment shape.
214 @property(nonatomic, readonly) cpFloat radius;
219 /// A convex polygon shape.
220 @interface ChipmunkPolyShape : ChipmunkShape
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;
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;
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;
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;
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;
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;
240 /// The number of vertexes in this polygon.
241 @property(nonatomic, readonly) int count;
243 /// Get the rounding radius of the polygon.
244 @property(nonatomic, readonly) cpFloat radius;
246 /// Access the vertexes of this polygon.
247 - (cpVect)getVertex:(int)index;