简体   繁体   中英

Detect if collision occurred between 2 objects

I started studying the Sprite Kit Game in iOS and I am want to find out how can I detect if a collision occurred when the ball entered in the hoop. 在此处输入图片说明

To make things easier to understand. My at my basketball hoop I have 2 corners (they are red and easy to see, they stand out), they have collision attached to them and if ball hits them, it gets trowed.

My question: I want to get noticed when the ball hits any of the corners or if the ball entered the hoop without hitting any of the 2 corners. (like a way to score differentiate normal throws from perfect throws)

UPDATED:

How do I set a flag to the corners? Can you point to the functions I need to use?

I have a node which notifies me if the ball entered the hoop, it's the steel bar in the middle of the hoop, when it collides, it adds my score.

func didBegin(_ contact: SKPhysicsContact)
    {
        // check for the ball contacting the scoreZone (scoreZone is the steel bar)
        guard let ballBody = ball.physicsBody, let scoreBody = score_zone.physicsBody else {
            return
        }
        // it doesn't matter who touches who, so just use array "contains" to handle both cases
        let bodies = [contact.bodyA, contact.bodyB]
        if bodies.contains(ballBody) && bodies.contains(scoreBody) && should_detect_score {
            // add score
            add_score()
        }
    }
  1. Set a flag when the ball touches either one of the red corers.
  2. Place a node in the middle of the net which is set to notify when it touches the ball.
  3. If the 'net node' triggers a contact yet the 'red corner' flag has not been set, then you have a perfect throw.
  4. Reset the 'red corner' flag.

here is some code for applying and detecting collision its what I use and seems to be very effective

-SWIFT 3-

enum bodyType: UIInt32 {
    case redDot1 = 1
    case redDot2 = 2
    case ball = 3
}
//do this to properly set up the physicsBodies of your detection otherwise the detection will not work at all
yourBall.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(x: ball.size.width, y: ball.size.height)
yourBall.physicsBody?.categoryBitMask = bodyType.ball.rawValue
yourBall.physicsBody?.collisionBitMask = bodyType.redDot1.rawValue | bodyType.redDot2.rawValue 

//repeat these steps for redDot1 and redDot2 and then put this in someplace (doesn't matter where)


func didBegin(_ contact: SKPhysicsContact) {
    if contact.bodyA.categoryBitMask = bodyType.ball.rawValue && contact.bodyB.categoryBitMask = bodyType.redDot1.rawValue || contact.bodyB.categoryBitMask = bodyType.ball.rawValue && contact.bodyA.categoryBitMask = bodyType.redDot1.rawValue {
    //in here is where you will put the notification
    }
if contact.bodyA.categoryBitMask = bodyType.ball.rawValue && contact.bodyB.categoryBitMask = bodyType.redDot2.rawValue || contact.bodyB.categoryBitMask = bodyType.ball.rawValue && contact.bodyA.categoryBitMask = bodyType.redDot2.rawValue {
    //in here is where you will put the notification
    }
}

some explanations

so if you look where I declared that your ball had a physicsBody it says categoryBitMask = BlahBlah that means your telling the code that your ball has a unique Integer that distinguishes it for collision detection

and then it says collisionBitMask that means your telling SpriteKit exactly what you want that ball to contact with

and at the bottom the function is the physics handler it can make it so you can tell exactly what contacts what and when it happens and allows you to put in things that happen when the contact is called (like give a heads-up)

I recommend you read more about it and study apples documentation on SKPhysicsBodies

if you have any more issues please let me know as I would love to help you more with this stuff

and like Steve ives just posted you want to do four things

  1. make some form of integer that gets added if the red dots are touched

  2. if the integer > 0 then its not a perfect throw

  3. if the integer = 0 its a perfect throw

  4. when the score is added reset the integer and do it all again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM