SpriteKit: SKPhysicsBody slips away - ios

I have a SKSpriteNode (Bird) with a SKPhysicsBody (Circle).
The bird node is affected by gravity and falls down, until it reaches a y position of 100px.
override func didSimulatePhysics() {
if (bird.position.y < 100) {
bird.position = CGPointMake(bird.position.x, 100)
}
}
The problem is, that the physicsBody is moving away from the Node.
The physicsBody should stay centered on the Bird.
As soon as I tap the screen the Bird gets an impulse and moves up. Immediately the physicsBody is back in place.
This is the physicsBody code:
bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.frame.width * 0.5)
bird.physicsBody!.contactTestBitMask = PhysicsCategory.LeftBoard | PhysicsCategory.RightBoard | PhysicsCategory.Border
bird.physicsBody!.categoryBitMask = PhysicsCategory.Bird
bird.physicsBody!.collisionBitMask = PhysicsCategory.None
bird.physicsBody!.mass = 1
bird.physicsBody!.affectedByGravity = false
bird.physicsBody!.allowsRotation = false

You need to stop the velocity. You are forcing the position but the velocity continues after each step.
Also you should typically make such changes in the update method before the physics are simulated.

Related

How do I stop physicsbody's from falling in SpriteKit?

I am making a game in Swift using spritekit and I want 2 objects on screen, one that I move with the touchesMoved function (player) and the other to stay still in its position in the screen (cell).
I wanted to make the objects physicsbody's so that I could work out collisions between them, but now whenever the scene loads the objects fall straight away. And the object moved through touch falls as soon as the user stops touching the screen.
Here's all of the code relating to the object that should stay stationary in the scene and I am unsure what makes it fall.
//Defining the cell
let cell = SKSpriteNode(imageNamed: "cell.png")
override func didMove(to view: SKView){
self.physicsWorld.contactDelegate = self
cell.physicsBody?.categoryBitMask = cellCategory
cell.physicsBody?.contactTestBitMask = playerCategory
cell.physicsBody?.isDynamic = true
cell.physicsBody = SKPhysicsBody(rectangleOf: cell.frame.size)
//Adding the cell to the scene
cell.position = CGPoint(x:size.width * 0.9, y:size.height * 0.9)
cell.size = CGSize(width: 50, height: 50)
addChild(cell)
Any help in what makes the object fall and how I can stop it from happening would be greatly appreciated.
Gravity is by default on. You can either turn it off entirely by setting the gravity property of the scene's physicsWorld:
// somewhere in the scene's initialization, or in didMove if you like
physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)
See https://developer.apple.com/documentation/spritekit/skphysicsworld for info on that.
Or you can turn off whether individual objects are affected by gravity. E.g.,
let body = SKPhysicsBody(circleOfRadius: 10.0)
body.affectedByGravity = false
See https://developer.apple.com/documentation/spritekit/skphysicsbody, and in particular, https://developer.apple.com/documentation/spritekit/skphysicsbody/1519774-affectedbygravity
You have a different problem in that you're setting the properties of your cell's physics body before you're creating it. cell.physicsBody?... = will not assign anything until cell.physicsBody exists. So those assignments must be moved after you create cell.physicsBody = SKPhysicsBody(rectangleOf: ...)

SpriteKit contact detection has slight delay

