Skip to content

Commit 329a512

Browse files
committed
Merge branch 'develop-v3' of https://github.com/cocos2d/cocos2d-iphone into develop-v3
Former-commit-id: b63e754
2 parents 2707bd9 + e1008b0 commit 329a512

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-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/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)