0

Is it possible to detect if a node collides on only one side with another side?

For example an animal that runs against another animal. In that case the collision detection method gets called. But when the animal jumps over an animal and "walks" on it the collision detection do not get activated.

I would appreciate every answer. Thanks

My code:

if (lionNode.frame.origin.y + lionNode.frame.size.height < animalNode.frame.origin.y) {
        NSLog(@"Walk");
    }
    else {
        NSLog(@"dead");
    }
David P
  • 228
  • 2
  • 14

2 Answers2

1

in the collision detection callback check the following.

if(y-origin of the first animal + its height < the y-origin of the second animal){
    //then do the logic for walking on top of another animal (probably nothing would happen here depending on what you are trying to build)
}
else {
    //forward to collision logic (e.g life loss, lose points, restart level ,etc..)
}

if it is possible for the first animal to go underneath the second animal then you need to update your condition to the following

if(firstAnimal.origin.y + firstAnimal.frame.size.height < secondAnimal.frame.origin.y  
|| firstAnimal.origin.y > secondAnimal.frame.origin.y + secondAnimal.frame.size.height)

be wary of different coordinate systems it could be confusing sometimes. hope this helps, feel free to ask if anything is unclear.

rockstarr
  • 296
  • 1
  • 4
  • Thank's for your answer! I have tried it multiple times but it have not worked yet.. I have posted my code above – David P Mar 21 '14 at 22:36
  • try logging the origin values and the height of the lion to get an understanding of what is going on. if nothing makes sense try using the position instead as follows – rockstarr Mar 22 '14 at 13:21
  • I think i found the problem in the code, in skscene the origin is at the bottom left so the condition should change to greater than if (lionNode.frame.origin.y + lionNode.frame.size.height > animalNode.frame.origin.y) – rockstarr Mar 22 '14 at 13:35
  • Two things first try to log the values and share them so that we could get an understanding of what values are being compared. Second how are you moving the sprites around (e.g run action, change position, ..) and if you could please share the code in which you define the physics bodies of both sprites – rockstarr Mar 23 '14 at 22:30
  • The problem is solved. The y origin of the animal is always nil, so the else condition cannot get called. Thanks for your great help!! – David P Mar 25 '14 at 12:21
0

The collision detection will get called in ether case but in the callback you can compare the velocity vectors; if they're moving away from each other treat it as a miss.

geowar
  • 4,397
  • 1
  • 28
  • 24
  • Thank's for your answer! Could you please explain your idea a little more? I am not quite clear what you mean with comparing the velocity vectors in the callback. – David P Mar 20 '14 at 20:10