How to find contacted object, which is not inherited Node class, when they contact using physics body?

Hi, here are some code.

class Missile
{
    ....
    cocos2d::Sprite sprite{ nullptr };
    cocos2d::PhysicsBody physicsBody{ nullptr };
    float damage{ 0.0f };
    ....
}

....

class Zombie
{
    ....
    cocos2d::Sprite sprite{ nullptr };
    cocos2d::PhysicsBody physicsBody{ nullptr };
    float hp{ 0.0f };
    ....
}

What I want to do is adding Physics Contact Event after initializing sprite and physics so
when the missile hits zombie, zombie will decrease hp by missile’s damage.

contactListener->onContactBegin = [this](cocos2d::PhysicsContact& contact)->bool
{
    auto A{ contact.getShapeA()->getCategoryBitmask() };
    auto B{ contact.getShapeB()->getCategoryBitmask() };
    if (A != B)
    {
        // Find which missile has hit.
        // Find which zombied has hit
        if(A == ObjectTypeBitmask::Missile)
        {
        }
        else
        {
        }
        return true;
    }
    return false;
}

When the missile hits zomibe, it calls event function above. But I could find which ones have contacted.

How do I find which missile and zombie have contacted?

  1. you need to set bitmasks for each, I don’t see this being done

  2. cpp-tests shows this exactly.

Thanks, but the solution is not what I asked.

The ball is a sprite had circular physics, so it can be casted to do some physics work.

But my one, Zombie and Missile class, are not inherited from node, physics or other class derived from Ref. So, These cannot cast to Zombie or Missile.

These just have a sprite, a physics body and some properties like damage, hp, etc.
I implemented the physics body contact event as written on tutorial document.

But when they contacted, I cannot access to property damage, hp, ect.

So I though about that,

  1. Missile and zombie are inherited from physics body. So when they contact, they can be cast to missile and zombie to access damage, hp, ect.
    But I think missile HAS A physics body, not IS A phyics body.

  2. When they contact, find the missile and zombie using shape A and B position.
    But if missiles and zombies are a lot, the performance goes low.

  3. Similar to 2. When they contact, set tag of shape A and B “contacted” to find they are contacted.
    This is same performance problem.

Do you know other better ways to do that?

I guess there needs to be more code shown.

While it’s understandable that you’re applying object oriented rules to this, it’s not quite the same thing in your case, and I don’t think it applies. It is also working against you, and perhaps not the right way to think about it.

The missile is just a visual representation of the physics body, so don’t think about it as a separate entity, because it is one and the same. That physics body could be represented by a tennis ball for all you care, since how you visualize it is up to you, and how it looks is not what dictates it’s interaction with other objects.

So, with that in mind, the missile class should be inheriting all the traits of the physics object, because that is what it represents.

Thanks, It clarify my thought.