Sprite Kit - Determine vector of swipe gesture to flick sprite - ios

I have a game where circular objects shoot up from the bottom of the screen and I would like to be able to swipe them to flick them in the direction of my swipe. My issue is, I don't know how to calculate the vector/direction of the swipe in order to get the circular object to get flicked in the proper direction with the proper velocity.
The static vector "(5,5)" I am using needs to be calculated by the swipe speed and direction of the swipe. Also, I need to make sure that once I make first contact with the object, it no longer happens, as to refrain from double hitting the object. Here's what I am doing currently:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKNode* node = [self nodeAtPoint:location];
[node.physicsBody applyImpulse:CGVectorMake(5, 5) atPoint:location];
}
}

Here's an example of how to detect a swipe gesture:
First, define instance variables to store the starting location and time .
CGPoint start;
NSTimeInterval startTime;
In touchesBegan, save the location/time of a touch event.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Avoid multi-touch gestures (optional) */
if ([touches count] > 1) {
return;
}
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
// Save start location and time
start = location;
startTime = touch.timestamp;
}
Define parameters of the swipe gesture. Adjust these accordingly.
#define kMinDistance 25
#define kMinDuration 0.1
#define kMinSpeed 100
#define kMaxSpeed 500
In touchesEnded, determine if the user's gesture was a swipe by comparing the differences between starting and ending locations and time stamps.
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
// Determine distance from the starting point
CGFloat dx = location.x - start.x;
CGFloat dy = location.y - start.y;
CGFloat magnitude = sqrt(dx*dx+dy*dy);
if (magnitude >= kMinDistance) {
// Determine time difference from start of the gesture
CGFloat dt = touch.timestamp - startTime;
if (dt > kMinDuration) {
// Determine gesture speed in points/sec
CGFloat speed = magnitude / dt;
if (speed >= kMinSpeed && speed <= kMaxSpeed) {
// Calculate normalized direction of the swipe
dx = dx / magnitude;
dy = dy / magnitude;
NSLog(#"Swipe detected with speed = %g and direction (%g, %g)",speed, dx, dy);
}
}
}
}

There is another way to do it, you can add a pan gesture and then get the velocity from it:
First add pan gesture in your view:
UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanFrom:)];
[self.view addGestureRecognizer:gestureRecognizer];
Then handle the gesture:
- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan) {
CGPoint location = [recognizer locationInView:recognizer.view];
if ([_object containsPoint:location]){
self.movingObject = YES;
<.. object start moving ..>
}
} else if (recognizer.state == UIGestureRecognizerStateChanged) {
if (!self.movingObject)
return;
CGPoint translation = [recognizer translationInView:recognizer.view];
object.position = CGPointMake(object.position.x + translation.x, object.position.y + translation.y);
[recognizer setTranslation:CGPointZero inView:recognizer.view];
} else if (recognizer.state == UIGestureRecognizerStateEnded) {
if (!self.movingObject)
return;
self.movingObject = NO;
float force = 1.0f;
CGPoint gestureVelocity = [recognizer velocityInView:recognizer.view];
CGVector impulse = CGVectorMake(gestureVelocity.x * force, gestureVelocity.y * force);
<.. Move object with that impulse using an animation..>
}
}

In touchesBegan save the touch location as a CGPoint you can access throughout your app.
In touchesEnded calculate the distance and direction of your initial touch (touchesBegan) and ending touch (touchesEnded). Then apply the appropriate Impulse.
To refrain from double hitting, add a bool canHit that you set to NO when the impulse is applied and set back to YES when you are ready to hit again. Before applying the impulse, make sure canHit is set to YES.

Related

Speed of touch varies with number of objects on Scene

