0
votes

My problem is very simple, but I can't fix it.

I have a radar that is rotating and I have a player that is moving with a Joystick

Well, I just want to detect the collision between the radar and the player. It works perfectly when both are moving but it doesn't when radar is moving and my player is not.

Here you have the code to detect it and how it works:

//moves radar
    [self schedule:@selector(loopRadar) interval: 12];

//moves player with joystick
    body->SetLinearVelocity(b2Vec2(scaledVelocity.x*dt, scaledVelocity.y*dt));
    actor.position = ccp(body->GetPosition().x * PTM_RATIO,
                         body->GetPosition().y * PTM_RATIO);

// DETECTS COLLISION BETWEEN RADAR AND PLAYER
    std::vector<MyContact>::iterator pos;
    for(pos = _contactListener->_contacts.begin();
        pos != _contactListener->_contacts.end(); ++pos) {
        MyContact contact = *pos;



        if ((contact.fixtureA == radarBody->GetFixtureList() && contact.fixtureB == body->GetFixtureList()) ||
            (contact.fixtureA == body->GetFixtureList() && contact.fixtureB == radarBody->GetFixtureList())) {

            //DO SOMETHING LIKE GAME OVER



        }

    }
//ENDS COLLISION

MycontactListener class:

#import "MyContactListener.h"

MyContactListener::MyContactListener() : _contacts() {
 }

 MyContactListener::~MyContactListener() {
 }

void MyContactListener::BeginContact(b2Contact* contact) {
// We need to copy out the data because the b2Contact passed in
// is reused.
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
_contacts.push_back(myContact);
}

void MyContactListener::EndContact(b2Contact* contact) {
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
std::vector<MyContact>::iterator pos;
pos = std::find(_contacts.begin(), _contacts.end(), myContact);
if (pos != _contacts.end()) {
    _contacts.erase(pos);
}
}

void MyContactListener::PreSolve(b2Contact* contact,
                             const b2Manifold* oldManifold) {
}

void MyContactListener::PostSolve(b2Contact* contact,
                              const b2ContactImpulse* impulse) {
}

So, the problem is that when radar goes through the player and player isn't moving, it doesn't detect collision, but when both are moving it works perfectly.

Any tips? Thank you very much!

1
have you tried disabling sleep of player & radar bodies?LearnCocos2D
Worked! thank you very much!!mursang

1 Answers

0
votes

Worked adding these lines as "LearnCocos2D" said, setting both bodies awake

    body->SetAwake(TRUE);
    radarBody->SetAwake(TRUE);

Thank you very much!