Frage

I'm new to C++ so excuse me if my use of terminology is off.

I'm writing an app in openframeworks that makes use of a physics library called ofxMSAPhysics. For my purposes, I've created a custom class called "particle," which inherits from the MSAPhysics Particle3D class.

Here I pass a pointer to my custom object to the physics engine:

particle * p = new particle(ofVec3f(getTokenCoors(index)));
physics.addParticle(p);

In my custom class I have a public member called collide that I set to true when one particle collides with another. To do this, I override one of Particle3D's virtual methods:

void collidedWithParticle(ParticleT *other, ofVec3f collisionForce) {
        collide = true;
    }

Now I'd like to check to see when one particle collides with another. physics.getParticle(i) returns a particle from the physics engine, but one of the type Particle3D, and not of my custom particle type. So when I loop through the particles returned from getParticle(), none of them contain my "collide" variable.

Is there a way to access my custom particles once they are added to the engine?

Please let me know if I can clarify something.

EDIT: collidedWithParticle() is a virtual member of Particle3D. I just had my overridden method set collide to true to see if it worked.

War es hilfreich?

Lösung

if you're sure that all particles in the system are of your overiden type, then you can simply cast the Particle3D to particle by using

particle* myParticle = static_cast<particle*>(other);

but this way isn't of the safest ones, i'd suggest you, if possible check maybe there are some methods that you could override in Particle3D class that would enable you to see if particle collided or save a list of your particles and use that list to be sure which particles are of your inherited type.
Of course dynamic_cast would be more appropriate and more safe, but i don't suggest using it because of RTTI, since RTTI heavily slows down code execution.

Andere Tipps

When you look through your particles, you need to have all the objects cast to the base class. You could technically have an array like so:

std::vector <Particle3D*> particles;

Because your own particle has inherited from this class, you can do the following:

particle* yourParticle = new particle();
particles.push_back(yourParticle);

You are allowed to do this because yourParticle is a class that has Particle3D as a base class. Thats just one of the many features of Object Oriented programming.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top