身体从圆形更改为矩形时无法识别联系人 [英] contacts not recognized when body is changed from circle to rectangle

查看:11
本文介绍了身体从圆形更改为矩形时无法识别联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的帮助下在这里 我已经让一个圆形体穿过给定的路径.我在一些路径点有一些尸体,并在 didBeginContact 中记录了联系.当身体与特定身体接触时,圆形身体将变为矩形.假设这个矩形体穿过与原始圆形体相同的路径,但由于没有记录接触,它没有到达路径点.我也尝试将 radiusPoint 更改为矩形的宽度或高度,但这没有用.矩形体也比圆形体大.如何让矩形穿过识别出接触的点?请看下面的代码.

With help from here I have made a circle body traverse a given path. I have some bodies at some of the path points and have logged contact in didBeginContact. When the body gets in contact with a specific body the circle body is changed to a rectangle. This rectangular body is suppose to traverse the same path as the original circle body but it doesn't reach the path points as the contact is not logged. I tried changing radiusPoint to the width or height of the rectangle also but that didn't work. Also the rectangle body is bigger than the circle body. How can I get the rectangle to traverse the points with the contact recognised? Please see code below.

路径遍历相关代码:

    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 如下:

It seems like when you change the firstBody to a rectangle (in the didBeginContact method), you don't set the bit mask. From what you described, it appears you want to set it to circleCategory as such:

firstBody.node!.physicsBody?.categoryBitMask = circleCategory

我会把它放在 firstBody.node!.physicsBody?.allowsRotation = false 的正下方.

I would put this right below the firstBody.node!.physicsBody?.allowsRotation = false.

这篇关于身体从圆形更改为矩形时无法识别联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