I am making a game where a ball(guy) has to pass through an opening in a rotating circle(circle) and hit a target. I set up the collision detection to the rotating circle and then I try to make the ball(guy) pass through it does not pass through the opening. I know it has something to do with the collision detection. Also I have circleOfRadius set to the radius, but I can't seem to make an opening for the ball to pass through. If you can help I would love to hear from you. Here is my code, Its not all of it, its just whats important.
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var circle = SKSpriteNode()
var guy = SKSpriteNode()
let guyCategory :UInt32 = 0x1 << 0
let circleCategory :UInt32 = 0x1 << 1
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
scene?.anchorPoint = CGPoint(x: 0, y: 0)
backgroundColor = UIColor.lightGray
circle = SKSpriteNode(imageNamed: "Circle1")
circle.position = CGPoint(x: self.frame.size.width / 2, y:
self.frame.size.height / 2)
circle.physicsBody = SKPhysicsBody(circleOfRadius: 298 )
circle.physicsBody?.isDynamic = false
circle.physicsBody?.allowsRotation = true
circle.physicsBody?.affectedByGravity = false
circle.run(SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat.pi * 2.0, duration: 7.0)), withKey: "rotatecircle")
self.addChild(circle)
guy = SKSpriteNode(imageNamed: "Guy")
guy.position = CGPoint(x: self.frame.size.width/2 , y:40)
guy.name = "guy"
guy.physicsBody = SKPhysicsBody(circleOfRadius: guy.size.width/2)
guy.physicsBody?.isDynamic = false
guy.physicsBody?.affectedByGravity = false
self.addChild(guy)
circle.physicsBody?.categoryBitMask = circleCategory
circle.physicsBody?.collisionBitMask = targetCategory
circle.physicsBody?.contactTestBitMask = targetCategory
guy.physicsBody?.categoryBitMask = guyCategory
guy.physicsBody?.collisionBitMask = targetCategory
guy.physicsBody?.contactTestBitMask = targetCategory
}
func didBegin(_ contact: SKPhysicsContact) {
let collision1:UInt32 = contact.bodyA.categoryBitMask
if collision1 == targetCategory {
backgroundColor = UIColor.green
}
else {
backgroundColor = UIColor.red
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.physicsWorld.gravity = CGVector(dx: 0 ,dy: 10);
guy.physicsBody = SKPhysicsBody(circleOfRadius: guy.size.width / 2)
}
override func update(_ currentTime: TimeInterval) {
}
}
SKPhysicsBody(circleOfRadius: 298 )
. This includes also your opening in the circle. You could useinit(texture:alphaThreshold:size:)
of the SKPhysicsbody class to replicate the shape of your circle + hole in a physicsbody – Marcelcircle = SKSpriteNode(texture: SKTexture(imageNamed: "Circle1"))
Then you can access the texture property and create a physicsbody like this:circle.physicsbody = SKPhysicsbody(texture: circle.texture!, size: CGSize(width: circle.frame.width, height: circle.frame.height))
– Marcel