Collision Reporting

swyphcosmo

10-05-2006 01:47:06

How does nxOgre handle collision reporting? I am trying to detect the collision between a two objects (a bullet and an enemy) in a project I am currently working on. I was interested in the statemachine described in the book draft. When I went to implement it, I was unable to find the createContactPair functions found on page 75. Is there another way to accomplish this? Any help would be appreciated.

Thanks,
-Michael

betajaen

10-05-2006 09:31:48

Yes! Loads of help.

There are two types of contact reporters; "contact" and "State". Contacts are mostly all code. State contacts utilise the state system which passes on states between bodies, with the state system however the code is reusable (such as assigning a state to indicate that the enemy is dead).


The contact reporter way.


class myContactReporter : public customContactReporter {

bool onStartTouch(body *a, body *b) {
if (a->getName == "bullet" && body->getName = "enemy") {
// Kill enemy code goes in here.
}
}

bool onEndTouch(body *a, body *b) {
}

bool onTouch(body *a, body *b) {
}
}


...


mScene->mContactReporter->addReporter(new myContactReporter());
mScene->createContactPair(bullet,enemy);





The State way.

First you should think out semantically. The enemy should have an IS_ALIVE state, and the bullet to have DEADLY state.

Once the bullet (or anything with DEADLY state) touches our enemy with IS_ALIVE, it should delete the IS_ALIVE.


enum myStates {
IS_ALIVE = 1,
DEADLY = 2
};

..

// Set up a contact so if *ANY* body as a contact pair which is deadly meets another body which IS_ALIVE - clear IS_ALIVE

nxOgre::contactReporter::stateContact c;
c.action = nxOgre::contactReporter::CLEAR_B;
c.bodyA_state = DEADLY;
c.bodyB_state = IS_ALIVE;
c.type = nxOgre::contactReporter::START_TOUCH;
mScene->mContactReporter->mStateContacts.push_back(c);

..


// Create our enemy code goes here.

enemy->addState(IS_ALIVE);


// Bullet code goes here.

bullet->addState(DEADLY);


// Alright that's the states part done, perhaps we should write some actual code for the death scenes!

class myStateMachine : public customStateMachine {

bool simulate(body *_body, state &_state) {

if (_state.status == DELETE || _state.status == ACTIVATED)
return false;

switch(_state.type) {

case IS_ALIVE:
return set_IS_ALIVE(_body,_state);
break;

}

return false;
}

bool set_IS_ALIVE(body *_body, state &_state) {
if (_state.status == STARTED) {
// Nothing really needed to be done here, but we should
// activate it.
_state.status = ACTIVATED;
return true;
}
else if (_state.status == CLEAR) {
// Okay the enemy has died.

// Enemy dying code goes here.


// We've done our dying thing, we should call for a delete.
_state.status = DELETE;
return true;
}

return false;
}

}




That's it. But I think you should use the contactReporter for your bullet to enemy thing, it is faster and more suitable. However say if your game has explosive mines or runs into a room full of deadly gas, then the state system would be more suitable. Considering the mine and gas code would be 99.9999% the same ;)

SFAOK

17-05-2006 18:41:00

I'm sorry if I'm being a bit slow here, but I can't find a contactReporter function anywhere in NxOgre!

I'm also looking for a way of reporting when two objects are coliding (when a foot touches the ground, so I can 'clamp' it in place and set off some dust particles). But so far the closest I've got is this thread!

Any help would be great! :)

betajaen

17-05-2006 18:58:25

Checkout NxTutorial105 and the "customContactReporter" class, I believe it's only in the CVS though. :D

SFAOK

17-05-2006 19:10:19

Checkout NxTutorial105 and the "customContactReporter" class, I believe it's only in the CVS though. :DAh I see. I can't actually get the CVS because unless I'm mistaken downloading needs certain UDP ports open, which the firewall around my University doesn't like :( I guess I'll have to figure something out! Thanks :)

betajaen

17-05-2006 19:24:54

It would take some time but you could manually down load the CVS from browsing it from the Sourceforge web site.

SFAOK

17-05-2006 19:35:35

It would take some time but you could manually down load the CVS from browsing it from the Sourceforge web site.
I looked there, but all I could find was 0.5.Preview.3.0. Is this the latest CVS? I assumed it was the standard release I already had :)

http://sourceforge.net/project/showfile ... _id=155574

betajaen

17-05-2006 19:47:21

Nope, this is the CVS:

http://nxogre.cvs.sourceforge.net/nxogre/nxogre/NxOgre/