My ball (SKSpriteNode) is suppose to bounce of the wall that is around the screen, but it simply past through it, and disappeared from the screen.
GameScene.swift
let borderCategory: UInt32 = 1
let ball = SKShapeNode(circleOfRadius: 30)
let bCategoryName = "Ball"
let bCategory: UInt32 = 2
let paddle = SKShapeNode(rectOfSize: CGSize(width: 90, height: 35))
let pCategoryName = "Paddle"
let pCategory: UInt32 = 3
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.backgroundColor = SKColor.whiteColor()
self.physicsWorld.gravity = CGVectorMake(0, 0)
let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
NSLog(String(self.frame.width) + ":" + String(self.frame.height), nil)
self.physicsBody = borderBody
self.physicsBody.friction = 0.0
self.physicsBody.categoryBitMask = borderCategory
self.physicsBody.restitution = 1.0
setupPaddle()
setupBall()
}
func setupPaddle() {
paddle.name = pCategoryName
paddle.position = CGPointMake(CGRectGetMidX(self.frame), paddle.frame.size.height * 0.6)
paddle.fillColor = SKColor.blackColor()
self.addChild(paddle)
paddle.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 90, height: 35))
paddle.physicsBody.restitution = 0.1
paddle.physicsBody.friction = 0.4
paddle.physicsBody.dynamic = false
paddle.physicsBody.categoryBitMask = pCategory
paddle.physicsBody.collisionBitMask = bCategory
}
func setupBall() {
ball.name = bCategoryName
ball.position = CGPointMake(self.frame.size.width/2, paddle.position.y+paddle.frame.height+10)
ball.fillColor = SKColor.blackColor()
self.addChild(ball)
ball.physicsBody = SKPhysicsBody(circleOfRadius: 30)
ball.physicsBody.friction = 0.0
ball.physicsBody.restitution = 1.0
ball.physicsBody.linearDamping = 0.0
ball.physicsBody.angularDamping = 0.0
ball.physicsBody.allowsRotation = false
ball.physicsBody.dynamic = true
ball.physicsBody.categoryBitMask = bCategory
ball.physicsBody.collisionBitMask = borderCategory
}
The output for NSLog(String(self.frame.width) + ":" + String(self.frame.height), nil)
is 1024.0:768.0, ran on iPhone 5S, IOS8.
How do i solve this problem? I believe that self.frame is the problem, but i already changed my the controller view from viewDidLoad to viewWillLayoutSubview.
EDIT 1: I noticed that the ball is now bouncing after setting the boundary's restitution to 1.0. BUT, I realised the ball is bouncing in an unknown region. It is bouncing out of the screen, then return back. The weird thing is it can bounce once it hit the top of the screen, but it won't hit side of the screen
EDIT 2: I realised another problem, UIDevice.currentDevice().orientation.isPortrait returns false, but yet I'm running my game in portrait mode
EDIT 3: If I changed orientation to landscape, the ball can escape out of the screen from the top and bottom by a little then bounce back, the ball bounces off the side of the screen correctly. But now the paddle is below the screen.
Related
Can some body help me, I created a SpriteKit node called(player) and once I set its physics it fall off the screen:
let player = SKSpriteNode(imageNamed: "car")
player.position.x = -300
player.zPosition = 1
player.physicsBody = SKPhysicsBody(texture: player.texture!, size: player.size)
player.physicsBody.categoryBitMask = 1
addChild(player)
I already tried adding
affectedByGravity
but the same result for the player and the enemy.
If I got it, you want your car not to be effected by gravity.
If it is so you might want to setup the physics of your "world", like:
func setWorld() {
self.backgroundColor = UIColor.white
//This should do the trick!
physicsWorld.gravity = CGVector(dx:0, dy:0)
//This is useful if you want to create wall along the borders
let frame = CGRect(x: self.frame.minX, y: self.frame.minY, width: self.frame.width, height: self.frame.height)
//here you set the borders as walls
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
physicsBody?.friction = 0
physicsBody?.restitution = 1
physicsBody?.linearDamping = 0
}
and call this function in didMove
When using high velocity (linear or angular) in SpriteKit, sprites look blurry as if there are "ghosts" trailing the sprite. The sprite looks fine at slow speeds.
Below is a screenshot and GIF illustrating the blurriness/ghosting problem with high linear velocity, but the problem also occurs with the angularVelocity property.
Ball Code (use SKScene below to reproduce blurriness):
let radius = CGFloat(8)
let body = SKPhysicsBody(circleOfRadius: radius)
body.isDynamic = true
body.affectedByGravity = false
body.allowsRotation = true
body.friction = 0
body.restitution = 0.0
body.linearDamping = 0.0
body.angularDamping = 0
body.categoryBitMask = categoryBitMask
let ball = SKShapeNode(circleOfRadius: radius)
ball.physicsBody = body
ball.physicsBody?.velocity.dx = 0
ball.physicsBody?.velocity.dy = -1200
Looks fine:
ball.physicsBody?.velocity.dy = -200
Looks blurry:
ball.physicsBody?.velocity.dy = -1200
Screenshot:
GIF:
SKScene (drop in project and present scene to see blurriness):
import Foundation
import SpriteKit
class TestScene : SKScene, SKPhysicsContactDelegate {
let BallBitMask : UInt32 = 0x1 << 1
let BottomWallBitMask : UInt32 = 0x1 << 3
let TopWallBitMask : UInt32 = 0x1 << 4
let RightWallBitMask : UInt32 = 0x1 << 5
let LeftWallBitMask : UInt32 = 0x1 << 6
let SceneBackgroundColor = UIColor(red: 58/255.0, green: 50/255.0, blue: 96/255.0, alpha: 1.0)
let HorizontalWallHeight = CGFloat(10)
let VerticallWallWidth = CGFloat(5)
override init() {
super.init()
}
override init(size: CGSize) {
super.init(size: size)
doInit()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
fileprivate func doInit() {
// Set background
backgroundColor = SceneBackgroundColor
// Set scale mode
scaleMode = .resizeFill
// Set anchor point to screen center
anchorPoint = CGPoint(x: 0.5, y: 0.5)
// Add walls
layoutWalls()
// Create ball
let radius = CGFloat(8)
let body = SKPhysicsBody(circleOfRadius: radius)
body.isDynamic = true
body.affectedByGravity = false
body.allowsRotation = true
body.friction = 0
body.restitution = 0.0
body.linearDamping = 0.0
body.angularDamping = 0
body.categoryBitMask = BallBitMask
body.collisionBitMask = TopWallBitMask | RightWallBitMask | BottomWallBitMask | LeftWallBitMask
let ball = SKShapeNode(circleOfRadius: radius)
ball.fillColor = UIColor.orange
ball.physicsBody = body
ball.physicsBody?.velocity.dx = 0
ball.physicsBody?.velocity.dy = -1200
// Add ball to scene
addChild(ball)
}
fileprivate func layoutWalls() {
// Set wall offset
let wallOffset = CGFloat(3)
// Layout bottom wall
let bottomWallSize = CGSize(width: UIScreen.main.bounds.width, height: HorizontalWallHeight)
let bottomWall = SKSpriteNode(color: UIColor.red, size: bottomWallSize)
bottomWall.position.y = -UIScreen.main.bounds.height/2 - bottomWallSize.height/2 - wallOffset
bottomWall.physicsBody = createWallPhysics(categoryBitMask: BottomWallBitMask, wallSize: bottomWallSize)
addChild(bottomWall)
// Layout top wall
let topWallSize = CGSize(width: UIScreen.main.bounds.width, height: HorizontalWallHeight)
let topWall = SKSpriteNode(color: UIColor.red, size: topWallSize)
topWall.position.y = UIScreen.main.bounds.height/2 + topWallSize.height/2 + wallOffset
topWall.physicsBody = createWallPhysics(categoryBitMask: TopWallBitMask, wallSize: topWallSize)
addChild(topWall)
// Layout right wall
let rightWallSize = CGSize(width: VerticallWallWidth, height: UIScreen.main.bounds.height)
let rightWall = SKSpriteNode(color: UIColor.blue, size: rightWallSize)
rightWall.position.x = UIScreen.main.bounds.width/2 + rightWallSize.width/2 + wallOffset
rightWall.physicsBody = createWallPhysics(categoryBitMask: RightWallBitMask, wallSize: rightWallSize)
addChild(rightWall)
// Layout left wall
let leftWallSize = CGSize(width: VerticallWallWidth, height: UIScreen.main.bounds.height)
let leftWall = SKSpriteNode(color: UIColor.blue, size: leftWallSize)
leftWall.position.x = -UIScreen.main.bounds.width/2 - leftWallSize.width/2 - wallOffset
leftWall.physicsBody = createWallPhysics(categoryBitMask: LeftWallBitMask, wallSize: leftWallSize)
addChild(leftWall)
}
fileprivate func createWallPhysics(categoryBitMask: UInt32, wallSize: CGSize) -> SKPhysicsBody {
// Create new physics body for wall
let physicsBody = SKPhysicsBody(edgeLoopFrom: CGRect(x: -wallSize.width/2, y: -wallSize.height/2, width: wallSize.width, height: wallSize.height))
physicsBody.isDynamic = true
physicsBody.friction = 0
physicsBody.restitution = 1.0
physicsBody.linearDamping = 0
physicsBody.angularDamping = 0.0
physicsBody.categoryBitMask = categoryBitMask
// Return body
return physicsBody
}
}
Which one of these looks more ghosty?
The "trick" is being performed by the eye. We're not equipped to deal with screens at a lowly 60fps with fast moving objects. We sustain an image on the screen and in position through a faux persistence of vision so our brains and consciousness can figure out how fast something is "moving" on the screen.
In real life we get a near infinite number of "frames" to process movement with, and depth and all sorts of other cues, so we rarely do this anywhere near as much.
We still do it, but it's much less perceptible because we've got that near infinite number of frames to call on.
The below three images do different things to reveal this in different ways.
The first one is linear speed, accelerates instantly to its velocity of rotation and stops instantly.
The second has a ramp up and ramp down to its rotational speed, which has a higher peak speed of rotation. This has an interesting effect on the brain that permits it to prepare for the velocity that's going to be achieved.
The final has a lot of fake motion blur (too much for real world motion graphics usage) that shows how effective blur is at solving the effect of this problem, and why slow shutter speeds are so incredibly important to movie making.
Linear rotation rate:
Accel and decel:
Heavily blurred:
All this code in my project works fine, but I am having some problems. When the plane touches the border of the screen it starts to spin after contact. I can't figure out how to make it so it doesn't spin when it hits the border of the screen. It only has this problem when I use:
plane.physicsBody = SKPhysicsBody(texture: plane.texture!, size: plane.size)
Other forms of definition I have no problem, but I need it to be the size of the texture.
plane = SKSpriteNode(imageNamed: sett.store() as! String)
plane.size = CGSize(width: 80, height: 80)
plane.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 4)
// scorenode collison off, makes images spin on any contact
plane.physicsBody = SKPhysicsBody(texture: plane.texture!, size: plane.size)
plane.physicsBody?.categoryBitMask = Physics.Plane
plane.physicsBody?.collisionBitMask = Physics.Bird
plane.physicsBody?.contactTestBitMask = Physics.Bird | Physics.Score | Physics.Coin
plane.physicsBody?.affectedByGravity = true
plane.physicsBody?.dynamic = true
plane.zPosition = 2
self.addChild(plane)
// screen boarder so cant leave screen
let boredBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
boredBody.friction = 0
self.physicsBody = boredBody
My other problem is again with the the plane
plane.physicsBody = SKPhysicsBody(texture: plane.texture!, size: plane.size)
but as well I need this so it's the size of the texture. When it hits my score node on the screen it should only add 1 point but it varies 1-about 60 points when it crosses the scoreNode. How would i fix this?
let scoreNode = SKSpriteNode()
scoreNode.size = CGSize(width: 800, height: 1)
scoreNode.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 350)
scoreNode.physicsBody = SKPhysicsBody(rectangleOfSize: scoreNode.size)
scoreNode.physicsBody?.affectedByGravity = false
scoreNode.physicsBody?.dynamic = false
scoreNode.physicsBody?.categoryBitMask = Physics.Score
scoreNode.physicsBody?.collisionBitMask = 0
scoreNode.physicsBody?.contactTestBitMask = Physics.Plane
scoreNode.color = SKColor.whiteColor()
// when scoreNode and plane touch && initates score and highScore
if firstBody.categoryBitMask == Physics.Score && secondBody.categoryBitMask == Physics.Plane || firstBody.categoryBitMask == Physics.Plane && secondBody.categoryBitMask == Physics.Score {
score += 1
if score >= highScore{
highScore = score
scoreLabel.text = "SCORE: \(score)"
highScoreLabel.text = "HIGH SCORE: \(highScore)"
let highScoreDefault = NSUserDefaults.standardUserDefaults()
highScoreDefault.setValue(highScore, forKey: "highScore")
highScoreDefault.synchronize()
} else {
scoreLabel.text = "SCORE: \(score)"
highScoreLabel.text = "HIGH SCORE: \(highScore)"
}
}
It's spining because the collision has imparted angular velocity on the physics body. This is as it should be. If you really never want an object to have angular velocity you can do that by setting the node physics body
allowsRotation property to false.
in this case it would be something like:
scoreNode.physicsBody?.allowsRotation = false
https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/index.html#//apple_ref/occ/instp/SKPhysicsBody/allowsRotation
I got a problem with my simple game. Every picture as Player, background, floor, coins, Score Text etc are set as a "add child" in function didMoveToView. It looks like this:
override func didMoveToView(view: SKView) {
balon.physicsBody = SKPhysicsBody(circleOfRadius: balon.size.width/2)
balon.physicsBody?.dynamic = true
balon.physicsBody?.categoryBitMask = physics.Balon
balon.physicsBody?.contactTestBitMask = physics.Moneta
balon.physicsBody?.collisionBitMask = physics.None
balon.physicsBody?.usesPreciseCollisionDetection = true
physicsWorld.gravity = CGVector(dx: 0,dy: 0)
physicsWorld.contactDelegate = self
/////////////////// MOVE COINS ///////////////////////////////////////
runAction(SKAction.repeatActionForever(
SKAction.sequence([
SKAction.runBlock(addCoin),
SKAction.waitForDuration(1.0)
])
))
/////////////////// MOVE BOMB ///////////////////////////////////////
runAction(SKAction.repeatActionForever(
SKAction.sequence([
SKAction.runBlock(addBomb),
SKAction.waitForDuration(1.0)
])
))
self.backround.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.runningBar.anchorPoint = CGPointMake(0, 0.5)
self.runningBar.position = CGPointMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame))
self.orginalBarPosition = self.runningBar.position.x
self.MaxBarPos = self.runningBar.size.width - self.frame.size.width
self.MaxBarPos *= -1
self.balonBaseLine = self.runningBar.position.y + (self.runningBar.size.height/2) + (self.balon.size.height/2)
self.balon.position = CGPointMake(CGRectGetMidX(self.frame) - self.balon.size.width * 2, CGRectGetMidY(self.frame))
self.backround.anchorPoint = CGPointMake(0.5, 0.5)
self.originalBackgroundPosition = self.backround.position.x
self.maxBackgroundPosition = self.backround.size.width - self.frame.size.width * 1.11
self.maxBackgroundPosition *= -1
scoreLabel.text = "Score: 0"
scoreLabel.fontSize = 16
scoreLabel.fontColor = SKColor.blackColor();
scoreLabel.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2)
addChild(scoreLabel)
addChild(backround)
addChild(runningBar)
addChild(balon) }
There is a problem.. Sometimes everything works fine but usually when I'm running my program something will not shows up. For example background and coins shows up but the player (Balon) doesn't, or sometimes Coins and Bombs shows up but the floor(movingBar) doesn't.
RunningBar is moving, it's declared in override func update(currentTime: NSTimeInterval) just like moving background (it's a sky). I don't understand why sometimes everything is OK and mostly is not OK...
I have no errors while compiling my project.
I would really like to understand what's wrong with this.
Thanks for any help!
I've recently put the finishing touches to my first app however I've spotted a bug that is driving me crazy!
The object of the game is the character (a cat), jumps over obstacles but also has to avoid birds that fly through the air, the score increases every time a bird fly past without contact.
The game is over if contact is made when the cat makes contact with a bird or if the cat fails to jump over an obstacle and is pushed back to the left edge of the screen where a dummy contact node is placed.
Now the bug... everything works as expected however if the cat is in mid air when a bird makes contact the collision propels the cat off the visible screen, at this stage the player is then unable to reset the scene by tapping the screen, only if the cat is visible on the screen would you be able to reset the scene by tapping once and play again.
Here is all the code I could think of that may be useful, if anyone knows how this bug can be crushed please let me know.
override func didMoveToView(view: SKView) {
//details of the cat
cat = SKSpriteNode(texture: catTexture1)
cat.position = CGPoint(x: self.frame.size.width / 2.2, y: self.frame.size.height / 5.1 )
cat.runAction(run, withKey: "runningAction")
cat.physicsBody = SKPhysicsBody(circleOfRadius: cat.size.height / 2.0)
cat.physicsBody!.dynamic = true
cat.physicsBody!.allowsRotation = false
cat.physicsBody?.categoryBitMask = catCategory
cat.physicsBody?.collisionBitMask = crowCategory | worldCategory
cat.physicsBody?.contactTestBitMask = crowCategory | contact2Category
cat.physicsBody!.restitution = -10
moving.addChild(cat)
//contact node increases score when bird makes contact
var contact = SKSpriteNode(color: UIColor.clearColor(), size: CGSizeMake(1, 450))
contact.position = CGPoint(x: self.frame.size.width / 3.3, y: self.frame.size.height / 2.0 )
contact.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, 450))
contact.physicsBody!.dynamic = false
contact.physicsBody!.allowsRotation = false
contact.physicsBody?.categoryBitMask = scoreCategory
contact.physicsBody?.contactTestBitMask = crowCategory
self.addChild(contact)
//2nd contact node runs game over code
var contact2 = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(1, 450))
contact2.position = CGPoint(x: self.frame.size.width / 4.0, y: self.frame.size.height / 2.0 )
contact2.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, 450))
contact2.physicsBody!.dynamic = false
contact2.physicsBody!.allowsRotation = false
contact2.physicsBody?.categoryBitMask = contact2Category
contact2.physicsBody?.contactTestBitMask = catCategory
self.addChild(contact2)
//touch that initiates reset
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if ableToJump == true {
if (moving.speed > 0){
cat.physicsBody!.velocity = CGVectorMake(0, 0)
cat.physicsBody!.applyImpulse(CGVectorMake(0, 30))
} else if (canRestart) {
resetLabelNode.hidden = true
self.resetScene()
}
}
}
So I found the answer to this on a similar post, what they recommended was to set a frame round the visible screen. I just added the below code to didMoveToView
let frame = CGRectMake(255, 0, 515, self.frame.size.height)
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: frame)
I hope this helps someone in the future!