检测两个物体之间是否发生碰撞
Detect if collision occurred between 2 objects
我在 iOS 开始研究 Sprite Kit 游戏,我想知道如何检测球进入篮筐时是否发生碰撞。
为了让事情更容易理解。我在我的篮球架上有 2 个角(它们是红色的,很容易看到,它们很突出),它们有碰撞,如果球击中它们,它就会被扔掉。
我的问题:我想在球击中任何一个角或球进入篮筐但未击中任何两个角时引起注意。 (就像一种区分正常投掷和完美投掷的得分方式)
更新:
如何在角落设置标志?你能指出我需要使用的功能吗?
我有一个节点,如果球进了篮筐,它会通知我,它是篮筐中间的钢筋,当它碰撞时,它会增加我的分数。
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()
}
}
- 当球触及任何一个红色核者时设置一个标志。
- 在网络中间放置一个节点,设置为在它出现时通知
触球。
- 如果 'net node' 触发联系但 'red corner' 标志有
没有被设置,那么你有一个完美的投掷。
- 重置 'red corner' 标志。
这是我使用的一些应用和检测碰撞的代码,似乎非常有效
-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
}
}
一些解释
所以如果你看看我声明你的球有一个 physicsBody 的地方它说 categoryBitMask = BlahBlah 这意味着你告诉代码你的球有一个独特的整数来区分它用于碰撞检测
然后它说 collisionBitMask 这意味着你告诉 SpriteKit 你想让那个球接触什么
底部的函数是它可以创建的物理处理程序,这样你就可以准确地知道什么接触什么发生了什么,什么时候发生,并允许你输入调用接触时发生的事情(比如给个头-up)
我建议您阅读更多相关信息并研究有关 SKPhysicsBodies 的苹果文档
如果您还有其他问题,请告诉我,我很乐意为您提供更多帮助
就像史蒂夫·艾夫斯刚刚发布的那样,您想做四件事
制作某种形式的整数,如果触摸到红点,该整数会被添加
如果整数 > 0 那么它不是一个完美的抛出
如果整数 = 0 则完美抛出
添加分数后重置整数并重做
我在 iOS 开始研究 Sprite Kit 游戏,我想知道如何检测球进入篮筐时是否发生碰撞。
为了让事情更容易理解。我在我的篮球架上有 2 个角(它们是红色的,很容易看到,它们很突出),它们有碰撞,如果球击中它们,它就会被扔掉。
我的问题:我想在球击中任何一个角或球进入篮筐但未击中任何两个角时引起注意。 (就像一种区分正常投掷和完美投掷的得分方式)
更新:
如何在角落设置标志?你能指出我需要使用的功能吗?
我有一个节点,如果球进了篮筐,它会通知我,它是篮筐中间的钢筋,当它碰撞时,它会增加我的分数。
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()
}
}
- 当球触及任何一个红色核者时设置一个标志。
- 在网络中间放置一个节点,设置为在它出现时通知 触球。
- 如果 'net node' 触发联系但 'red corner' 标志有 没有被设置,那么你有一个完美的投掷。
- 重置 'red corner' 标志。
这是我使用的一些应用和检测碰撞的代码,似乎非常有效
-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
}
}
一些解释
所以如果你看看我声明你的球有一个 physicsBody 的地方它说 categoryBitMask = BlahBlah 这意味着你告诉代码你的球有一个独特的整数来区分它用于碰撞检测
然后它说 collisionBitMask 这意味着你告诉 SpriteKit 你想让那个球接触什么
底部的函数是它可以创建的物理处理程序,这样你就可以准确地知道什么接触什么发生了什么,什么时候发生,并允许你输入调用接触时发生的事情(比如给个头-up)
我建议您阅读更多相关信息并研究有关 SKPhysicsBodies 的苹果文档
如果您还有其他问题,请告诉我,我很乐意为您提供更多帮助
就像史蒂夫·艾夫斯刚刚发布的那样,您想做四件事
制作某种形式的整数,如果触摸到红点,该整数会被添加
如果整数 > 0 那么它不是一个完美的抛出
如果整数 = 0 则完美抛出
添加分数后重置整数并重做