gpt4 book ai didi

swift - body 从圆形变为矩形时无法识别联系人

转载 作者:行者123 更新时间:2023-11-28 08:56:41 24 4
gpt4 key购买 nike

here 的帮助下我做了一个圆体穿过给定的路径。我在一些路径点有一些 body ,并在 didBeginContact 中记录了联系方式。当 body 与特定 body 接触时,圆形 body 变为矩形。这个矩形体应该遍历与原始圆形体相同的路径,但由于没有记录接触,它没有到达路径点。我也尝试将 radiusPoint 更改为矩形的宽度或高度,但这没有用。矩形体也比圆形体大。如何让矩形遍历识别接触点的点?请看下面的代码。

路径遍历相关代码:

    let repeats: Bool = true //Whether to repeat the path.
var pathIndex = 0 //The index of the current point to travel.
var pointRadius: CGFloat = SKTexture(imageNamed: "circle").size().width //How close the node must be to reach the destination point.
let travelSpeed: CGFloat = 250 //Speed the node will travel at.
let rate: CGFloat = 0.9 //Motion smoothing. 0.5

circlePath = [
CGPoint(x:screenSize.width , y: screenSize.height/3),
CGPoint(x: screenSize.width/2, y: platform.sprite.frame.height),
CGPoint(x: 0.0, y: screenSize.height/3),
CGPoint(x: CGFloat(pos1) + screenSize.width/20, y: upperSpearPosHeight)]

final func didReachPoint() {
//reached point!
pathIndex++

if pathIndex >= ballPath.count && repeats {
pathIndex = 0
}
}

func updatePath() {

if pathIndex >= 0 && pathIndex < circlePath.count {
let destination = circlePath[pathIndex]
//currentPosition = destination
let displacement = CGVector(dx: destination.x-circle!.sprite.position.x, dy: destination.y-circle!.sprite.position.y)
let radius = sqrt(displacement.dx*displacement.dx+displacement.dy*displacement.dy)
let normal = CGVector(dx: displacement.dx/radius, dy: displacement.dy/radius)
let impulse = CGVector(dx: normal.dx*travelSpeed, dy: normal.dy*travelSpeed)
let relativeVelocity = CGVector(dx:impulse.dx-circle!.sprite.physicsBody!.velocity.dx, dy:impulse.dy-circle!.sprite.physicsBody!.velocity.dy);
circle!.sprite.physicsBody!.velocity=CGVectorMake(circle!.sprite.physicsBody!.velocity.dx+relativeVelocity.dx*rate, circle!.sprite.physicsBody!.velocity.dy+relativeVelocity.dy*rate);

if radius < pointRadius {
didReachPoint()
}
}
}

联系方式:

        func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody
var secondBody : SKPhysicsBody

if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}

if firstBody.categoryBitMask == circleCategory && secondBody.categoryBitMask == bonusCategory {
let img = SKTexture(imageNamed: "rectangular")
(firstBody.node! as? SKSpriteNode)?.size = img.size()
firstBody.node!.physicsBody = SKPhysicsBody(texture: img, size: img.size())
firstBody.node!.physicsBody?.allowsRotation = false
changeCircleAction = SKAction.setTexture(img)
firstBody.node!.runAction(changeCircleAction)
}

if firstBody.categoryBitMask == circleCategory && secondBody.categoryBitMask == platformCategory {

print("touched platform")
}

if firstBody.categoryBitMask == circleCategory && secondBody.categoryBitMask == smallStarCategory {

removeStar(secondBody.node!)
}

最佳答案

似乎当您将 firstBody 更改为矩形时(在 didBeginContact 方法中),您没有设置位掩码。根据您的描述,您似乎想将其设置为 circleCategory:

firstBody.node!.physicsBody?.categoryBitMask = circleCategory

我会将其放在 firstBody.node!.physicsBody?.allowsRotation = false 的正下方。

关于swift - body 从圆形变为矩形时无法识别联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32891830/

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