So I have two physics bodies (one moving one stationary) and I am trying to make it so that when they collide, another physics body spawns. The issue is that if the two bodies collide again and the body from before is already added it crashes. When the initial collision happens, the moving object has a positive velocity vector. The objects will collide again, but the moving object will now be falling, and therefore wont have the positive vector, thus not spawning the 3rd body again. Not sure if this is possible in swift. Thanks for your help!
Code:
func didBegin(_ contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if ((firstBody.categoryBitMask == PhysicsCatagory.Ball
&& secondBody.categoryBitMask == PhysicsCatagory.NodeSpawner
|| firstBody.categoryBitMask == PhysicsCatagory.NodeSpawner
&& secondBody.categoryBitMask == PhysicsCatagory.Ball)
&& (Ball.physicsBody?.velocity // is a positive vector )){
self.addChild(Bottom)}
}
I have tried moving the node after the first collision to get it out of the way of the second collision.
e.g.
if (firstBody.categoryBitMask == PhysicsCatagory.Ball
&& secondBody.categoryBitMask == PhysicsCatagory.NodeSpawner
|| firstBody.categoryBitMask == PhysicsCatagory.NodeSpawner
&& secondBody.categoryBitMask == PhysicsCatagory.Ball){
self.addChild(Bottom)
NodeSpawner.physicsBody?.isDynamic = true
NodeSpawner.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
NodeSpawner.physicsBody?.applyImpulse(CGVector(dx: 190, dy: 0)}
}
Seemed to crash the app as well. I guess you cant apply an impulse on a node during collisions? Any explanations?
Thanks again!
Aucun commentaire:
Enregistrer un commentaire