Collision Shape memory error

killsto
Posts: 10
Joined: Mon Mar 30, 2009 2:54 am

Collision Shape memory error

Post by killsto »

At runtime I get an access violation. All this is program tries to do is take the hello world and make an object class. I can't figure out this access violation or the life of me, and I do not know why.

physics.h:

Code: Select all

#ifndef __physicsinit__
#define __physicsinit__

#ifndef __btBulletDynamicsCommon__
#define __btBulletDynamicsCommon__
#include <btBulletDynamicsCommon.h>
#endif

class Object 
{
public:
	Object(btScalar radius, btTransform anglePos, btScalar mass);
	btRigidBody* getBody();

	btCollisionShape* collisionShape;
	btMotionState* motionState;
	btRigidBody* rigidBody;

};
btDiscreteDynamicsWorld* makeworld();





#endif
physics.cpp:

Code: Select all

#include "physics.h"



btDiscreteDynamicsWorld* makeworld()
{
	btVector3 worldAabbMin(-1000,-1000,-1000);
	btVector3 worldAabbMax(1000,1000,1000);

	btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin, worldAabbMax, 1024);

	btDefaultCollisionConfiguration* collisionConfig = new btDefaultCollisionConfiguration();

	btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfig);

	btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;

	btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfig);

	dynamicsWorld->setGravity(btVector3(0,-10,0));

	return dynamicsWorld;
}


Object::Object(btScalar radius, btTransform anglePos, btScalar mass)
{
	this->collisionShape = new btSphereShape(radius);
	//btSphereShape(
	this->motionState = new btDefaultMotionState(anglePos);
	btVector3 inertia(0,0,0);
	this->collisionShape->calculateLocalInertia(mass, inertia);
	btRigidBody::btRigidBodyConstructionInfo rigidBodyInfo(mass, this->motionState, this->collisionShape, inertia);
	this->rigidBody = new btRigidBody(rigidBodyInfo);
}

btRigidBody* Object::getBody()
{
	return this->rigidBody;
}
main.cpp:

Code: Select all

#include "physics.h"
#include <btBulletDynamicsCommon.h>
#include <iostream>
using namespace std;


int main()
{

	btDiscreteDynamicsWorld* pworld = makeworld();
	pworld->setGravity(btVector3(0,-10,0));
	Object firstObject(1,btTransform(btQuaternion(0,0,0,1)),0); //sphere with no mass and regular pos
	Object secondObject(1,btTransform(btQuaternion(0,0,0,1),btVector3(0,500,0)),10); //above other sphere - should fall
	pworld->addRigidBody(firstObject.getBody());
	pworld->addRigidBody(secondObject.getBody());
    //   for (int i=0 ; i<300 ; i++) 
	   //{
    //    
    //            pworld->stepSimulation(1/60.f,10);
    //            
    //            btTransform trans;
				////groundRigidBody->getMotionState()->getWorldTransform(trans);

				//secondObject.rigidBody->getMotionState()->getWorldTransform(trans);
    //    
    //            cout << "sphere height: " << trans.getOrigin().getY() << std::endl;
    //    }




	return 0;
}
The access violation turns up in "collisionObject->getCollisionShape()->getAabb(trans,minAabb,maxAabb);" in bullet's btCollisionWorld.cpp file. I don't see why it can't get the collision shape. It is public and has not been deleted.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Collision Shape memory error

Post by Erwin Coumans »

Code: Select all

 Object(btScalar radius, btTransform anglePos, btScalar mass);
You should not pass any Bullet object on the stack, but pass them by reference. btTransform, btVector3, btQuaternion etc are 16-byte aligned, and use SIMD.
Can you replace it by the following code:

Code: Select all

 Object(btScalar radius, const btTransform& anglePos, btScalar mass);
and see if that fixes your problem?
Thanks,
Erwin
killsto
Posts: 10
Joined: Mon Mar 30, 2009 2:54 am

Re: Collision Shape memory error

Post by killsto »

I can't believe I missed that, but it did not fix the problem.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Collision Shape memory error

Post by Erwin Coumans »

  • What kind of machine / CPU do you have?
  • Do the Bullet demos work fine in debug and release mode?
  • Can you try to disable SSE, by editing Bullet/src/LinearMath/btScalar.h, and comment out line 66 into:

    Code: Select all

    //#define BT_USE_SSE
    
  • How are you allocating collision shapes?
  • Full callstack at the time of the crash.
  • Can you provide a full testbed, reproducing the crash?
Thanks,
Erwin
killsto
Posts: 10
Joined: Mon Mar 30, 2009 2:54 am

Re: Collision Shape memory error

Post by killsto »

I got it to work using Bullet-2.69; however, I understand that this may need to be worked out so I wil provide you with information.

1.I have an intel Core2Duo.
2.I don't recall if I actually ran the demo's, I will try later and get back to you on that. I need to rebuild them.
3.I'm allocating collision shapes dynamically.
4.I don't know what that is, I'll look it up later and try it.
5. Same as above.

I'm using Windows xp and windows 7 (both 32 bit) with VC++ express 2008.

I'll try to look into the differences between the two version to try to figure out why 2.69 works but not 2.74 if I have time.
User avatar
hatboyzero
Posts: 5
Joined: Mon Jun 01, 2009 1:50 am
Location: Oxford, MS

Re: Collision Shape memory error

Post by hatboyzero »

I am experiencing the exact same issue. Investigating it further, I found that the craziness ensues when calling the btRigidBody constructor.

Essentially, what I am doing is this:

