I\'m trying to make a game in which I have some SKSpriteNodes and the User can hit them with finger movement, I\'m using apple\'s new Sprite Kit.
To
Haven't had time to actually run this but how about something like this: don't bother with putting a node X like described above, rather:
In touchesBegan, keep track of the time and position of the touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
//store location of first touch
self.firstTouch = location;
//get time
self.startTouch = [NSDate date];
}
In touchesMoved, check if you have touched any of the nodes, If so, work out the angle and speed based on the start and end positions of the touch and then apply velocity to the impacted node?
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
//if you've touched one of your nodes
if ([node.name isEqualToString:@"whateverNode"]) {
//work out time between touches
NSTimeInterval timeBetween = [[NSDate date] timeIntervalSinceDate:self.startTouch];
//work out angle between touches (if you want to send the impacted node in that direction)
float angle = atan2f (location.y - self.firstTouch.y, location.x - self.firstTouch.y) ;
//apply to node
CGFloat thrust = 0.01/timeBetween;
CGPoint thrustVector = CGPointMake(thrust*cosf(angle),
thrust*sinf(angle));
[node.physicsBody applyTorque:thrustVector];
}
}
Note the physics bit at the end could be (completely) incorrect.
After searching and making a lot of experiments, I finally have an answer!
The first step in the solution was provided by AyatollahAndy - finding the point of impact:
SKNode *node = [self nodeAtPoint:location];
this line gets the node that we hit in the specified location, if it returns nil we didn't hit anything.
secondly- we want to "Hit" the object, therefor we need to ApplyImpulse:
[node.physicsBody applyImpulse:CGVectorMake(thrustV.x, thrustV.y) atPoint:location];
Here you can see I applied the impulse with some vector that I created at the point of impact, That's it - pretty straight forward, I hope this will help others.
Sharing is caring, Thanks for taking the time to read my post.
You could do something like this.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
touchOn=YES;
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self.parent];
touchPos =location;
[self.physicsBody setVelocity:CGVectorMake(0, 0)];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
touchOn=YES;
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self.parent];
touchPos =location;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
touchOn=NO;
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
touchOn=NO;
}
-(void)update:(NSTimeInterval)dt {
if (touchOn) {
CGVector vector = CGVectorMake((touchPos.x-self.position.x)/dt, (touchPos.y-self.position.y)/dt);
self.physicsBody.velocity=vector;
}
}
Subclass a node and make a method called update that receives updates from the scene along with the change in time. Also add touches methods and keep track of the current touch position.