Contacts are not recognized when the body changes from a circle to a rectangle

With the help here, I made a circular body crossing this path. I have some bodies at some points in the path and a contact is registered in didBeginContact . When the body contacts a specific body, the body of the circle changes to a rectangle. This rectangular body should follow the same path as the original body of the circle, but it does not reach the points of the path, since the contact is not registered. I tried changing the radiusPoint to the width or height of the rectangle, but that didn't work. Also, the body of the rectangle is larger than the body of the circle. How can I get a rectangle for passing points with a recognized contact? See code below.

Code associated with path traversal:

  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() } } } 

Contact Code:

  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!) } 
0
swift sprite-kit skphysicsbody
source share
1 answer

It seems that when you change firstBody to a rectangle (in the didBeginContact method), you do not set the bit mask. From what you described, it seems you want to set it as circleCategory as such:

 firstBody.node!.physicsBody?.categoryBitMask = circleCategory 

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

0
source share

All Articles