I'm creating a small game where the ball needs to bounce on the upper, left and right wall, while it should stop as soon as it touches the floor.
I already have a working contact-detection code and delegate that is called in the correct situation and with the correct timing.
Creating a small circle in the collision point found in the func didBegin(_ contact: SKPhysicsContact) creates the node in the correct position.
In the same method I call ball.physicsBody?.velocity = .zero and ball.position = contact.contactPoint and the ball correctly stops. The problem is that sometimes the ball stops a frame too late, after it's already bounced off the floor.
The ball has a restitution of 1 because I would like it to bounce without slowing down, except when touching the floor.
I have already tried to set the position to zero, the velocity to zero, its isDynamic attribute to false. Each of these tries does what it's supposed to do, but sometimes it happens a frame too late.
Both the ball and the floor have the usesPreciseCollisionDetection property set to true.
If I try to add some artificial delay, the contact method is called again (breaking part of the gameplay).
Here is the code of my ball node:
class Ball: SKSpriteNode {
convenience init() {
self.init(imageNamed: "earth")
physicsBody = SKPhysicsBody(circleOfRadius: 12.5)
physicsBody?.usesPreciseCollisionDetection = true
physicsBody?.categoryBitMask = Masks.ball
physicsBody?.contactTestBitMask = Masks.floor
physicsBody?.allowsRotation = false
physicsBody?.restitution = 1
physicsBody?.friction = 0
physicsBody?.angularDamping = 0
physicsBody?.linearDamping = 0
}
}
and here is the code for my didBegin method:
func didBegin(_ contact: SKPhysicsContact) {
ball.physicsBody?.velocity = .zero
ball.position = contact.contactPoint
}
I would expect the ball to stop while still touching the floor, and not a frame after it's already bounced off.

Projectile not originating from the correct location

I am trying to make a SKSpriteNode come from a SKShapeNode. When the below code runs, the projectiles are appearing, but they will originate from a different point on the screen, not the player location.
Here is my shoot function that is in my Player Class.
func shoot() {
let newProjectile = Projectile()
newProjectile.position = self.position
self.addChild(newProjectile)
let action = SKAction.moveTo(CGPointMake(
600 * -cos(newProjectile.zRotation - 1.57079633) + newProjectile.position.x,
600 * -sin(newProjectile.zRotation - 1.57079633) + newProjectile.position.y
), duration: 2.0)
let actionMoveDone = SKAction.removeFromParent()
newProjectile.runAction(SKAction.sequence([action, actionMoveDone]))
}
Here is my Projectile Class :
class Projectile : SKSpriteNode {
let Texture = SKTexture(imageNamed: "image.png")
static var counter : Int = 0
init(){
//super.init()
super.init(texture: Texture, color: UIColor.whiteColor(), size: CGSize(width: radius * 2, height: radius * 2))
self.name = "projectile-" + NSUUID().UUIDString
self.physicsBody = SKPhysicsBody(circleOfRadius: radius)
self.physicsBody?.categoryBitMask = GlobalConstants.Category.projectile
self.physicsBody?.collisionBitMask = GlobalConstants.Category.projectile
self.physicsBody?.contactTestBitMask = GlobalConstants.Category.projectile | GlobalConstants.Category.wall
self.zPosition = GlobalConstants.ZPosition.projectile
}
}
you probably need to add your projectiles to the scene instead of the player itself. you're adding the projectiles to your player.
you should add the projectile to the parents scene. something like this.
self.scene!.addChild(newProjectile)
If you want the projectiles to be children of the player, then the projectile's position must be expressed in terms of the player i.e. A position of (0, 0) will place the projectile at the player's anchor point.
If you want the projectiles to be children of the scene, then the projectile's position will be expressed in terms of the scene i.e. a position equal to the player's position will be needed to put the projectile on top of the player.
You combined the two, causing the projectile to be placed with the (x,y) position of the player in the scene, but relatIve to the player, which means that wherever the player is compared to the scene's origin, then that's where the projectile will appear relative to the player.

Nodes Spawning off of screen in sprite kit swift

