当身体从圆形变为矩形时,无法识别联系人

2024-03-08

在来自的帮助下here https://stackoverflow.com/questions/32352886/how-to-set-physics-properties-for-a-circle-so-it-follows-given-path我已经制作了一个圆形物体穿过给定的路径。我在某些路径点有一些尸体并已记录联系信息didBeginContact。当物体与特定物体接触时,圆形物体会变成矩形。该矩形体应该与原始圆形体经过相同的路径,但由于未记录接触,因此它不会到达路径点。我尝试改变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.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当身体从圆形变为矩形时,无法识别联系人 的相关文章

随机推荐