I want to override collisions in SpriteKit.
The idea is that I have a ball which bounces around the scene. When didBeginContact detects contact between an edge and the ball, I want the ball to rebound in a random direction and speed.
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let kEdgeCollisionCategory:UInt32 = 0x1 << 1
let kSquareCollisionCategory:UInt32 = 0x1 << 2
override func didMoveToView(view: SKView) {
/* Setup your scene here */
// Physics world
self.physicsWorld.gravity = CGVectorMake(0.0, 0.0)
self.physicsWorld.contactDelegate = self
// Edge
let frameEdges = SKNode()
frameEdges.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
frameEdges.physicsBody?.categoryBitMask = kEdgeCollisionCategory
self.addChild(frameEdges)
// Sprite
var sprite = SKSpriteNode(imageNamed: "blue")
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(sprite.size.width, sprite.size.height))
sprite.physicsBody?.restitution = 1.0
sprite.physicsBody?.linearDamping = 0.0
sprite.physicsBody?.angularDamping = 0.0
sprite.physicsBody?.friction = 0.0
sprite.physicsBody?.dynamic = true
sprite.physicsBody?.categoryBitMask = kSquareCollisionCategory
sprite.physicsBody?.collisionBitMask = kEdgeCollisionCategory
sprite.physicsBody?.contactTestBitMask = kEdgeCollisionCategory
self.addChild(sprite)
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody:SKPhysicsBody?
var second:SKPhysicsBody?
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
var randomX = CGFloat(arc4random_uniform(UInt32(4))))
var randomY = CGFloat(arc4random_uniform(UInt32(4))))
if firstBody!.categoryBitMask == kSquareCollisionCategory && secondBody!.categoryBitMask == kEdgeCollisionCategory {
firstBody!.velocity = (CGVectorMake(firstBody!.velocity.dx * randomX, firstBody!.velocity.dy * randomY))
}
}
}
Upon contact you can apply a force or impulse to the ball depending on the ball's current vector. For example, upon contact the ball's vector is (30,-20). This example vector translates to the ball moving right and and down. You can then apply a new vector making the ball move left and up (-20, 25). You can use arc4random to set new values or set static ones.
If you need, read up on using vectors with physics bodies.
Your problem is here ,
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
This means first body has categoryBitMask of kEdgeCollisionCategory and second body has categoryBitMask of kSquareCollisionCategory. So change this to,
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
else {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
To get random number use this,
int minSpeed = 1;
int maxSpeed = 20;
int randNum = rand() % (max-min) + min;
Then apply impulse over the required body.
Related
Here is my code. I am trying to create a space shooter game and when the alien hits the bottom of the screen, I want the player to disappear. I'm not exactly sure what is wrong with this code.
func gameOver() {
let borderBody = SKPhysicsBody(edgeLoopFrom: self.frame)
borderBody.categoryBitMask = borderCategory
func didBegin(_ contact: SKPhysicsContact) {
var firstBody:SKPhysicsBody
var secondBody:SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
}else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if (firstBody.categoryBitMask & alienCategory) != 0 && (secondBody.categoryBitMask & borderCategory) != 0 {
player.removeFromParent()
}
I have created a sprite with circle shape physics body. I want to change the circle shape into a rectangle shaped physics body on contact/collision with another body. I believe this should be done in the didBeginContact. Here's what I've done so far
ball.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2)
self.addChild(ball)
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.size.width/2)
ball.physicsBody?.friction = 0
ball.physicsBody?.restitution = 1.2
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.angularDamping = 0
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.applyImpulse(CGVectorMake(2, -4))
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody
var secondBody : SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.categoryBitMask == ballCategory && secondBody.categoryBitMask == enemyCategory {
//change ball physics shape here
}
}
Remember that a physics body is something that belongs to a node. It's simply a property, and its dependent on that node to exist. All you have to do is swap one body for another in your node's physicsBody property:
//inside of didBeginContact, say you want to change firstBody's body to rectangle
firstBody.node.physicsBody = SKPhysicsBody(rectangleOfSize:...)
With help from here I have made a circle body traverse a given path. I have some bodies at some of the path points and have logged contact in didBeginContact. When the body gets in contact with a specific body the circle body is changed to a rectangle. This rectangular body is suppose to traverse the same path as the original circle body but it doesn't reach the path points as the contact is not logged. I tried changing radiusPoint to the width or height of the rectangle also but that didn't work. Also the rectangle body is bigger than the circle body. How can I get the rectangle to traverse the points with the contact recognised? Please see code below.
Code related to path traversal:
let repeats: Bool = true //Whether to repeat the path.
var pathIndex = 0 //The index of the current point to travel.
var pointRadius: CGFloat = SKTexture(imageNamed: "circle").size().width //How close the node must be to reach the destination point.
let travelSpeed: CGFloat = 250 //Speed the node will travel at.
let rate: CGFloat = 0.9 //Motion smoothing. 0.5
circlePath = [
CGPoint(x:screenSize.width , y: screenSize.height/3),
CGPoint(x: screenSize.width/2, y: platform.sprite.frame.height),
CGPoint(x: 0.0, y: screenSize.height/3),
CGPoint(x: CGFloat(pos1) + screenSize.width/20, y: upperSpearPosHeight)]
final func didReachPoint() {
//reached point!
pathIndex++
if pathIndex >= ballPath.count && repeats {
pathIndex = 0
}
}
func updatePath() {
if pathIndex >= 0 && pathIndex < circlePath.count {
let destination = circlePath[pathIndex]
//currentPosition = destination
let displacement = CGVector(dx: destination.x-circle!.sprite.position.x, dy: destination.y-circle!.sprite.position.y)
let radius = sqrt(displacement.dx*displacement.dx+displacement.dy*displacement.dy)
let normal = CGVector(dx: displacement.dx/radius, dy: displacement.dy/radius)
let impulse = CGVector(dx: normal.dx*travelSpeed, dy: normal.dy*travelSpeed)
let relativeVelocity = CGVector(dx:impulse.dx-circle!.sprite.physicsBody!.velocity.dx, dy:impulse.dy-circle!.sprite.physicsBody!.velocity.dy);
circle!.sprite.physicsBody!.velocity=CGVectorMake(circle!.sprite.physicsBody!.velocity.dx+relativeVelocity.dx*rate, circle!.sprite.physicsBody!.velocity.dy+relativeVelocity.dy*rate);
if radius < pointRadius {
didReachPoint()
}
}
}
Contact code:
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody
var secondBody : SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.categoryBitMask == circleCategory && secondBody.categoryBitMask == bonusCategory {
let img = SKTexture(imageNamed: "rectangular")
(firstBody.node! as? SKSpriteNode)?.size = img.size()
firstBody.node!.physicsBody = SKPhysicsBody(texture: img, size: img.size())
firstBody.node!.physicsBody?.allowsRotation = false
changeCircleAction = SKAction.setTexture(img)
firstBody.node!.runAction(changeCircleAction)
}
if firstBody.categoryBitMask == circleCategory && secondBody.categoryBitMask == platformCategory {
print("touched platform")
}
if firstBody.categoryBitMask == circleCategory && secondBody.categoryBitMask == smallStarCategory {
removeStar(secondBody.node!)
}
It seems like when you change the firstBody to a rectangle (in the didBeginContact method), you don't set the bit mask. From what you described, it appears you want to set it to circleCategory as such:
firstBody.node!.physicsBody?.categoryBitMask = circleCategory
I would put this right below the firstBody.node!.physicsBody?.allowsRotation = false.
I have a dynamic yellow SpriteNode in motion with a categoryBitMask and a contactTestBitMask and it's intended to collide with the static red rectangle SpriteNodes with their own categoryBitMasks and contactTestBitMasks.
I would like to call a function only when both the rectangles have been hit by the yellow sprite? Does anyone know how to do that? I currently have the following code below that I use to check for the collusion with one rectangle.
func didBeginContact(contact: SKPhysicsContact) {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.categoryBitMask == kYellowCatergory && secondBody.categoryBitMask == kRectangleTarget {
//self.functionToCall()
}
}
So, I am still experimenting with Sprite Kit for my first time ever, and I would like to test for collision. So, I searched around a bit in Apple's documentation, around Stack Overflow, online tutorials, and other forums. However, I was unable to find something a tip or code that makes what I am doing work. So, here are the relevant pieces of code:
This is the code for an obstacle:
func createObstacle(){
var ball = SKShapeNode(circleOfRadius: 20)
var width = UInt32(self.frame.width)
var random_number = arc4random_uniform(width)
ball.position = CGPointMake(CGFloat(random_number), frame.height+20)
ball.strokeColor = SKColor.blackColor()
ball.glowWidth = 1.0
ball.fillColor = SKColor.darkGrayColor()
ball.physicsBody = SKPhysicsBody(circleOfRadius: 20)
ball.physicsBody!.affectedByGravity = true
ball.physicsBody?.categoryBitMask = 6
ball.physicsBody?.dynamic = true
self.addChild(ball)
}
This is relevant code for the thing that it would collide with:
let circle = SKShapeNode(circleOfRadius: 20)
circle.physicsBody = SKPhysicsBody(circleOfRadius: 20)
circle.fillColor = SKColor.blueColor()
circle.strokeColor = SKColor.blueColor()
circle.glowWidth = 1.0
circle.physicsBody?.categoryBitMask = 4
circle.physicsBody?.dynamic = true
circle.physicsBody?.affectedByGravity = false
And this is the code for contact:
func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody!
var secondBody: SKPhysicsBody!
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if ((firstBody.categoryBitMask == 4 && secondBody.categoryBitMask == 6) || (firstBody.categoryBitMask == 6 && secondBody.categoryBitMask == 4)){
println("HI")
}else{println("NO")}
}
Sadly, nothing is being printed at all, so something's wrong. Any idea why this doesn't work?
Your class should have delegate SKPhysicsContactDelegate.
class GameScene: SKScene, SKPhysicsContactDelegate {
In didMoveToView write this:
physicsWorld.contactDelegate = self
EDIT
Define CategoryBitMask like this
struct PhysicsCategory {
static let circleCategory : UInt32 = 0b1 // 1
static let ballCategory : UInt32 = 0b10 // 2
}
Give CategoryBitMask to circle and ball
circle.physicsBody?.categoryBitMask = PhysicsCategory.circleCategory
ball.physicsBody?.categoryBitMask = PhysicsCategory.ballCategory
Then check contact like this:
(func didBeginContact(contact: SKPhysicsContact) {
if ((contact.bodyA.categoryBitMask == 0b1 && contact.bodyB.categoryBitMask == 0b10 ) || ( contact.bodyA.categoryBitMask == 0b1 && contact.BodyB.categoryBitMask == 0b1 ))
println("Contact")
}
}
Sorry for typos didnt used editor