Classes containing rigidBodies

Post Reply
einsa92
Posts: 1
Joined: Tue Feb 17, 2015 3:38 pm

Classes containing rigidBodies

Post by einsa92 »

Me and a few friends are trying to create a class (Leaf) containing a btRigidBody, and then putting instances of these objects in a vector and adding them to the world. This works fine on my mac with Xcode, however we can't get it to work on a Windows machine with Visual Studio 2013. Getting a weird memory error when trying to adding the leafs to the world: "Unhandled exception at 0x009F392D in Win32.exe: 0xC0000005: Access violation reading location 0xFFFFFFFF"

Here's the relevant code (might be some trash in there)
Leaf.h

Code: Select all

class Leaf
{
public:
    Leaf(double m, double a, double dens, double air,
            const btVector3& pos, const btVector3& flu,const btVector3& angularVel);
	//~Leaf();
	btRigidBody* getBody();
	float setFlutter(float time);
	btVector3 getFlutter(const btVector3& angularPos);
	btVector3 getRotation();
    double getMass();
	void drawLeaf();
	btVector3 getPosition();
	double getAirResistance(const btVector3& velocity, double a, double d);

protected:
	glm::vec3 velocity;
	double mass, angle, airCoeff, density, area;
	btVector3 flutter;
	btVector3 position, angularVelocity;
	
	btRigidBody* leafBody;
    btCollisionShape* fallShape;
    btDefaultMotionState* fallMotionState;
    btVector3 fallInertia;
    btQuaternion rotation;
};
Leaf.cpp

Code: Select all

Leaf::Leaf(double m, double a, double dens, double air,
           const btVector3& pos, const btVector3& flu,const btVector3& angularVel)
{
    mass = m;
    area = a;
    density = dens;
    airCoeff = air;
    position = pos;
    flutter = flu;
    angularVelocity = angularVel;
    
    rotation=btQuaternion(0,0,0,1);
    fallShape = new btSphereShape(1);
    fallMotionState =
    new btDefaultMotionState(btTransform(rotation, btVector3(0, 3, 0)));
    fallInertia=btVector3(0,0,0);
    fallShape->calculateLocalInertia(mass, fallInertia);
    btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState, fallShape, fallInertia);
    leafBody = new btRigidBody(fallRigidBodyCI);
    leafBody->setLinearVelocity(btVector3(0, 0, 0));
}

btRigidBody* Leaf::getBody()
{
	return leafBody;
}
World.h

Code: Select all

class World
{
    public:
        World();
       // ~World();
        void initWorld();
        btDiscreteDynamicsWorld* getDynamicsWorld();
    private:
        btBroadphaseInterface* broadphase;
        btDefaultCollisionConfiguration* collisionConfiguration;
        btCollisionDispatcher* dispatcher;
        btSequentialImpulseConstraintSolver* solver;
        btDiscreteDynamicsWorld* dynamicsWorld;
};
World.cpp

Code: Select all

World::World()
{
    // Build the broadphase
    broadphase = new btDbvtBroadphase();
    
    // Set up the collision configuration and dispatcher
    collisionConfiguration = new btDefaultCollisionConfiguration();
    dispatcher = new btCollisionDispatcher(collisionConfiguration);
    
    // The actual physics solver
    solver = new btSequentialImpulseConstraintSolver;
    
    // The world.
    dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
    dynamicsWorld->setGravity(btVector3(0, -9.82, 0));
}

btDiscreteDynamicsWorld* World::getDynamicsWorld()
{
    return dynamicsWorld;
}
main.cpp

Code: Select all

int main(void)
{
	
	double airCoeff = 1.28;
	double dens = 1.2041;
	double area = 0.0025;
	double mass = 0.1;
	btVector3 pos(0, 0, 0);
	btVector3 angularVel(0.f, 0.f, 0.f);
	btVector3  flu(0.0f,0.0f,0.0f);
	vector <Leaf> theLeaves;
	srand(time(NULL));
	btVector3 wind(0.0f, 1.0f, 0.0f);

    World theWorld;
	btScalar transMatrix[16];
    btRigidBody* body;
	for (int i = 0; i < 10; i++)
	{
		float randNumbX = rand() % 10000 / 100 - 50;
		float randNumbY = rand() % 10000 / 100 - 50;
		float randNumbZ = rand() % 10000 / 100 - 50;
        pos = btVector3(randNumbX, randNumbY, randNumbZ);
        angularVel = btVector3(randNumbX, randNumbY, randNumbZ);
        Leaf newLeaf(mass, area, dens, airCoeff, pos, flu, angularVel);

		
        theLeaves.push_back(newLeaf);
    
        theWorld.getDynamicsWorld()->addRigidBody(newLeaf.getBody()); //this is where the error is (we think)
		
	}
Post Reply