gpt4 book ai didi

xcode - 物理物体与另一个物理物体碰撞

转载 作者:行者123 更新时间:2023-11-30 10:08:46 25 4
gpt4 key购买 nike

好的,我有一个非常具体的问题,希望可以帮助您。我将附上一张图片来澄清我在说什么。

clarification

redball.physicalsBodyboss.physicalsBody 上方生成 - 掉落(穿过)boss。与 whitebar.physicalsBody 碰撞 - 弹回并再次与 boss.physicalsBody 碰撞。这次,我想触发一个事件,该事件会注意到球何时弹回并击中 Boss。

目前。 Boss和Ball共享相同的collisionBitMask,这样它们就可以互相穿过。当球与杆碰撞时,我尝试将 ball.physicalsBody?.contactTestBitMask =PhysicsCategory.boss.rawValue 添加到球中。这样我就可以注意到球和老板之间返回途中的碰撞。然而这似乎不起作用。

有人能解决这个奇怪的问题吗?非常感谢您的帮助。

旁注:如果有人能想出一个聪明的标题,请告诉我,我会编辑它!

编辑:添加代码

enum PhysicsCategory : UInt32 {
case bar = 1
case ball = 2
case boss = 4
case noCollision = 8
}

class GameScene: SKScene, SKPhysicsContactDelegate {

var score = Int()
var background = SKSpriteNode(imageNamed: "background.png")
var bar = SKSpriteNode(imageNamed: "bar.png")
var boss1 = SKSpriteNode(imageNamed: "boss1.png")

override func didMoveToView(view: SKView) {
self.scene?.size = CGSize(width: 640, height:1136)
physicsWorld.contactDelegate = self

background.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
background.zPosition = -20
self.addChild(background)

bar.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 8)
bar.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: bar.size.width, height: bar.size.height))
bar.physicsBody?.categoryBitMask = PhysicsCategory.bar.rawValue
bar.physicsBody?.contactTestBitMask = PhysicsCategory.ball.rawValue
bar.physicsBody?.collisionBitMask = PhysicsCategory.bar.rawValue
bar.physicsBody?.affectedByGravity = false
bar.physicsBody?.dynamic = false
self.addChild(bar)

boss1.position = CGPoint(x: self.frame.width, y: self.frame.height - boss1.size.height / 2)
boss1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: boss1.size.width, height: boss1.size.height))
boss1.physicsBody?.categoryBitMask = PhysicsCategory.boss.rawValue
boss1.physicsBody?.contactTestBitMask = PhysicsCategory.ball.rawValue
boss1.physicsBody?.collisionBitMask = PhysicsCategory.noCollision.rawValue
boss1.physicsBody?.affectedByGravity = false
boss1.physicsBody?.dynamic = false
boss1.zPosition = 5
self.addChild(boss1)

boss1.runAction(SKAction.moveToX(self.frame.width / 2, duration: 1))

let spawnBallsAction = SKAction.sequence([SKAction.waitForDuration(2), SKAction.runBlock(spawnBalls)])
self.runAction(SKAction.repeatActionForever(spawnBallsAction))

}

func didBeginContact(contact: SKPhysicsContact) {
let firstBody : SKPhysicsBody = contact.bodyA
let secondBody : SKPhysicsBody = contact.bodyB

if (firstBody.categoryBitMask & PhysicsCategory.bar.rawValue == PhysicsCategory.bar.rawValue &&
secondBody.categoryBitMask & PhysicsCategory.ball.rawValue == PhysicsCategory.ball.rawValue) {

CollisionWithBar(firstBody.node as! SKSpriteNode, ball: secondBody.node as! SKSpriteNode)

}

if (firstBody.categoryBitMask & PhysicsCategory.ball.rawValue == PhysicsCategory.ball.rawValue &&
secondBody.categoryBitMask & PhysicsCategory.boss.rawValue == PhysicsCategory.boss.rawValue) {

CollisionWithBoss(firstBody.node as! SKSpriteNode, boss: secondBody.node as! SKSpriteNode)

}

}

func CollisionWithBar(bar: SKSpriteNode, ball: SKSpriteNode) {
ball.physicsBody?.applyImpulse(CGVectorMake(0, 500))
ball.physicsBody?.contactTestBitMask = PhysicsCategory.boss.rawValue //Trying to solve the problem, aint working
}

func CollisionWithBoss(ball: SKSpriteNode, boss: SKSpriteNode) {
NSLog("Ball hit the boss")
}


func spawnBalls(){

let ball = SKSpriteNode(imageNamed: "ball.png")
let MinValue = self.frame.width / 8
let MaxValue = self.frame.width - 20
let SpawnPoint = UInt32(MaxValue - MinValue)
ball.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.frame.height - 128)
ball.zPosition = 50

