SpriteKit move physics body on touch (air hockey game) - ios

I'm trying to make an air hockey game using SpriteKit. I trying to make the pucks draggable but I can't make them continue to move after the touch has ended. Right now I am binding the touch and the puck and setting it's position when the touch moves.
using the physics system:
override func update(currentTime: NSTimeInterval) {
for (touch, node) in draggingNodes {
let targetPosition = touch.locationInNode(self)
let distance = hypot(node.position.x - targetPosition.x, node.position.y - targetPosition.y)
var damping = sqrt(distance * 100)
if (damping < 0) {
damping = 0.0
}
node.physicsBody!.linearDamping = damping
node.physicsBody!.angularDamping = damping
let translation = CGPointMake(targetPosition.x - node.position.x, targetPosition.y - node.position.y)
node.physicsBody!.velocity = CGVectorMake(translation.x * 100, translation.y * 100);
}
}

You're likely going to need to do a lot more reading. Either you'll use the physics system:
In which case you'll impart an impulse onto the puck on the touch end event, having calculated the speed based on a delta in position and delta in time from last frame to current frame (or some type of average over more than 1 frame).
https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Physics/Physics.html
[OR]
You'll manually set velocity on the puck (not using the physics system), and then manually update the puck's position per frame based on that velocity, then recalculate its vector when it comes into contact with another object based on angle of of incidence.

Related

How to concisely control a spriteNode's movement with swipes

I am building a spriteKit game in Xcode 10.2.1 with swift 5, where the player has to reach the end of a large background (avoiding enemies, obstacles etc.) in order to complete the level. The camera is tied to the player spriteNode's position.x and the player can move the spriteNode in any direction with swipes.
I am using a cobbled together UIPanGestureRecognizer to enable this movement, and this seems to work reasonably well.
#objc func handlePan(recognizer: UIPanGestureRecognizer) {
let transformerX = 1024/self.view!.frame.size.width
let transformerY = 768/self.view!.frame.size.height
if recognizer.state == .began {
lastSwipeBeginningPoint = recognizer.location(in: self.view)
} else if (recognizer.state == .ended) {
if scrolling { // this is controlls whether player can be moved - scrolling is set to true once introductory elements have run
if playerDamage < 4 { //player cannot be controlled and falls off screen if it has 4 damage
let velocity = recognizer.velocity(in: self.view)
player.physicsBody?.applyImpulse(CGVector(dx: velocity.x * transformerX / 5.4, dy: velocity.y * transformerY * -1 / 5.4)) //-1 inverts y direction so it moves as expected
}
}
}
}
This does allow movement around the level, but not in as precise and controlled a way as I would like. The player moves off at the set speed but any further swipes (to change direction/avoid obstacles) seem to multiply the speed and generally make it difficult to control accurately.
Is there a way of choking off the speed, so that it starts off at the set speed but then starts to lose momentum, or is there a better way altogether of controlling multi-direction movement?

SpriteKit: how to smoothly animate SKCameraNode while tracking node but only after node moves Y pixels?

