Domanda

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");
    }
È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top