I am having a difficult time with collision detection. As an example, let’s use the uberjumper as our environment. Works great … in learning I am trying to keep the unknown variables low. To that end - I wanted to take that and add falling objects from the top to the bottom of the screen. I created my object as an SKNode - much like the ‘player’ in the game and then gave it physicsbody? parameters so that it is affected by gravity etc…
The object 'misses ’ the platforms and collides with the player, but it cannot move past the stars of the game…
I cannot get the falling object to MISS the stars. Also, how can I move the player object?
I register the hit with Collisionetc.player & Collisionetc.fallingobject - and I added the player.position = CGPoint(new x/y) to simulate a hit and a bump, the code actually executes via breakpoint, but the player doesn’t actually move…
I am thinking it is a SKNode vs SKSpriteNode …?
I have tried start.zPosition and zPostion with the player/enemy and changing their respective levels. I have changed the ‘collisionBitMask’ for the object to a different value.
Any guide lines? Collisions is difficult with 3 or more objects to manage…
Could you post your code for collision detection?
Are you logging each time a different type of collision is detected, such as one log for player + object vs a different log for object vs platform?
I am logging each ‘collision’. I started from scratch last night as a test and will post my code. It was a fun exercise, but I still cannot get the ‘player’ to react to the collision.
Code Bits:
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
}
bounceCount = bounceCount + 1 // I used the bounceCount to 'spawn' enemies
if ((firstBody.categoryBitMask & CollisionCategoryBitmask.Player != 0) && (secondBody.categoryBitMask & CollisionCategoryBitmask.Enemy != 0)) {
let whichNode = (contact.bodyA.node != player ) ? contact.bodyA.node : contact.bodyB.node
let other = whichNode! as SKNode
print("Other Name: \(other.name)")
if other.name == "NODE_ENEMY" {
print("Contact: Enemy")
self.collisionWithEnemy(player)
} else if other.name == "NODE_PLATFORM" {
/* platform in this case is just the bottom of the 'screen' SKPhysicsBody(rectangleOfSize: CGSize(width:size.width, height: 8.0)) */
print("Contact: Platform")
self.collisionWithPlatform(player)
}
}
}
This code works fine… I log the collision, the resulting func does get called:
func collisionWithPlatform(player: SKNode) ->Bool {
if player.physicsBody?.velocity.dy < 0 { // dy < 0, if falling then…
player.physicsBody?.velocity = CGVector(dx: player.physicsBody!.velocity.dx, dy: 250.0) // bounce and bounce
}
return false
}
func collisionWithEnemy(player: SKNode) {
player.position = CGPoint(x: 20.0, y: player.position.y)
// I want the 'player' to move to the 'left', but stay in the same 'y-plane' - it should work...
}
Ironically, the ‘player’ bounces fine which is a result of the func collisionWithPlatform.
Any ideas? I can post more, but the create player and enemy are functionally identical as well as the stars.
Stars aside, if I can figure out why the player doesn’t move after collision with the enemy, I think I can get the enemy to move past the stars. I think they might be related.
Thanks!
Neil
P.S., Logs:
Added Toaster // enemy
RandomEnemyPlaceX < 100 210.0
Other Name: nil // no contact
Other Name: Optional(“NODE_PLATFORM”) //bounce
Contact: Platform // calls func collisionWithPlatform
Other Name: Optional(“NODE_ENEMY”) // toaster
Contact: Enemy // calls func collisionWithEnemy
Other Name: nil // no contact
Other Name: Optional(“NODE_PLATFORM”) // bounce
Contact: Platform // calls func collisionWithPlatform
Other Name: nil // no contact
So the player does collide with the platform and even bounces off of it. So what isnt working as expected?
Yup… the player bounces off the platform - but the enemy does not… I thought it would be easy to create more than one collision, but turns out, that isn’t true.
IT seems that I can have a effect for collision but only in the ‘y’ direction.
Code snippet:
func collisionWithEnemy(player: SKNode) → Bool{
player.physicsBody?.velocity = CGVector(dx: (player.physicsBody!.velocity.dx - 100.0), dy: 300.0) // this give the 'player a boost in the 'y' plane, but the 'x' plane is unaffected.
}
Any ideas?