Code: Select all


    btDefaultMotionState* pMotionState =
        new btDefaultMotionState();

    btRigidBody::btRigidBodyConstructionInfo rbInfo(
        static_cast<btScalar>(m_pDynamicsData->getMass()),
        pMotionState,
        m_pRootCollisionShape.get(),
        localInertia
    );

    rbInfo.m_restitution = static_cast<btScalar>(m_pDynamicsData->getRestitution());
    rbInfo.m_friction = static_cast<btScalar>(m_pDynamicsData->getFriction());

    btRigidBody* pBody = new btRigidBody(rbInfo);

    m_pCollisionObject.reset( pBody );

    pSimulation->getBtDynamicsWorld()->addRigidBody( pBody );

The contents of the rbInfo struct seem to be sane just before entering the constructor to btRigidBody. *However*, inside of the btRigidBodyConstructor, just before setupRigidBody() is called, I checked the contents of the constructionInfo struct (which, incidentally, should be the same as rbInfo since it is just rbInfo passed by reference). The contents of said struct are most definitely not sane...

The m_mass and m_motionState members seem sane in both cases, but things start to go south when you look at the m_startWorldTransform member and all following members. As a matter of fact, it seems that m_startWorldTransform.m_origin.m_floats[3] in constructionInfo (inside the constructor) is equivalent to m_localInertia.m_floats[0] in rbInfo (outside of the constructor). After exiting the constructor, the contents of rbInfo are still sane, so I don't think anything is actually *corrupted*, so to say -- to be honest, I think it's just an alignment issue, but I haven't quite put my finger on how to fix it :P

How this relates to the "collisionObject->getCollisionShape()->getAabb(trans,minAabb,maxAabb);" access violation is due to the face that the m_collisionShape is initialized with an invalid object.

If you figure out how to fix this before I do, please share ;) Thanks,
User avatar
hatboyzero
Posts: 5
Joined: Mon Jun 01, 2009 1:50 am
Location: Oxford, MS

Re: Collision Shape memory error

Post by hatboyzero »

bump.
User avatar
hatboyzero
Posts: 5
Joined: Mon Jun 01, 2009 1:50 am
Location: Oxford, MS

Re: Collision Shape memory error

Post by hatboyzero »

Ok, I'm at wits end concerning this particular problem -- no matter what I try (forcing btRigidBodyConstructionInfo to be 16 byte aligned, allocating a new btRigidBodyConstructionInfo and assigning it to a pointer versus allocating it on the local stack, etc.), I still experience the same problem. It was suggested by my mentor that this could be related to a DLL boundary error -- unfortunately, he doesn't have the time to investigate this issue in detail, and I'm still banging my head against a brick wall...

Does anyone have any suggestions for a solution to this problem? Thanks,
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Collision Shape memory error

Post by Erwin Coumans »

Again, do the Bullet demos compile, link and run fine?

Please zip all the files for a fully building reproduction case, cpp/header files and projectfiles, and attach it to this topic.
Snippets are not useful.
DLL boundary error
Are you using Bullet inside a DLL?
Thanks,
Erwin
User avatar
hatboyzero
Posts: 5
Joined: Mon Jun 01, 2009 1:50 am
Location: Oxford, MS

Re: Collision Shape memory error

Post by hatboyzero »

I am using Bullet inside a DLL -- I was actually implementing it as part of a plugin (ZBullet) for a game engine framework I've been working on (IndieZen -- http://www.indiezen.org/wiki). This problem has been reproduced by multiple parties on multiple machines.

I will generate a zip file of the source and attach it shortly.

Thanks,
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Collision Shape memory error

Post by Erwin Coumans »

Again, do the Bullet demos compile, link and run fine?

Unfortunately, dynamically linking Bullet is not supported, we are not joining you in DLL-hell :-)
If you can reproduce it statically linked, we might be able to help out.
Thanks,
Erwin
User avatar
hatboyzero
Posts: 5
Joined: Mon Jun 01, 2009 1:50 am
Location: Oxford, MS

Re: Collision Shape memory error

Post by hatboyzero »

:? That being the case, I'll have to put off implementing a ZBullet plugin for Zen until it does support dynamic linking... Regardless, thanks for the response,

Edit: Oh, and yes, the bullet demos all compile, link, and run fine ;)
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Collision Shape memory error

Post by Erwin Coumans »

Bullet should work fine with dynamic linking, and the Dynamica Maya plugin uses Bullet in DLLs.

We just don't support developers getting this to work. You might want to try the Microsoft support helpline :-)
Thanks,
Erwin
spy32
Posts: 19
Joined: Fri Aug 01, 2008 1:12 pm

Re: Collision Shape memory error

Post by spy32 »

Hey guys. I've got exactly the same problem here. The demos are working just fine on my PC, but for some reason when I compile it in my own project nothing works. Maybe my code can help you to fix this problem. http://pastebin.com/f78f2263d. I've decided to upload my whole project. You only will need irrlicht 1.5.1 as additional dependency: http://rapidshare.com/files/270339110/T ... 08-2009.7z. Another weird thing is that the error only comes at the second, dynamic box and not at the static "fake plane". I really hope this will be fixed soon, because I want to continue using bullet.

So here a few informations about my machine:
OS: Windows XP SP3 32bit
CPU: Intel Core 2 Quad Q6700
Compiler: msvc 9.0 sp1
Bullet Version: 2.75-rc7

Greetz spy

Ps. Can anyone say me which version I have to use to avoid this bug?
spy32
Posts: 19
Joined: Fri Aug 01, 2008 1:12 pm

Re: Collision Shape memory error

Post by spy32 »

*push*