I am creating game in which user can hit the object falling from the top of the screen with the racket. user can continuously move the racket but if it is at minimal speed or is at rest it should not hit the object, but if it above the minimal speed user should hit them. I have achieved that but the issue is when user start touching the racket which continously move with the user touch, the speed varition is their it does not start with the same speed and while touch is moving at that time also some times speed is very less even though the movement is fast. Here is my piece of code
-(void)didMoveToView:(SKView *)view {
self.physicsWorld.contactDelegate = (id)self;
racketNode = [SKSpriteNode spriteNodeWithImageNamed:#"racket"];
racketNode.size = CGSizeMake(50,50);
racketNode.position = CGPointMake(self.frame.origin.x + self.frame.size.width - 50,50);
racketNode.name = #"racket";
[self addChild:racketNode];
}
-(void) didBeginContact:(SKPhysicsContact *)contact {
SKSpriteNode *nodeA = (SKSpriteNode *)contact.bodyA.node ;
SKSpriteNode *nodeB = (SKSpriteNode *) contact.bodyB.node;
if (([nodeA.name isEqualToString:#"racket"] && [nodeB.name isEqualToString:#"fallingObject"])) {
if (racketNode.speed > kMinSpeed)
[nodeB removeFromParent];
else {
nodeB.physicsBody.contactTestBitMask = 0;
[self performSelector:#selector(providingCollsion:) withObject:nodeB afterDelay:0.1];
}
}
}
-(void) providingCollsion:(SKSpriteNode *) node {
node.physicsBody.contactTestBitMask = racketHit;
}
-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
start = location;
startTime = touch.timestamp;
racketNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:racketNode.frame.size];
racketNode.physicsBody.categoryBitMask = racket;
racketNode.physicsBody.contactTestBitMask = HitIt;
racketNode.physicsBody.dynamic = NO;
racketNode.physicsBody.affectedByGravity = NO;
[racketNode runAction:[SKAction moveTo:location duration:0.01]];
}
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
racketNode.physicsBody = nil;
racketNode.speed = 0;
}
-(void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
CGFloat dx = location.x - start.x;
CGFloat dy = location.y - start.y;
CGFloat magnitude = sqrt(dx*dx+dy*dy);
// Determine time difference from start of the gesture
CGFloat dt = touch.timestamp - startTime;
// Determine gesture speed in points/sec
CGFloat speed = magnitude/dt;
racketNode.speed = speed;
[handNode runAction:[SKAction moveTo:[touch locationInNode:self] duration:0.01]];
}
Please tell me which part my code is wrong so as to make same object collide with high speed only not on slow speed and also no collision on stable state.
Instead of doing it manually, use UIPanGestureRecognizer to handle your swipes. With it, there is a velocity property that you can use to check if the speed is greater than a given value.
Here is a great tutorial to do it:
https://www.raywenderlich.com/76020/using-uigesturerecognizer-with-swift-tutorial

Different touches that not react with each other objective c

I'm making simple game for two players on Ipad, just like ping pong, when one finger is on screen player can react with his paddle, but when second finger is on his paddle, he cant move his paddle, but he move first paddle instead, I set up some NSLog and it says that both of them moves, but that isn't right,here are sample of my code:
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
//First player
CGPoint touchLocation = [touch locationInNode:self];
SKPhysicsBody* body = [self.physicsWorld bodyAtPoint:touchLocation];
if (body && [body.node.name isEqualToString: paddleCategoryName]) {
NSLog(#"Began touch on first paddle");
self.isFingerOnPaddle = YES;
}
//Second player
CGPoint secondTouchLocation = [touch locationInNode:self];
SKPhysicsBody* secondbody = [self.physicsWorld bodyAtPoint:secondTouchLocation];
if (secondbody && [secondbody.node.name isEqualToString: secondPaddleCategoryName]) {
NSLog(#"Began touch on second paddle");
self.isSecondFingerOnPaddle = YES;
}
}
}
-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
for (UITouch *touch in touches) {
//for first player
if (self.isFingerOnPaddle) {
CGPoint touchLocation = [touch locationInNode:self];
CGPoint previousLocation = [touch previousLocationInNode:self];
SKSpriteNode* paddle = (SKSpriteNode*)[self childNodeWithName: paddleCategoryName];
int paddleY = paddle.position.y + (touchLocation.y - previousLocation.y);
paddleY = MAX(paddleY, paddle.size.height/2);
paddleY = MIN(paddleY, self.size.height - paddle.size.height/2);
paddle.position = CGPointMake(paddle.position.x, paddleY);
NSLog(#"First paddle moving");
}
//for second player
if (self.isSecondFingerOnPaddle) {
CGPoint touchLocation = [touch locationInNode:self];
CGPoint previousLocation = [touch previousLocationInNode:self];
SKSpriteNode* secondPaddle = (SKSpriteNode*)[self childNodeWithName: secondPaddleCategoryName];
int secondPaddleY = secondPaddle.position.y + (touchLocation.y - previousLocation.y);
secondPaddleY = MAX(secondPaddleY, secondPaddle.size.height/2);
secondPaddleY = MIN(secondPaddleY, self.size.height - secondPaddle.size.height/2);
secondPaddle.position = CGPointMake(secondPaddle.position.x, secondPaddleY);
NSLog(#"Second paddle moving");
}
}
}
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
self.isFingerOnPaddle = NO;
self.isSecondFingerOnPaddle = NO;
}
What I do wrong, and what do I need to change to my code work, like it should
A better approach would be to sub class paddle and do your touch code inside the paddle subclass. This eliminates having to loop through touch arrays to see if your paddle is being touched, plus it makes the code a lot neater and easier to read. The only thing is you will have to style your node set up a little differently, because touch moved will not work outside of the paddle.
Here is how you style it
1) Subclass Paddle to SKNode
2) Create a child of SKSpriteNode for your actual paddle gfx
3) Paddle frame should be set at the width of your scene, with a height that is allowed for the player to touch (probably the height of the paddle gfx)
4) override the touch code inside Paddle, all touch code in the set should only relate to what is being touched by the paddle
5) Do all of your sliding logic inside these overrides
Now you have Paddle defined for the behavior you are looking for, you can add it to the scene where ever you like.
The nice part about this method is you eliminate a lot of duplicate code, (Since the paddles on both sides behave differently) and if you want to add some fun, you can add even more paddles to both sides to provide more variation. (Each player has a paddle in the middle of the play field, and the bottom, which can be moved independantly)
your logic is wrong, just add a touchArray property to store first touch and second touch, set it when touch begin then determine when touches moved.
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInNode:self];
SKSpriteNode *node = (SKSpriteNode *)[self nodeAtPoint:touchLocation];
if (node && [node.name isEqualToString: paddleCategoryName]) {
NSLog(#"Began touch on first paddle");
[self.touchArray replaceObjectAtIndex:0 withObject:touch];
}else if (node && [node.name isEqualToString: secondPaddleCategoryName]) {
NSLog(#"Began touch on second paddle");
[self.touchArray replaceObjectAtIndex:1 withObject:touch];
}
}
}
-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInNode:self];
UITouch *firstTouch = self.touchArray[0];
UITouch *secondTouch = self.touchArray[1];
SKSpriteNode *paddle = [[SKSpriteNode alloc] init];
if (touch == firstTouch) {
CGPoint previousLocation = [touch previousLocationInNode:self];
SKSpriteNode* paddle = (SKSpriteNode*)[self childNodeWithName: paddleCategoryName];
int paddleY = paddle.position.y + (touchLocation.y - previousLocation.y);
paddleY = MAX(paddleY, paddle.size.height/2);
paddleY = MIN(paddleY, self.size.height - paddle.size.height/2);
paddle.position = CGPointMake(paddle.position.x, paddleY);
NSLog(#"First paddle moving");
}else if (touch == secondTouch) {
CGPoint touchLocation = [touch locationInNode:self];
CGPoint previousLocation = [touch previousLocationInNode:self];
SKSpriteNode* secondPaddle = (SKSpriteNode*)[self childNodeWithName: secondPaddleCategoryName];
int secondPaddleY = secondPaddle.position.y + (touchLocation.y - previousLocation.y);
secondPaddleY = MAX(secondPaddleY, secondPaddle.size.height/2);
secondPaddleY = MIN(secondPaddleY, self.size.height - secondPaddle.size.height/2);
secondPaddle.position = CGPointMake(secondPaddle.position.x, secondPaddleY);
NSLog(#"Second paddle moving");
}
}
}
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
for (UITouch *touch in touches) {
if (touch == self.touchArray[0]) {
[self.touchArray replaceObjectAtIndex:0 withObject:#""];
}else if (touch == self.touchArray[1]) {
[self.touchArray replaceObjectAtIndex:1 withObject:#""];
}
}
self.isFingerOnPaddle = NO;
self.isSecondFingerOnPaddle = NO;
}

Sprite Kit: How to make a node "jump" and also move it left or right, dependant on node position and length of touch?

This is my first post so go easy haha.
I'm new to 'iOS' 'coding', 'Xcode' and 'spritekit'. I'm looking to make an image node "jump" a distance on the positive y-axis if I touch anywhere on the screen, although if I touch somewhere to the left or right if the image and hold for a certain time, it moves in the respective left or right direction, a distance respective to the length of the touch.
Not sure if that's very clear, but any help would be appreciated! Thanks!
You could move a node like this
In touchesEnded: or touchesBegan: method:
{
node.position.y += 50;
}
In order for sprite to move somewhere you could also use actions, there is a family of actions like moveTo action.
I would suggest picking upa tutorial or a book about sprite kit. Good site with free tutorials is raywenderlich.com
Please Try this code :
a sprite move : left/right according to your touch direction anywhere on screen
// CGPoint initialPos; // in .h file
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPt = [touch locationInView:touch.view];
initialPos = [[CCDirector sharedDirector] convertToGL:touchPt];
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch=[touches anyObject];
CGPoint currentPos=[myTouch locationInView:[myTouch view]];
currentPos=[[CCDirector sharedDirector] convertToGL:currentPos];
float diffX = currentPos.x - initialPos.x;
float diffY = currentPos.y - initialPos.y;
CGPoint velocity = ccp(diffX, diffY);
initialPos = currentPos;
[Sprite setPosition:ccpAdd([SmileBall position], velocity)];
}
- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch=[touches anyObject];
CGPoint currentPos=[myTouch locationInView:[myTouch view]];
currentPos=[[CCDirector sharedDirector] convertToGL:currentPos];
float diffX = currentPos.x - initialPos.x;
float diffY = currentPos.y - initialPos.y;
CGPoint velocity = ccp(diffX, diffY);
initialPos = currentPos;
[Sprite setPosition:ccpAdd([SmileBall position], velocity)];
}
a sprite jump up: according to your touch on screen
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CCSprite *RunningChar;
RunningChar=(CCSprite *)[self getChildByTag:10];
UITouch *touch = [touches anyObject];
CGPoint touchPt = [touch locationInView:touch.view];
touchPt = [[CCDirector sharedDirector] convertToGL:touchPt];
float radian = ccpToAngle(ccpSub(touchPt, prevPoint));
float degrees = CC_RADIANS_TO_DEGREES(radian);
if (degrees >= 22.5 && degrees <= 112.5) // for upward direction :). otherwise you can write only code without ant condition
{
CCJumpTo *jump=[CCJumpTo actionWithDuration:0.7 position:CGPointMake(RunningChar.position.x, 87) height:110 jumps:1];
[RunningChar runAction:seq];
}
else
{
}
}
Try this :)