I am building a ios game with swift and I have run into a bit of a problem. I am trying to spawn balls from the top of the screen and have them come down towards the ground. They are supposed to have random x values and go down at random rates but instead of spawning on the screen the nodes spawn on an x value which is not encompassed by the screen. Please help me as I think I have done everything right.
Here is the code for my addball function...
func addBall(){
//create ball sprite
var ball = SKSpriteNode(imageNamed: "ball.png")
//create physics for ball
ball.physicsBody = SKPhysicsBody(rectangleOfSize: ball.size) // 1
ball.physicsBody?.dynamic = true // 2
ball.physicsBody?.categoryBitMask = PhysicsCategory.Ball // 3
ball.physicsBody?.contactTestBitMask = PhysicsCategory.Person & PhysicsCategory.Ground
ball.physicsBody?.collisionBitMask = PhysicsCategory.None // 5
//generate random postion along x axis for ball to spawn
let actualX = random(min:ball.frame.size.width/2+1, max: self.frame.size.width - ball.frame.size.width/2-1)
println(actualX)
//set balls positon
ball.position = CGPoint(x: actualX, y: size.height - ball.size.width/2)
//add ball to scene
addChild(ball)
//determine speed of ball
let actualDuration = random(min: CGFloat(3.0), max: CGFloat(5.0))
//create movement actions
let actionMove = SKAction.moveTo(CGPoint(x:actualX, y: -ball.size.width/2), duration: NSTimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()
ball.runAction(SKAction.sequence([actionMove, actionMoveDone]), withKey: "action")
}
here is the code for my random functions
func random() -> CGFloat {
return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}
func random(#min: CGFloat, max: CGFloat) -> CGFloat {
return random() * (max - min) + min
}
The problem here is that your SKScene likely takes up much more space than the screen of your device. Thus, when you calculate a random value using the whole scene, some of the time the ball will spawn in the area of the scene not visible to you.
The two main properties that control the scene's size are its size and scaleMode properties. The scaleMode property relates to how the scene is mapped. Unless you initialized and presented this scene yourself, you can check the scaleMode in your view controller. It will likely be set to aspectFill, which according to Apple means:
The scaling factor of each dimension is calculated and the larger of the two is chosen. Each axis of the scene is scaled by the same scaling factor. This guarantees that the entire area of the view is filled but may cause parts of the scene to be cropped.
If you don't like this, there are other scaleModes. However, in most cases this mode would actually be preferable since SpriteKit's internal scaling is able to make universal apps. If this is fine for you, then the easiest thing to do is set hardcoded values for something like the spawn locations for your ball node.

Prevent sprite rotation from changing gravity

I have a function in SpriteKit that spawns a sprite (which is a square) at the top of the screen, and then gravity pulls the sprite to the bottom of the screen. I'm trying to get the sprite to rotate smoothly for an indefinite amount of time until it is removed when it reaches the bottom of the screen. The following code is in the class for the sprite:
func rotate() {
var action = SKAction.rotateByAngle(CGFloat(M_PI_2), duration: NSTimeInterval(1.5))
var repeatAction = SKAction.repeatActionForever(action)
self.runAction(repeatAction)
}
The problem that I am having is that, as the sprite turns, the sprite travels in the direction of the bottom of itself, not towards the bottom of the screen (the direction gravity is supposed to be). To further clarify, the object rotates, but as it rotates to 90 degrees, it travels sideways instead of downwards. This doesn't make sense to me. This is the code I'm using to add gravity in the didMoveToView() function:
self.physicsWorld.gravity = CGVectorMake(0.0, -1.8)
and this is the code used to spawn the sprite (the rs.rotate() calls the rotate method that is listed above):
func spawnRedSquares() {
if !self.gameOver {
let rs = RedSquare()
var rsSpawnRange = randomNumberBetween(self.leftSideBar.position.x + rs.sprite.size.width / 2, secondNum: self.rightSideBar.position.x - rs.sprite.size.width / 2)
rs.position = CGPointMake(rsSpawnRange, CGRectGetMaxY(self.frame) + rs.sprite.size.height * 2)
rs.zPosition = 3
self.addChild(rs)
self.rsArray.append(rs)
rs.rotate()
let spawn = SKAction.runBlock(self.spawnRedSquares)
let delay = SKAction.waitForDuration(NSTimeInterval(timeBetweenRedSquares))
let spawnThenDelay = SKAction.sequence([delay, spawn])
self.runAction(spawnThenDelay)
}
}
How can I get the object to rotate, but still fall down as if it were affected by normal gravity?
It looks like you are adding the rotation action to 'self' which I would assume is your scene as opposed to your sprite. This is causing the entire scene to rotate, and therefore its gravity is rotating as well.
Add the rotating action to your sprite and that should solve the issue.
Example: assuming your square sprite is called squareSprite:
let action = SKAction.rotateByAngle(CGFloat(M_PI_2), duration: NSTimeInterval(2))
let repeatAction = SKAction.repeatActionForever(action)
squareSprite.runAction(repeatAction) //Add the repeatAction to your square sprite

Resources