Skip to content

Commit b4201f4

Browse files
committed
Added better position handling in template
Former-commit-id: c6cecd9
1 parent 3a4f5ae commit b4201f4

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

templates/cocos2d iOS.xctemplate/Newton/LightBulb.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ - (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
6060
- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event
6161
{
6262
// Place the light bulb at the touch position
63-
self.position = touch.locationInWorld;
63+
self.position = [_parent convertToNodeSpace:touch.locationInWorld];
6464
}
6565

6666
// -----------------------------------------------------------------------

templates/cocos2d iOS.xctemplate/Newton/NewtonScene.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ - (instancetype)init
5656
{
5757
// Apple recommend assigning self with supers return value, and handling self not created
5858
self = [super init];
59+
self.scale = 0.5;
5960
if (!self) return(nil);
6061

6162
// Here is where custom code for the newton scene starts

templates/cocos2d iOS.xctemplate/Newton/NewtonSphere.m

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,12 @@ - (BOOL)hitTestWithWorldPos:(CGPoint)pos
154154

155155
// To fix this, the hit test function is overridden.
156156
// As this is a simple sphere, the hit test will return YES, if the touch distance to sphere, is less than radius
157+
158+
// get the position in the node
159+
CGPoint nodePos = [self convertToNodeSpace:pos];
157160

158-
// calculate distance from touch to node position
159-
float distance = ccpDistance(pos, self.position);
161+
// calculate distance from touch to node center
162+
float distance = ccpLength(nodePos);
160163
return(distance < _sphere.contentSize.width * 0.5);
161164
}
162165

@@ -166,26 +169,34 @@ - (BOOL)hitTestWithWorldPos:(CGPoint)pos
166169

167170
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
168171
{
172+
// convert the touch into parents coordinate system
173+
// This is often the same as the world location, but if the scene is ex, scaled or offset, it might not be
174+
CGPoint parentPos = [_parent convertToNodeSpace:touch.locationInWorld];
175+
169176
// The spehre was grabbed.
170177
// To move the sphere around in a "believeable" manner, two things has to be done
171178
// 1) the mass has to be increased, to simulate "an unstopable force" (please dont add an imovable object to the space, or chipmunk will crash ... no it wont :)
172179
self.physicsBody.mass = NewtonSphereMovingMass;
173-
180+
174181
// 2) Save state data, like time and position, so that a velocity can be calculated when moving the sphere.
175182
// Velocity must be set on forced movement, otherwise the collisions will feel "mushy"
176183
_grabbed = YES;
177184
_previousVelocity = CGPointZero;
178185
_previousTime = event.timestamp;
179-
_previousPos = touch.locationInWorld;
186+
_previousPos = parentPos;
180187
CCLOG(@"A Newton Sphere was touched");
181188
}
182189

183190
- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event
184191
{
192+
// convert the touch into parents coordinate system
193+
// This is often the same as the world location, but if the scene is ex, scaled or offset, it might not be
194+
CGPoint parentPos = [_parent convertToNodeSpace:touch.locationInWorld];
195+
185196
// on each move, calculate a velocity used in update, and save new state data
186-
_previousVelocity = ccpMult( ccpSub(touch.locationInWorld, _previousPos), 1 / (event.timestamp - _previousTime));
197+
_previousVelocity = ccpMult( ccpSub(parentPos, _previousPos), 1 / (event.timestamp - _previousTime));
187198
_previousTime = event.timestamp;
188-
_previousPos = touch.locationInWorld;
199+
_previousPos = parentPos;
189200
}
190201

191202
- (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event

0 commit comments

Comments
 (0)