I have Ball
class which is derived from PhysicsObject
class. In the following method I am getting a Ball
instance from a void
pointer and checking if its of type PhysicsObject
. When I call the collidingWith
method, it is not being called in the Ball
class object. What am I doing wrong here?
UPDATE I have added an sscce at the bottom. Please refer that.
CODE
void Levels::BeginContact(b2Contact *contact) {
b2Fixture *fixtureA = contact->GetFixtureA();
b2Fixture *fixtureB = contact->GetFixtureB();
void *objA = fixtureA->GetUserData();
void *objB = fixtureB->GetUserData();
PhysicsObject* physicsObjA = reinterpret_cast<PhysicsObject*>(objA);
PhysicsObject* physicsObjB = reinterpret_cast<PhysicsObject*>(objB);
if ((physicsObjA != 0) && (physicsObjB != 0)) {
physicsObjA->collidingWith(physicsObjB); //not working
physicsObjB->collidingWith(physicsObjA);
}
}
PHYSICSOBJECT
#ifndef PHYSICSOBJECT_H_
#define PHYSICSOBJECT_H_
class PhysicsObject {
public:
PhysicsObject();
virtual ~PhysicsObject();
virtual void collidingWith(PhysicsObject *obj) = 0;
};
#endif /* PHYSICSOBJECT_H_ */
Ball.h
#ifndef BALL_H_
#define BALL_H_
#include "Box2D/Box2d.h"
#include "cocos2d.h"
#include "PhysicsObject.h"
class Ball : public PhysicsObject {
public:
//other methods
void collidingWith(PhysicsObject *obj);
};
#endif /* BALL_H_ */
Ball.cpp
void Ball::collidingWith(PhysicsObject *obj) {
CCLOG("Ball::collidingWith"); //this method is not being called
}
//other methods
Ball::Ball() {
//other code
b2FixtureDef ballShapeDef;
ballShapeDef.userData = this;
//other code
}
UPDATE
I have 3-4 other classes which are being derived from PhysicsObject
just like Ball
, those I havent mentioned here, but the common code is same.
UPDATE SSCCE
In main.cpp
's foo()
getting error at baseObjA->collidingWith(baseObjA);
Base Class
class Base
{
public:
Base(void);
virtual ~Base(void);
virtual void collidingWith(Base *obj) = 0;
};
Another Base Class
class AnotherBase
{
public:
AnotherBase(void);
~AnotherBase(void);
virtual void foo();
};
Derived Class Header
#include "Base.h"
#include "AnotherBase.h"
class Derived :
public AnotherBase, Base
{
public:
Derived(void);
~Derived(void);
void collidingWith(Base *obj);
};
Derived Class Implementation
#include "Derived.h"
void Derived::collidingWith(Base *obj) {
printf("Ball::collidingWith");
}
main.cpp
#include "Derived.h"
void myFoo(void* userData);
int _tmain(int argc, _TCHAR* argv[])
{
Derived *derived = new Derived();
void* userData = derived;
myFoo(userData);
return 0;
}
void myFoo(void* userData)
{
Base* baseObjA = reinterpret_cast<Base*>(userData);
if (baseObjA != 0) {
baseObjA->collidingWith(baseObjA);// Error here
}
}