//Physics
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.height / 2)
ball.physicsBody?.categoryBitMask = PhysicsCategory.ball.rawValue
ball.physicsBody?.contactTestBitMask = PhysicsCategory.bar.rawValue
ball.physicsBody?.collisionBitMask = PhysicsCategory.noCollision.rawValue
ball.physicsBody?.affectedByGravity = true
ball.physicsBody?.dynamic = true

self.addChild(ball)
}

最佳答案

这就是联系方式:

创建球后,将联系人类别设置为:(您拥有的)

ball.physicsBody?.contactTestBitMask = PhysicsCategory.bar.rawValue

确保老板没有联系任何人:

boss1.physicsBody?.contactTestBitMask = PhysicsCategory.noCollision.rawValue

然后在“联系”上,您想要执行以下操作:

func didBeginContact(contact: SKPhysicsContact) {
let firstBody : SKPhysicsBody = (contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? contact.bodyA : contact.bodyB
let secondBody : SKPhysicsBody = (contact.bodyA.categoryBitMask >= contact.bodyB.categoryBitMask) ? contact.bodyA : contact.bodyB

if (firstBody.categoryBitMask & PhysicsCategory.bar.rawValue == PhysicsCategory.bar.rawValue &&
secondBody.categoryBitMask & PhysicsCategory.ball.rawValue == PhysicsCategory.ball.rawValue) {
//This should be inside CollisionWithBar
ball.physicsBody?.contactTestBitMask = PhysicsCategory.bar.rawValue | PhysicsCategory.boss.rawValue

//the ball has hit the bar, so lets enable hitting on the boss
//normally we would only want to do this once, but since this is
//tiny, it would be more time to wrap it in ifs and putting guards on it
boss1.physicsBody?.contactTestBitMask = PhysicsCategory.ball.rawValue
CollisionWithBar(firstBody.node as! SKSpriteNode, ball: secondBody.node as! SKSpriteNode)

}

if (firstBody.categoryBitMask & PhysicsCategory.ball.rawValue == PhysicsCategory.ball.rawValue &&
secondBody.categoryBitMask & PhysicsCategory.boss.rawValue == PhysicsCategory.boss.rawValue) {
//This should be inside CollisionWithBoss
ball.physicsBody?.contactTestBitMask = PhysicsCategory.bar.rawValue

CollisionWithBoss(firstBody.node as! SKSpriteNode, boss: secondBody.node as! SKSpriteNode)

}

}

这基本上是说,如果球击中横杆,则启用 Boss 击球,如果球击中 Boss,则禁用 Boss 击球

编辑:在意识到拥有多个球后,我们必须改变一些事情

确保 Boss 在创建时再次接触球:

boss1.physicsBody?.contactTestBitMask = PhysicsCategory.ball.rawValue

现在我们需要创建一个新类别

enum PhysicsCategory : UInt32 {
case bar = 1
case ball = 2
case boss = 4
case noCollision = 8
case initialBall = 16
}

首先,将球的类别分配给此:

ball.physicsBody?.categoryBitMask = initialBall

然后你的 CollissionWithBar 将如下所示:

func CollisionWithBar(bar: SKSpriteNode, ball: SKSpriteNode) {
ball.physicsBody?.applyImpulse(CGVectorMake(0, 500))
ball.physicsBody?.contactTestBitMask = PhysicsCategory.bar.rawValue | PhysicsCategory.boss.rawValue


ball.physicsBody?.categoryBitMask = PhysicsCategory.ball.rawValue
}

你的 CollissionWithBolls 将如下所示:

func CollisionWithBoss(ball: SKSpriteNode, boss: SKSpriteNode) {
ball.physicsBody?.contactTestBitMask = PhysicsCategory.bar.rawValue
}

最后,在“联系”上,您需要执行以下操作:

func didBeginContact(contact: SKPhysicsContact) {
let firstBody : SKPhysicsBody = (contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? contact.bodyA : contact.bodyB
let secondBody : SKPhysicsBody = (contact.bodyA.categoryBitMask >= contact.bodyB.categoryBitMask) ? contact.bodyA : contact.bodyB

if (firstBody.categoryBitMask & PhysicsCategory.bar.rawValue == PhysicsCategory.bar.rawValue &&
secondBody.categoryBitMask & PhysicsCategory.ball.rawValue == PhysicsCategory.ball.rawValue) {
CollisionWithBar(firstBody.node as! SKSpriteNode, ball: secondBody.node as! SKSpriteNode)

}

if (firstBody.categoryBitMask & PhysicsCategory.ball.rawValue == PhysicsCategory.ball.rawValue &&
secondBody.categoryBitMask & PhysicsCategory.boss.rawValue == PhysicsCategory.boss.rawValue) {


CollisionWithBoss(firstBody.node as! SKSpriteNode, boss: secondBody.node as! SKSpriteNode)

}

}

关于xcode - 物理物体与另一个物理物体碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34401249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com