Detect sprite collision (no physics) before second sprite added

Hi All

I am trying to implements collision detection (no physics) between sprites. Its simple enough to implement rect.IntersectsRect when both childs are added to the layer. However, what I want to do is detect collision before a sprite is added. If one child is added want to know when I add the second child it wont clash with the first. Anyone come across this before?

Raycast can do this by sending normal from the sprite. See the speed of current sprite and check will it collide or not given the angle, if it does then you can calculate time of collision.

Isn’t that physics based though ray cast? No physworld is enabled on level/game?

perhaps a check in the update() method to check the bounding boxs of the sprite’s? You could use something like:

cocos2d::Vec2 p = touch->getLocation();
cocos2d::Rect rect = this->getBoundingBox();
        
if(rect.containsPoint(p))
{
      //they are touching or overlapping by some amount           
 }

Thanks for suggestion but already tried all this the issue is before the second sprite is added to layer it won’t have data to check for collision on rect Internet. I am trying to achieve this before its added to later without collision.

I’m sorry, I don’t understand this.

Ok so you have…

  1. Sprite 1 (added to layer)
  2. Check that Sprite 2 doesnt clash with Sprite 1 (before added to layer)
  3. Sprite 2 gets added layer

Why not addChild() , having setVisible(false); check your collision, If it is touching, re setPosition(); setVisible(true) if it is not touching then just setVisible(true)

1 Like

omg I ve been looking 3 hours. You saved me thanks

1 Like