This question and others discuss how to track a node in SpriteKit using a SKCameraNode.
However, our needs vary.
Other solutions, such as updating the camera's position in update(_ currentTime: CFTimeInterval) of the SKScene, do not work because we only want to adjust the camera position after the node has moved Y pixels down the screen.
In other words, if the node moves 10 pixels up, the camera should remain still. If the node moves left or right, the camera should remain still.
We tried animating the camera's position over time instead of instantly, but running a SKAction against the camera inside of update(_ currentTime: CFTimeInterval) fails to do anything.
I just quickly made this. I believe this is what you are looking for?
(the actual animation is smooth, just i had to compress the GIF)
This is update Code:
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
SKShapeNode *ball = (SKShapeNode*)[self childNodeWithName:#"ball"];
if (ball.position.y>100) camera.position = ball.position;
if (fabs(ball.position.x-newLoc.x)>10) {
// move x
ball.position = CGPointMake(ball.position.x+stepX, ball.position.y);
}
if (fabs(ball.position.y-newLoc.y)>10) {
// move y
ball.position = CGPointMake(ball.position.x, ball.position.y+stepY);
}
}
I would not put this in the update code, try to keep your update section clutter free, remember you only have 16ms to work with.
Instead create a sub class for your character node, and override the position property. What we are basically saying is if your camera is 10 pixels away from your character, move towards your character. We use a key on our action so that we do not get multiple actions stacking up and a timing mode to allow for the camera to smoothly move to your point, instead of being instant.
class MyCharacter : SKSpriteNode
{
override var position : CGPoint
{
didSet
{
if let scene = self.scene, let camera = scene.camera,(abs(position.y - camera.position.y) > 10)
{
let move = SKAction.move(to: position, duration:0.1)
move.timingMode = .easeInEaseOut
camera.run(move,withKey:"moving")
}
}
}
}
Edit: #Epsilon reminded me that SKActions and SKPhysics access the variable directly instead of going through the stored property, so this will not work. In this case, do it at the didFinishUpdate method:
override func didFinishUpdate()
{
//character should be a known property to the class, calling find everytime is too slow
if let character = self.character, let camera = self.camera,(abs(character.position.y - camera.position.y) > 10)
{
let move = SKAction.move(to: character.position, duration:0.1)
move.timingMode = .easeInEaseOut
camera.run(move,withKey:"moving")
}
}

Move SpriteNode in direction of a swipe at a preset speed

So basically I have the coin object, and I want to launch it across the screen. I have the following code which runs some calculations on the swipe, but not sure what is relevant for my current situation:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
touching = true
let touch = touches.first
let loc = touch?.locationInNode(self)
if coin.containsPoint(loc!){
touchPoint = loc!
touchTime = (touch?.timestamp)!
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
touching = false
let touch = touches.first
let loc = touch?.locationInNode(self)
var xDiff = (loc?.x)! - touchPoint.x
var yDiff = (loc?.y)! - touchPoint.y
let distance = sqrt(xDiff * xDiff + yDiff * yDiff)
}
I want the coin to be essentially thrown in the direction of the swipe, but I want it to reach a certain y-coordinate on the screen each time, before falling back down due to gravity. So I guess I need to somehow calculate the perfect speed each time to make it reach that y-point, and then push the coin object in the swipe direction at that speed?
Any help much appreciated! I'm online for the next few hours, so ask for more info and I'll be able to get back to you pretty quick.
It depends if you want the coin to be interacting with other physics bodies afterward. You have attached several "physics" tags to the question so I assume you want that :)
I'd go for a mathematics-based approach indeed. So basically you are just getting a direction from the swipe. The speed itself is dependent on the gravity that's affecting the coin.
You can separate the speed vector in the vertical and horizontal component.
The vertical distance to cover is let verticalDistance = targetHeight - coin.position.y. The coin has to have zero vertical speed at the highest point, and we know that during the animation it decelerates with a constant -9.81 pt/s*s (off the top of my head).
So the speed graph looks like this:
with formula f(x) = -9.81 * x + v where v is the vertical starting speed we are solving for. Algebra tells us that f(x) intersects with the x-axis with x-value v / 9.81.
The total distance covered up until the point where f(x) intersects with the x-axis is equal to the surface area of the triangle under f(x). This triangle's surface is 1/2 * v/9.81 * v (which is 1/2 * width * height).
Since you know what this distance should be (verticalDistance from previous paragraph), algebra dictates v = sqrt(2 * 9.81 * d). Now you know you starting vertical speed, based on the height the coin has to travel!
Since you know the coins angle (dictated by user's swipe) and the vertical speed, you can calculate the horizontal speed. I'm leaving that to you ;) Note that we are of course ignoring drag / friction which could impact the coin speed. Also my algebra might be rusty.
As a side note
I would replace let touch = touches.first with
guard let touch = touches.first else {
return
}
In this way, touch is a non-optional.

Centering Camera on Velocity Applied Node Not Working

