0
votes

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) {

    }
}
1
You create a physicsbody for your whole circle with SKPhysicsBody(circleOfRadius: 298 ). This includes also your opening in the circle. You could use init(texture:alphaThreshold:size:) of the SKPhysicsbody class to replicate the shape of your circle + hole in a physicsbodyMarcel
thank you!! but i am not familiar when working with textures, learning more about it it now.Matt Young
just assign a texture to your spritenode instead of an image like this: circle = 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
dude you are the best. thank you so much! it worked.Matt Young
You're welcome, glad it worked for you. I posted an answer to formalize it, it whould be great if you could accept it.Marcel

1 Answers

0
votes

The problem here is that you assign a physicsbody for your whole circle with SKPhysicsBody(circleOfRadius: 298 ). This includes also your hole in the circle, so that nothing can pass through. You should use the texture initialiser of the SKPhysicsbody class to replicate the shape of your circle and let the hole untouched. The following will work for you:

circle = SKSpriteNode(texture: SKTexture(imageNamed: "Circle1"))
circle.physicsbody = SKPhysicsbody(texture: circle.texture!, size: CGSize(width: circle.frame.width, height: circle.frame.height))