Sprite Kit - Determine vector of swipe gesture to flick sprite
Asked Answered
S

3

6

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];
}
}
Stralsund answered 24/10, 2014 at 16:3 Comment(0)
D
10

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);
            }
        }
    }
}
Disposable answered 24/10, 2014 at 17:37 Comment(2)
your a boss! Thanks brother!Stralsund
@iTroyd23 adjust kMinSpeed and kMaxSpeed (and the other constants) as neededDisposable
M
1

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..>
    }
}
Multiphase answered 21/4, 2015 at 15:52 Comment(0)
H
0

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.

Hexose answered 24/10, 2014 at 16:39 Comment(1)
I do understand that, but I am having a hard time figuring out the code for it. Could you provide a code sample?Stralsund

© 2022 - 2024 — McMap. All rights reserved.