I have tried creating bitmask categories, setting all restitution values to 1 and everything else under the sun including running the application an a device and it still is not working at all. After setting an SKSpriteNode with retitution 1 to hit another skspritenode also with restitution one at dx: 100, it still doesn't bounce! Here is my code for GameScene.swift:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
let Ball = SKSpriteNode()
Ball.self.childNode(withName: "Ball")
let playerPaddle = SKSpriteNode()
playerPaddle.self.childNode(withName: "playerPaddle")
let computerPaddle = SKSpriteNode()
computerPaddle.self.childNode(withName: "computerPaddle")
let ballCategory: UInt32 = 0x1 << 1
let paddleCategory: UInt32 = 0x1 << 2
let borderCategory: UInt32 = 0x1 << 3
Ball.physicsBody?.categoryBitMask = ballCategory
playerPaddle.physicsBody?.categoryBitMask = paddleCategory
computerPaddle.physicsBody?.categoryBitMask = paddleCategory
self.physicsBody?.categoryBitMask = borderCategory
let playerScore = SKLabelNode()
playerScore.fontName = "Pong Score"
playerScore.text = "0"
playerScore.fontSize = 100
playerScore.color = SKColor.white
playerScore.position = CGPoint(x: -200, y: 220)
playerScore.zPosition = 3
addChild(playerScore)
let comScore = SKLabelNode()
comScore.fontName = "Pong Score"
comScore.text = "0"
comScore.fontSize = 100
comScore.color = SKColor.white
comScore.position = CGPoint(x: 200, y: 220)
comScore.zPosition = 3
addChild(comScore)
var comScoreInt: Int = Int(comScore.text!)!
comScoreInt += 1
comScore.text = String(comScoreInt)
var playerScoreInt: Int = Int(playerScore.text!)!
playerScoreInt += 1
playerScore.text = String(playerScoreInt)
let bodyBorder = SKPhysicsBody(edgeLoopFrom: self.frame)
bodyBorder.friction = 0
bodyBorder.restitution = 1
self.physicsBody = bodyBorder
}
}
PS most all of the physics is in GameScene.sks not GameScene.swift, however this is where the bitmasks are.
EDIT:
Ball Config;
Position x:0, y:0, z:0,
Rotation: 0,
Size: w:25, h:25,
Anchor Point: x:0.5, y:0.5,
Color: white,
Blend Factor: 0,
Blend Mode: alpha,
Alpha: 1,
Ik Constraints: min:0, max:360,
Scale: x:1, y:1,
PHYSICS
Body Type: bounding rect,
only dynamic checked,
Friction: 0,
Restitution: 1,
Linuar Damp:0,
Angular damp: 0,
Category, Collision, and Field mask: 4294967295,
Contact Mask: 0
Inital Velocity: dx:100 dy:0
Paddles are the same physics wise
PLEASE HELP!
Insead of applying an Inital Velocity in GameScene.sks I instead applied a force in GameScene.swift like so: Ball.physicsBody?.applyImpulse(CGVector(dx:50, dy:0)) restitution began working
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:
I have created a simple 2D arcade game using SpriteKit and I am trying to add a scoring system.Basically the game is a square sprite which has to jump over various obstacles.
What I have tried so far is creating an SKSpriteNode() which adds +1 to the score counter label when it contacts the player's physicsBody. My problem is that when they contact each other the player sprite floats over the gap's PhysicsBody. I have set it's collisionBitMask to 0 and have made it isDynamic = false. Can anybody help?
Okay so here's my gap's code
var gap = SKNode()
gap.position = CGPoint(x: obstacle.position.x, y: platform.position.y + 210)
gap.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 30, height: 70))
gap.physicsBody!.isDynamic = true
gap.physicsBody!.affectedByGravity = false
gap.physicsBody?.contactTestBitMask = PhysicsCategory.Player
gap.physicsBody?.categoryBitMask = PhysicsCategory.Gap
gap.physicsBody?.collisionBitMask = PhysicsCategory.None
gap.physicsBody?.restitution = 0
gap.physicsBody?.friction = 0
self.addChild(gap)
And here's my players code
let playerTexture = SKTexture(imageNamed: "white.jpg")
player = SKSpriteNode(texture: playerTexture)
player.size = CGSize(width: 40, height: 40)
player.physicsBody = SKPhysicsBody(rectangleOf: player.size)
player.physicsBody!.isDynamic = false
player.position = CGPoint(x: self.frame.midX - 100 , y: self.frame.midY + 50 )
player.physicsBody!.restitution = 0
player.physicsBody!.friction = 0
player.physicsBody?.categoryBitMask = PhysicsCategory.Player
player.physicsBody?.contactTestBitMask = PhysicsCategory.Obstacles | PhysicsCategory.Gap
player.physicsBody?.collisionBitMask = PhysicsCategory.Obstacles | PhysicsCategory.Floor
self.addChild(player)
Physics Category is
struct PhysicsCategory {
static let None : UInt32 = 0
static let Obstacles : UInt32 = 0b1 // 1
static let Player : UInt32 = 0010
static let Floor : UInt32 = 0100
static let Gap : UInt32 = 1000
}
I currently have two SKSpriteNodes that I have added SKPhysicsBodies to. When they have no SKJoint attached, they collide as expected. As soon as I add the SKPhysicsJoint, they just pass right through each other. Any joint I add functions properly, but the SKPhysicsJointLimit only limits the extent to which the nodes can travel apart from each other, not how close they can get. How can I fix this?
Here is code I am using for the joint:
let joint = SKPhysicsJointLimit.joint(withBodyA: object1.physicsBody!, bodyB: object2.physicsBody!, anchorA: CGPoint(x: object1.position.x + iconController.position.x, y: object1.position.y + iconController.position.y), anchorB: CGPoint(x: object2.position.x + iconController.position.x, y: object2.position.y + iconController.position.y))
joint.maxLength = screen.height * 0.4
physicsWorld.add(joint)
PhysicsBody of both nodes:
self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width / 2)
self.physicsBody?.allowsRotation = false
self.physicsBody?.friction = 0
self.physicsBody?.mass = 0.1
I have tested it with different values for the above modifications of the SKPhysicsBody and it performs the same.
An SKPhysicsJoint object connects two physics bodies so that they are simulated together by the physics world.
You can use also SKPhysicJointPin:
A pin joint allows the two bodies to independently rotate around the
anchor point as if pinned together.
If your objects work well before the SKPhysicsJoint addition with the physic engine so they fired the didBeginContact as you wish and as you have setted, I think your problem is simply a wrong anchor. Try to add:
let skView = self.view as! SKView
skView.showsPhysics = true
to your scene initialization code: you will see an outline of the physic bodies and maybe you'll see the issue immediatly.
To help you I'll try to make an example of elements configured to collide each other:
enum CollisionTypes: UInt32 {
case Boundaries = 1
case Element = 2
}
class GameScene: SKScene,SKPhysicsContactDelegate {
private var elements = [SKNode]()
override func didMoveToView(view: SKView) {
physicsWorld.gravity = CGVector(dx: 0, dy: 0)
self.physicsWorld.contactDelegate = self
let boundariesFrame = CGRectMake(20, 20, 200, 400)
let boundaries = SKShapeNode.init(rect: boundariesFrame)
boundaries.position = CGPointMake(350,150)
let boundariesBody = SKPhysicsBody.init(edgeLoopFromRect: boundariesFrame)
boundariesBody.dynamic = false
boundariesBody.categoryBitMask = CollisionTypes.Boundaries.rawValue
boundariesBody.contactTestBitMask = CollisionTypes.Element.rawValue
boundaries.physicsBody = boundariesBody
addChild(boundaries)
for index in 0..<5 {
let element = SKShapeNode(circleOfRadius: 10)
let body = SKPhysicsBody(circleOfRadius: 10)
body.linearDamping = 0
// body.mass = 0
body.dynamic = true
body.categoryBitMask = CollisionTypes.Element.rawValue
body.contactTestBitMask = CollisionTypes.Boundaries.rawValue | CollisionTypes.Element.rawValue
body.collisionBitMask = CollisionTypes.Boundaries.rawValue | CollisionTypes.Element.rawValue
element.physicsBody = body
element.position = CGPoint(x: size.width / 2, y: size.height / 2 - 30 * CGFloat(index))
elements.append(element)
addChild(element)
}
}
}
Hope it can help you to find your issue.
I am searching for the most efficient way to Nodes with random SKSpriteNodes on given positions.
I have 10 Platforms, on each of them I want to have an Enemy (SKNode) that contains for 3 different SKSpriteNodes which will randomly spawn. These SKSpriteNodes all do other behaviors, thats why I can't simply change the Texture of the SKNode.
As with my Code, I wanted to spawn the same Node on different Platforms.
However, the code does not work properly, the nodes do spawn but only at 1 position(the position from the last SpawnEnemycode in didMoveToView), meaning 10 nodes at 1 position.
I also tried it with adding 10 different Enemy nodes, each given a Platform. But this just looks like a copy & paste code thats inefficient.
How can I fix my problem, or is there either another way to spawn them more efficiently?
My Code:
class GameScene: SKScene, SKPhysicsContactDelegate {
var Enemy: SKNode!
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsWorld.contactDelegate = self
self.physicsBody?.velocity = CGVectorMake(0, 0)
self.anchorPoint = CGPoint(x: 0, y: 0.20)
backgroundColor = SKColor(red: 81.0/255.0, green: 192.0/255.0, blue: 201.0/255.0, alpha: 1.0)
self.Enemy1 = SKNode()
addChild(Enemy1)
SpawnPlayer()
SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform1.position.y + 15))
SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform2.position.y + 15))
//Same code for all the other Platforms.
}
func SpawnEnemy(position: CGPoint!){
switch (arc4random_uniform(3)){
case 0:
Picture1.filteringMode = .Nearest
Picture2.filteringMode = .Nearest
Animation1 = SKAction.animateWithTextures([Picture1,Picture2], timePerFrame: 0.5)
AnimationRepeat1 = SKAction.repeatActionForever(Animation1)
Sprite1 = SKSpriteNode (imageNamed: "Sprite1.png")
Sprite1.size = CGSize(width: 64, height: 64)
Sprite1.zPosition = 2
Sprite1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 45, height: 50))
Sprite1.physicsBody?.dynamic = true
Sprite1.physicsBody?.allowsRotation = false
Sprite1.physicsBody?.restitution = 0.0
Sprite1.physicsBody?.usesPreciseCollisionDetection = true
Sprite1.physicsBody?.categoryBitMask = EnemyCategory
Sprite1.physicsBody?.collisionBitMask = PlayerCategory | Platform1Category
Sprite1.physicsBody?.contactTestBitMask = Wall1Category | Wall2Category | PlayerCategory
Sprite1.runAction(AnimationRepeat1)
Enemy.position = position
Enemy.addChild(Sprite1)
case 1:
Picture3.filteringMode = .Nearest
Picture4.filteringMode = .Nearest
Animation1 = SKAction.animateWithTextures([Picture3,Picture4], timePerFrame: 0.5)
AnimationRepeat1 = SKAction.repeatActionForever(Animation1)
Sprite2 = SKSpriteNode (imageNamed: "Sprite2.png")
Sprite2.size = CGSize(width: 64, height: 64)
Sprite2.zPosition = 2
Sprite2.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 45, height: 50))
Sprite2.physicsBody?.dynamic = true
Sprite2.physicsBody?.allowsRotation = false
Sprite2.physicsBody?.restitution = 0.0
Sprite2.physicsBody?.usesPreciseCollisionDetection = true
Sprite2.physicsBody?.categoryBitMask = EnemyCategory
Sprite2.physicsBody?.collisionBitMask = PlayerCategory | Platform1Category
Sprite2.physicsBody?.contactTestBitMask = Wall1Category | Wall2Category | PlayerCategory
Sprite2.runAction(AnimationRepeat1)
Enemy.position = position
Enemy.addChild(Sprite2)
case 2:
Picture5.filteringMode = .Nearest
Picture6.filteringMode = .Nearest
Animation1 = SKAction.animateWithTextures([Picture5,Picture6], timePerFrame: 0.5)
AnimationRepeat1 = SKAction.repeatActionForever(Animation1)
Sprite3 = SKSpriteNode (imageNamed: "Sprite3.png")
Sprite3.size = CGSize(width: 64, height: 64)
Sprite3.zPosition = 2
Sprite3.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 45, height: 50))
Sprite3.physicsBody?.dynamic = true
Sprite3.physicsBody?.allowsRotation = false
Sprite3.physicsBody?.restitution = 0.0
Sprite3.physicsBody?.usesPreciseCollisionDetection = true
Sprite3.physicsBody?.categoryBitMask = EnemyCategory
Sprite3.physicsBody?.collisionBitMask = PlayerCategory | Platform1Category
Sprite3.physicsBody?.contactTestBitMask = Wall1Category | Wall2Category | PlayerCategory
Sprite3.runAction(AnimationRepeat1)
Enemy.position = position
Enemy.addChild(Sprite3)
default:
return
}
}
The reason you're only seeing one position is that you're only creating one "enemy" and moving it around with SpawnEnemy. Since your sprites are all added to the same "enemy", they will all show up wherever your enemy was moved to last.
Let's chop down what your code is to the relevant bits:
var Enemy: SKNode!
override func didMoveToView(view: SKView) {
self.Enemy1 = SKNode()
addChild(Enemy1)
SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform1.position.y + 15))
SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform2.position.y + 15))
}
func SpawnEnemy(position: CGPoint!){
switch (arc4random_uniform(3)){
case 0:
Enemy.position = position
Enemy.addChild(Sprite1)
case 1:
Enemy.position = position
Enemy.addChild(Sprite2)
case 2:
Enemy.position = position
Enemy.addChild(Sprite3)
default:
return
}
}
So, in didMoveToView(), you create one Enemy node. Then, as you call SpawnEnemy, you do a bunch of stuff to add other sprites, but as far as Enemy is concerned, you're just changing its position. Since you're adding the sprites to the same node, when you move that Enemy node, the child sprites move with it, too.
You have two options to fix this. Either add your sprites to some higher-level node (like the scene), or create new "enemy" nodes for each of the sprites you intend to add. Either should work, but the best choice will depend on what you're trying to accomplish with them - the scope of that discussion is beyond what you're talking about here.