I have a subclass of SKNode which consists of a few sprites that make up a player. I would like the "camera" to center on this node (Always have the player in the center). Now before you down vote this for it being a duplicate, hear me out. The Apple documents suggest making the player node completely static, and instead moving around a camera node. However in my case I'm applying multiple properties of physics to my character, including velocity impulses. My first thought would be to just apply these impulses to the camera node itself, however this has become impossible due to the fact that the character has a small soft-body physics engine on it. I'm applying velocity to it like so:
player.primaryCircle.physicsBody!.velocity = CGVector(dx: player.primaryCircle.physicsBody!.velocity.dx+relVel.dx*rate, dy: player.primaryCircle.physicsBody!.velocity.dy+relVel.dy*rate)
I managed to get it to partially work with the following code:
override func didSimulatePhysics() {
self.player.position = player.primaryCircle.position
self.camera.position = player.position
centerOnNode(camera)
}
func centerOnNode(node: SKNode) {
let cameraPositionInScene: CGPoint = node.scene!.convertPoint(node.position, fromNode: node.parent!)
node.parent!.position = CGPoint(x:node.parent!.position.x - cameraPositionInScene.x, y:node.parent!.position.y - cameraPositionInScene.y)
}
However that didn't 100% work, as seen here: (It should be focused on the red circle)
http://gyazo.com/b78950e6cc15b60f390cd8bfd407ab56
As you can see, the world/map is moving, however it doesn't seem to be moving fast enough to center the player in the middle. (And note that the "Unamed" text is at a fixed spot on the screen -- That's why it seems to always be in the center)
I think this should still work with physics unless I am not truly understanding the question. We did something similar with our SKATiledMap with that Auto Follow Feature. What you need to do is make sure the player is added to a node you can move (usually a map) as a child and then in the update function you do something like this...(sorry it isn't in swift)
-(void)update
{
if (self.autoFollowNode)
{
self.position = CGPointMake(-self.autoFollowNode.position.x+self.scene.size.width/2, -self.autoFollowNode.position.y+self.scene.size.height/2);
//keep map from going off screen
CGPoint position = self.position;
if (position.x > 0)
position.x = 0;
if (position.y > 0)
position.y = 0;
if (position.y < -self.mapHeight*self.tileWidth+self.scene.size.height)
position.y = -self.mapHeight*self.tileWidth+self.scene.size.height;
if (position.x < -self.mapWidth*self.tileWidth+self.scene.size.width)
position.x = -self.mapWidth*self.tileWidth+self.scene.size.width;
self.position = CGPointMake((int)(position.x), (int)(position.y));
}
}
Map being the node that the player is added to. Hopefully that helps. Also here is the link to the git hub project we have been working on. https://github.com/SpriteKitAlliance/SKAToolKit

Enforce single-axis velocity

I'm trying to build a game, using SpriteKit, in which there's a ball that bounces up and down. Now I want to let the player control the balls movement in the X axis and let the physics engine control the Y velocity.
For example, when the ball hits a corner it starts moving sideways on it's own. I would like it to bounce of the corner and then quickly stabilize and stop moving side-ways. Is there anyway of doing this without trying to counteract any sideways movement by applying an impulse? Would it be easier to just manually control the ball's movement up and down?
I've tried applying a counteracting force without much success (the ball freaks out):
override func update(currentTime: NSTimeInterval) {
let ballDx = ball?.physicsBody?.velocity.dx
if let ballVelocityX = ballDx {
if ballVelocityX != 0 {
ball?.physicsBody?.applyForce(CGVectorMake(ballVelocityX * -1, 0))
}
}
}
Sounds like you need to apply linear damping in the x direction. Here's an example of how to do that:
// Adjust this value as needed. It should be in [0,1], where a value of 1 will
// have no effect on the ball and a value of 0 will stop the ball immediately.
let xAlpha:CGFloat = 0.95
override func update(currentTime: CFTimeInterval) {
/* Apply damping only in x */
let dx = sprite.physicsBody!.velocity.dx * xAlpha
let dy = sprite.physicsBody!.velocity.dy
sprite.physicsBody?.velocity = CGVectorMake(dx, dy)
}

Resources