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 #import "ObjectiveChipmunk/ObjectiveChipmunk.h"
24 @interface ChipmunkGrab : NSObject<ChipmunkObject> {
25 NSArray *_chipmunkObjects;
30 ChipmunkShape *_grabbedShape;
35 /// Last touch location of the grab.
36 @property(nonatomic, readonly) cpVect pos;
38 /// The ChipmunkShape that this grab was created for.
39 @property(nonatomic, readonly) ChipmunkShape *grabbedShape;
41 /// User definable pointer
42 @property(nonatomic, retain) id data;
47 /// Simple class to implement multitouch grabbing of physics objects.
48 @interface ChipmunkMultiGrab : NSObject {
49 ChipmunkSpace *_space;
50 NSMutableArray *_grabs;
55 cpFloat _grabFriction;
56 cpFloat _grabRotaryFriction;
60 bool (^_grabFilter)(ChipmunkShape *shape);
61 cpFloat (^_grabSort)(ChipmunkShape *shape, cpFloat depth);
63 bool _pushMode, _pullMode;
66 cpFloat _pushFriction;
67 cpFloat _pushElasticity;
68 cpCollisionType _pushCollisionType;
71 @property(nonatomic, assign) cpFloat smoothing;
72 @property(nonatomic, assign) cpFloat grabForce;
74 /// Layers used for the point query when grabbing objects.
75 @property(nonatomic, assign) cpShapeFilter filter;
77 /// Group used for the point query when grabbing objects
78 @property(nonatomic, assign) cpGroup group;
80 /// Gives you the opportunity to further filter shapes. Return FALSE to ignore a shape.
81 /// The default implementation always returns TRUE.
82 @property(nonatomic, copy) bool (^grabFilter)(ChipmunkShape *shape);
84 /// When clicking on a spot where two shapes overlap, the default behavior is to grab the shape that
85 /// overlaps the grab point the most. It's possible to use a custom sorting order instead however.
86 /// The block is called with each shape and the grab depth.
87 /// It should return a positive float. The shape with the highest value is grabbed.
88 /// The block is only called if the touch location is within a shape.
89 @property(nonatomic, copy) cpFloat (^grabSort)(ChipmunkShape *shape, cpFloat depth);
91 /// Amount of friction applied by the touch.
92 /// Should be less than the grabForce. Defaults to 0.0.
93 @property(nonatomic, assign) cpFloat grabFriction;
95 /// The amount torque to apply to the grab to keep it from spinning.
97 @property(nonatomic, assign) cpFloat grabRotaryFriction;
99 /// On a touch screen, a single point query can make it really hard to grab small objects with a fat finger.
100 /// By providing a radius, it will make it much easier for users to grab objects.
102 @property(nonatomic, assign) cpFloat grabRadius;
104 @property(nonatomic, assign) bool pullMode;
105 @property(nonatomic, assign) bool pushMode;
107 @property(nonatomic, assign) cpFloat pushMass;
108 @property(nonatomic, assign) cpFloat pushFriction;
109 @property(nonatomic, assign) cpFloat pushElasticity;
110 @property(nonatomic, assign) cpCollisionType pushCollisionType;
112 @property(nonatomic, readonly) NSArray *grabs;
116 @c space is the space to grab shapes in.
117 @c smoothing is the amount of mouse smoothing to apply as percentage of remaining error per second.
118 cpfpow(0.8, 60) is a good starting point that provides fast response, but smooth mouse updates.
119 @c force is the force the grab points can apply.
121 -(id)initForSpace:(ChipmunkSpace *)space withSmoothing:(cpFloat)smoothing withGrabForce:(cpFloat)grabForce;
123 /// Start tracking a new grab point
124 /// Returns the ChipmunkGrab that is tracking the touch, but only if a shape was grabbed.
125 /// Returns nil when creating a push shape (if push mode is enabled), or when no shape is grabbed.
126 -(ChipmunkGrab *)beginLocation:(cpVect)pos;
128 /// Update a grab point.
129 /// Returns the ChipmunkGrab that is tracking the touch, but only if the grab is tracking a shape.
130 -(ChipmunkGrab *)updateLocation:(cpVect)pos;
132 /// End a grab point.
133 /// Returns the ChipmunkGrab that was tracking the touch, but only if the grab was tracking a shape.
134 -(ChipmunkGrab *)endLocation:(cpVect)pos;