touchesMoved, how do I make my view catch up - and stay under the finger?

I have written some code that restricts the movement of a box (UIView) to a grid.
When moving the box, the movement is locked to the grid and the box starts getting behind your finger if you drag diagonally or really fast.
So what is the best way to write a method that makes the box catch up and get back under the finger - it must move on the same path as your finger - and it must also not move through other boxes, so it needs collision detection - so I just can't do an Animate to new center point.
Any suggestions?
This is current code in use:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.superview];
lastLocation = location;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.superview];
CGPoint offset = CGPointMake(self.center.x + location.x - lastLocation.x, self.center.y + location.y - lastLocation.y);
CGPoint closestCenter = [self closestCenter:offset];
CGRect rect = CGRectMake(offset.x - (self.size.width / 2), offset.y - (self.size.height / 2), self.size.width, self.size.height);
if (fabsf(closestCenter.x - offset.x) < fabsf(closestCenter.y - offset.y)) {
offset.x = closestCenter.x;
}
else {
offset.y = closestCenter.y;
}
// Do collision detection - removed for clarity
lastLocation = location;
self.center = offset;
}
Don't use a relative offset movement. Instead, use the actual touch location as the desired position and then bound (modify) it based on the grid restrictions. In this way you won't get any lag behind the touch.
From your collision detection I guess there is a path that must be followed and a naive implementation will 'jump' the view to the touch across boundaries. A simple solution to this is to limit jumping to a maximum of half a grid square (so the user must bring the touch back to the view if they drop it).

Panning a subview of UIScrollView after zooming in

I have added a subview to a UIScrollView. When I zoom into the scroll view I want to pan around the subview.
In touchesBegan: I'm getting the initial location of the touch and then touchesMoved: I am able to determine how much to move the subview. It works perfectly when zoomscale is 1.0. However, when it is zoomed the pointer "breaks out" of the subview which it is intended to move (illustration here - pointer location is ilustrated as marquee tool).
The center of the view should be on pointer location, and not in it's current position! px and py variables ensure that wherever on the subview is clicked, while dragging it postion of the pointer always stays the same. illustration
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
location.x = location.x * self.zoomScale;
location.y = location.y * self.zoomScale;
px = location.x;
py = location.y;
if ([touch view] == rotateView) {
self.scrollEnabled = NO;
return;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
location.x = location.x * self.zoomScale;
location.y = location.y * self.zoomScale;
if ([touch view] == rotateView) {
rotateView.center = CGPointMake(rotateView.center.x + (location.x - px), rotateView.center.y + (location.y - py));
px = location.x;
py = location.y;
return;
}
    }
Instead of the approach you're taking, make the subview another UIScrollView and let it handle the panning.
(You may wish to set scrollEnabled = NO on your subview until zooming has occurred.)

Resources