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
23 Constraints connect two ChipmunkBody objects together. Most often constraints are simple joints, but can also be things like motors, friction generators or servos.
26 <object width="425" height="344">
27 <param name="movie" value="http://www.youtube.com/v/ZgJJZTS0aMM?fs=1&hl=en_US&rel=0"></param>
28 <param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
29 <embed src="http://www.youtube.com/v/ZgJJZTS0aMM?fs=1&hl=en_US&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed>
33 @interface ChipmunkConstraint : NSObject <ChipmunkBaseObject> {
38 /// Returns a pointer to the underlying cpConstraint C struct.
39 @property(nonatomic, readonly) cpConstraint *constraint;
41 /// The first ChipmunkBody the constraint controls.
42 @property(nonatomic, readonly) ChipmunkBody *bodyA;
44 /// The second ChipmunkBody the constraint controls.
45 @property(nonatomic, readonly) ChipmunkBody *bodyB;
47 /// Get the ChipmunkConstraint object associciated with a cpConstraint pointer.
48 /// Undefined if the cpConstraint wasn't created using Objective-Chipmunk.
49 +(ChipmunkConstraint *)constraintFromCPConstraint:(cpConstraint *)constraint;
52 Maximum force this constraint is allowed to use (defalts to infinity).
53 This allows joints to be pulled apart if too much force is applied to them.
54 It also allows you to use constraints as force or friction generators for controlling bodies.
56 @property(nonatomic, assign) cpFloat maxForce;
59 The rate at which joint error is corrected.
60 Defaults to pow(1.0 - 0.1, 60.0) meaning that it will correct 10% of the error every 1/60th of a second.
62 @property(nonatomic, assign) cpFloat errorBias;
65 Maximum rate (speed) that a joint can be corrected at (defaults to infinity).
66 Setting this value to a finite value allows you to control a joint like a servo motor.
68 @property(nonatomic, assign) cpFloat maxBias;
71 Whether or not the connected bodies should checked for collisions.
72 Collisions are filtered before calling callbacks.
75 @property(nonatomic, assign) BOOL collideBodies;
77 /// Get the most recent impulse applied by this constraint.
78 @property(nonatomic, readonly) cpFloat impulse;
80 /// Get the space the body is added to.
81 @property(nonatomic, readonly) ChipmunkSpace *space;
84 An object that this constraint is associated with. You can use this get a reference to your game object or controller object from within callbacks.
85 @attention Like most @c delegate properties this is a weak reference and does not call @c retain. This prevents reference cycles from occuring.
87 @property(nonatomic, assign) id userData;
89 /// Override this method to update a constraints parameters just before running the physics each step.
90 -(void)preSolve:(ChipmunkSpace *)space;
92 /// Override this method to poll values from a constraint each frame after the physics runs.
93 /// This can be used to implement breakable joints for instance.
94 -(void)postSolve:(ChipmunkSpace *)space;
100 Pin joints hold a set distance between points on two bodies.
101 Think of them as connecting a solid pin or rod between the two anchor points.
103 @interface ChipmunkPinJoint : ChipmunkConstraint
106 Create an autoreleased pin joint between the two bodies with the given anchor points.
107 The distance is calculated when the joint is initialized. It can be set explicitly using the property.
109 + (ChipmunkPinJoint *)pinJointWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b anchorA:(cpVect)anchorA anchorB:(cpVect)anchorB;
112 Initialize a pin joint between the two bodies with the given anchor points.
113 The distance is calculated when the joint is initialized. It can be set explicitly using the property.
115 - (id)initWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b anchorA:(cpVect)anchorA anchorB:(cpVect)anchorB;
117 /// The anchor point on the first body.
118 @property(nonatomic, assign) cpVect anchorA;
120 /// The anchor point on the second body.
121 @property(nonatomic, assign) cpVect anchorB;
123 /// The distance between the two anchor points that the joint keeps.
124 @property(nonatomic, assign) cpFloat dist;
130 Slide joints hold the distance between points on two bodies between a minimum and a maximum.
131 Think of them as a telescoping ChipmunkPinJoint.
133 @interface ChipmunkSlideJoint : ChipmunkConstraint
136 Create an autoreleased slide joint between the two bodies with the given anchor points and distance range.
138 + (ChipmunkSlideJoint *)slideJointWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b anchorA:(cpVect)anchorA anchorB:(cpVect)anchorB min:(cpFloat)min max:(cpFloat)max;
141 Initialize a slide joint between the two bodies with the given anchor points and distance range.
143 - (id)initWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b anchorA:(cpVect)anchorA anchorB:(cpVect)anchorB min:(cpFloat)min max:(cpFloat)max;
145 /// The anchor point on the first body.
146 @property(nonatomic, assign) cpVect anchorA;
148 /// The anchor point on the second body.
149 @property(nonatomic, assign) cpVect anchorB;
151 /// The minimum allowed distance between anchor points.
152 @property(nonatomic, assign) cpFloat min;
154 /// The maximum allowed distance between anchor points.
155 @property(nonatomic, assign) cpFloat max;
161 Pivot joints hold two points on two bodies together allowing them to rotate freely around the pivot.
163 @interface ChipmunkPivotJoint : ChipmunkConstraint
166 Create an autoreleased pivot joint between the two bodies with the two anchor points.
167 Make sure you have the bodies in the right place as the joint will fix itself as soon as you start simulating the space.
169 + (ChipmunkPivotJoint *)pivotJointWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b anchorA:(cpVect)anchorA anchorB:(cpVect)anchorB;
172 Create an autoreleased pivot joint between the two bodies by calculating the anchor points from the pivot point given in absolute coordinates.
174 + (ChipmunkPivotJoint *)pivotJointWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b pivot:(cpVect)pivot;
177 Initialize a pivot joint between the two bodies with the two anchor points.
178 Make sure you have the bodies in the right place as the joint will fix itself as soon as you start simulating the space.
180 - (id)initWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b anchorA:(cpVect)anchorA anchorB:(cpVect)anchorB;
183 Initialize a pivot joint between the two bodies by calculating the anchor points from the pivot point given in absolute coordinates.
185 - (id)initWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b pivot:(cpVect)pivot;
187 /// The anchor point on the first body.
188 @property(nonatomic, assign) cpVect anchorA;
190 /// The anchor point on the second body.
191 @property(nonatomic, assign) cpVect anchorB;
197 Groove joints hold a pivot point on one body to line along a line segment on another like a pin in a groove.
199 @interface ChipmunkGrooveJoint : ChipmunkConstraint
202 Create an autoreleased groove joint between the two bodies.
203 Make sure you have the bodies in the right place as the joint will snap into shape as soon as you start simulating the space.
204 @param grooveA The start of the line segment on the first body.
205 @param grooveB The end of the line segment on the first body.
206 @param anchorB The anchor point on the second body that is held to the line segment on the first.
208 + (ChipmunkGrooveJoint *)grooveJointWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b grooveA:(cpVect)grooveA grooveB:(cpVect)grooveB anchorB:(cpVect)anchorB;
211 Initialize a groove joint between the two bodies.
212 Make sure you have the bodies in the right place as the joint will snap into shape as soon as you start simulating the space.
213 @param grooveA The start of the line segment on the first body.
214 @param grooveB The end of the line segment on the first body.
215 @param anchorB The anchor point on the second body that is held to the line segment on the first.
217 - (id)initWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b grooveA:(cpVect)grooveA grooveB:(cpVect)grooveB anchorB:(cpVect)anchorB;
219 /// The start point of the groove on the first body.
220 @property(nonatomic, assign) cpVect grooveA;
221 /// The end point of the groove on the first body.
222 @property(nonatomic, assign) cpVect grooveB;
224 /// The anchor point on the second body.
225 @property(nonatomic, assign) cpVect anchorB;
231 A spring with a damper.
232 While a spring is not technically a constraint, the damper is. The spring forces are simply a convenience.
234 @interface ChipmunkDampedSpring : ChipmunkConstraint
237 Create an autoreleased damped spring between two bodies at the given anchor points.
238 @param restLength The length the spring wants to contract or expand to.
239 @param stiffness The <a href="http://en.wikipedia.org/wiki/Young's_modulus">young's modulus</a> of the spring.
240 @param damping The amount of viscous damping to apply.
242 + (ChipmunkDampedSpring *)dampedSpringWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b anchorA:(cpVect)anchorA anchorB:(cpVect)anchorB restLength:(cpFloat)restLength stiffness:(cpFloat)stiffness damping:(cpFloat)damping;
245 Initialize a damped spring between two bodies at the given anchor points.
246 @param restLength The length the spring wants to contract or expand to.
247 @param stiffness The <a href="http://en.wikipedia.org/wiki/Young's_modulus">young's modulus</a> of the spring.
248 @param damping The amount of viscous damping to apply.
250 - (id)initWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b anchorA:(cpVect)anchorA anchorB:(cpVect)anchorB restLength:(cpFloat)restLength stiffness:(cpFloat)stiffness damping:(cpFloat)damping;
252 /// The anchor point on the first body.
253 @property(nonatomic, assign) cpVect anchorA;
255 /// The anchor point on the second body.
256 @property(nonatomic, assign) cpVect anchorB;
258 /// The length the spring wants to contract or expand to.
259 @property(nonatomic, assign) cpFloat restLength;
261 /// The <a href="http://en.wikipedia.org/wiki/Young's_modulus">young's modulus</a> of the spring.
262 @property(nonatomic, assign) cpFloat stiffness;
264 /// The amount of viscous damping to apply.
265 @property(nonatomic, assign) cpFloat damping;
271 Like a ChipmunkDampedSpring, but operates in a rotational fashion.
273 @interface ChipmunkDampedRotarySpring : ChipmunkConstraint
277 Create an autoreleased damped rotary spring between the given bodies.
278 @param restAngle The angular offset in radians the spring attempts to keep between the two bodies.
279 @param stiffness The <a href="http://en.wikipedia.org/wiki/Young's_modulus">young's modulus</a> of the spring.
280 @param damping The amount of viscous damping to apply.
282 + (ChipmunkDampedRotarySpring *)dampedRotarySpringWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b restAngle:(cpFloat)restAngle stiffness:(cpFloat)stiffness damping:(cpFloat)damping;
285 Initialize a damped rotary spring between the given bodies.
286 @param restAngle The angular offset in radians the spring attempts to keep between the two bodies.
287 @param stiffness The <a href="http://en.wikipedia.org/wiki/Young's_modulus">young's modulus</a> of the spring.
288 @param damping The amount of viscous damping to apply.
290 - (id)initWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b restAngle:(cpFloat)restAngle stiffness:(cpFloat)stiffness damping:(cpFloat)damping;
292 /// The angular offset the spring attempts to keep between the two bodies.
293 @property(nonatomic, assign) cpFloat restAngle;
295 /// The <a href="http://en.wikipedia.org/wiki/Young's_modulus">young's modulus</a> of the spring.
296 @property(nonatomic, assign) cpFloat stiffness;
298 /// The amount of viscous damping to apply.
299 @property(nonatomic, assign) cpFloat damping;
305 Constrains the angle between two bodies.
306 This joint is often used in conjuction with a separate ChipmunkPivotJoint in order to limit the rotation around the pivot.
308 @interface ChipmunkRotaryLimitJoint : ChipmunkConstraint
311 Create an autoreleased rotary limit joint between the two bodies and angular range in radians.
312 Make sure you have the bodies in the right place as the joint will snap into shape as soon as you start simulating the space.
314 + (ChipmunkRotaryLimitJoint *)rotaryLimitJointWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b min:(cpFloat)min max:(cpFloat)max;
317 Create an autoreleased rotary limit joint between the two bodies and angular range in radians.
318 Make sure you have the bodies in the right place as the joint will snap into shape as soon as you start simulating the space.
320 - (id)initWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b min:(cpFloat)min max:(cpFloat)max;
322 /// The minimum angular delta of the joint in radians.
323 @property(nonatomic, assign) cpFloat min;
325 /// The maximum angular delta of the joint in radians.
326 @property(nonatomic, assign) cpFloat max;
332 Simple motors make two objects spin relative to each other.
333 They are most often used with the ChipmunkConstraint.maxForce property set to a finite value.
335 @interface ChipmunkSimpleMotor : ChipmunkConstraint
337 /// Create an autoreleased simple motor between the given bodies and relative rotation rate in radians per second.
338 + (ChipmunkSimpleMotor *)simpleMotorWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b rate:(cpFloat)rate;
340 /// Initialize a simple motor between the given bodies and relative rotation rate in radians per second.
341 - (id)initWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b rate:(cpFloat)rate;
343 /// The relative rotation speed of the two bodies in radians per second.
344 @property(nonatomic, assign) cpFloat rate;
350 Gear joints constrain the rotational speed of one body to another.
351 A ratio of 1.0 will lock the rotation of two bodies together, and negative ratios will cause them to spin in opposite directions.
352 You can also use gear joints as rotary servos by setting ChipmunkConstraint.maxForce and ChipmunkConstraint.maxBias to finite values and changing the ChipmunkGearJoint.phase property.
354 @interface ChipmunkGearJoint : ChipmunkConstraint
357 Create an autoreleased gear joint between the given bodies.
358 @param phase The angular offset.
359 @param ratio The ratio of the rotational speeds.
361 + (ChipmunkGearJoint *)gearJointWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b phase:(cpFloat)phase ratio:(cpFloat)ratio;
364 Initialize a gear joint between the given bodies.
365 @param phase The angular offset in radians.
366 @param ratio The ratio of the rotational speeds.
368 - (id)initWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b phase:(cpFloat)phase ratio:(cpFloat)ratio;
370 /// The angular offset in radians.
371 @property(nonatomic, assign) cpFloat phase;
372 /// The ratio of the rotational speeds.
373 @property(nonatomic, assign) cpFloat ratio;
378 Ratchet joints create rotary ratches similar to a socket wrench.
380 @interface ChipmunkRatchetJoint : ChipmunkConstraint
383 Create an autoreleased ratchet joint between the given bodies.
384 @param phase The angular offset of the ratchet positions in radians.
385 @param ratchet The angle in radians of each ratchet position. Negative values cause the ratchet to operate in the opposite direction.
387 + (ChipmunkRatchetJoint *)ratchetJointWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b phase:(cpFloat)phase ratchet:(cpFloat)ratchet;
390 Initialize a ratchet joint between the given bodies.
391 @param phase The angular offset of the ratchet positions in radians.
392 @param ratchet The angle in radians of each ratchet position. Negative values cause the ratchet to operate in the opposite direction.
394 - (id)initWithBodyA:(ChipmunkBody *)a bodyB:(ChipmunkBody *)b phase:(cpFloat)phase ratchet:(cpFloat)ratchet;
396 /// The current ratchet position in radians.
397 @property(nonatomic, assign) cpFloat angle;
399 /// The angular offset of the ratchet positions in radians
400 @property(nonatomic, assign) cpFloat phase;
402 /// The angle in radians of each ratchet position. Negative values cause the ratchet to operate in the opposite direction.
403 @property(nonatomic, assign) cpFloat ratchet;