Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sprite Kit - Determine vector of swipe gesture to flick sprite

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];
}
}
like image 207
Mike Simz Avatar asked Oct 24 '14 16:10

Mike Simz


People also ask

How do you handle swipes in sprite kit?

Handling touches and swipes is right at the core of most apps – after all, it’s how your users interact with the user interface! In Sprite Kit, there’s no UIButton equivalent to handle simple taps, and you need to implement touch handling yourself with callbacks or with gesture recognizers.

Is there a UIButton equivalent in sprite kit?

In Sprite Kit, there’s no UIButton equivalent to handle simple taps, and you need to implement touch handling yourself with callbacks or with gesture recognizers. How to use gesture recognizers with Sprite Kit for even more cool effects!

What is SpriteKit used for?

SpriteKit. The SpriteKit framework makes it easy to create high-performance, battery-efficient 2D games. With support for custom OpenGL ES shaders and lighting, integration with SceneKit, and advanced new physics effects and animations, you can add force fields, detect collisions, and generate new lighting effects in your games.

How do I set the anchor point in sprite kit?

The anchor point in Sprite Kit is at the center of the node by default, which means you're usually setting the center point when you set a node's position. By setting the anchor point to the lower left corner, when you set the position of the node, you are now setting where the lower left corner is.


1 Answers

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);
            }
        }
    }
}
like image 169
0x141E Avatar answered Oct 08 '22 13:10

0x141E