Problem with Collision

jcfalcone
Posts: 11
Joined: Mon Mar 03, 2008 12:09 pm

Problem with Collision

Post by jcfalcone »

Hello Every one,
I'm trying to import a grayscale terrain but my meshs didn't collide with the terrain.

This is my Terrain Code

Code: Select all

	if(this->$Enabled) 
	{
		// Get the collision mesh
		this->$CollisionMesh = new btTriangleMesh();
		Engine::PhysicEngine::Instance()->ConvertCollisionMeshBuffer((IMeshBuffer *)(&$MeshBuffer), this->$CollisionMesh, this->$Scale);
		this->$Shape = new btBvhTriangleMeshShape(this->$CollisionMesh, true);

		// Orientation
		btTransform Transform;
		btVector3 $Bullet_Position = btVector3(this->$TPosition.X, this->$TPosition.Y, this->$TPosition.Z);
		this->$MotionState = Engine::PhysicEngine::Instance()->GetBulletTransform(Transform, $Bullet_Position);

		
		// Body
		this->$RigidBody = new btRigidBody(this->$Mass, this->$MotionState, this->$Shape);
		this->$RigidBody->setUserPointer((void *)(this->$Node));
		
		Engine::PhysicEngine::Instance()->Get_World()->addRigidBody(this->$RigidBody);
	}
And this is my Mesh Code

Code: Select all

       // ira calcular fisica se Massa for diferente de 0
       bool isDynamic = (this->$Mass != 0.0f);  
       
       // Get the collision mesh
       btTriangleMesh* $CollisionMesh = new btTriangleMesh();
       
       Engine::PhysicEngine::Instance()->ConvertCollisionMesh(this->$Mesh->getMesh(0),
                                                             $CollisionMesh,
                                                             this->$Scale);
                                                             
		this->$Shape = new btBvhTriangleMeshShape($CollisionMesh, true);

       btVector3 LocalInertia(0,0,0);
       
       if(isDynamic)
       {
         this->$Shape->calculateLocalInertia(this->$Mass, LocalInertia);
       }
       
       // calculate value for ccdThreshold
       core::aabbox3d<f32> box = this->$Node->getBoundingBox();
       core::vector3df ext = box.getExtent();
       f32 ret=0;
       if (ext.X>ret)
       {
         ret=ext.X;
       }
       if (ext.Y>ret)
       {
         ret=ext.Y;
       }
       if (ext.Z>ret)
       { 
          ret=ext.Z;
       }
       this->$Threshold = ret/2;
    
       // Orientation
       btTransform $Transform;
       this->$MotionState = Engine::PhysicEngine::Instance()->GetBulletTransform($Transform, this->$TPosition);
      
       // Body
       this->$RigidBody = new btRigidBody(this->$Mass, 
                                          this->$MotionState, 
                                          this->$Shape, 
                                          LocalInertia);
                                          
       this->$RigidBody->setUserPointer((void *)(this->$Node));
      
       Engine::PhysicEngine::Instance()->Get_World()->addRigidBody(this->$RigidBody);
       $Objects.push_back(this->$RigidBody);
       
       this->$RigidBody->setCcdSquareMotionThreshold(this->$Threshold);
       //Experimental: better estimation of CCD Time of Impact:
       this->$RigidBody->setCcdSweptSphereRadius(0.2*this->$Threshold);
But if i trade this:

Code: Select all

		this->$Shape = new btBvhTriangleMeshShape(this->$CollisionMesh, true);
to this:

Code: Select all

		this->$Shape = new btConvexTriangleMeshShape(this->$CollisionMesh);
in the mesh code and Terrain code, the colision works but Is very slow.

Some one can help me...
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Problem with Collision

Post by Erwin Coumans »

>> grayscale terrain

If it is a 2d heightfield terrain, you can try using btHeightfieldTerrainShape. Don't use btConvexHullShape shape for large static terrains, it is only for small convex objects (typically less then 100 vertices), and terrains are typically non-convex.

You should be able to use btBvhTriangleMeshShape, so the 'ConvertCollisionMeshBuffer' might have an issue.

Often, debug rendering of the collision shapes can help, try using the GLDebugDrawer. See the demos how to use this.
Hope this helps,
Erwin
jcfalcone
Posts: 11
Joined: Mon Mar 03, 2008 12:09 pm

Re: Problem with Collision

Post by jcfalcone »

i'm trying to import a 3D map.

i'm using Bullet + Irrlicht +Dev-CPP, and this is the ConvertCollisionMeshBuffer code:

Code: Select all

void Engine::PhysicEngine::ConvertCollisionMeshBuffer(IMeshBuffer *TMeshBuffer, btTriangleMesh *TCollisionMesh, const vector3df &TScale) 
{
	
	btVector3 TriangleVertices[3];
	u16 *Indices = TMeshBuffer->getIndices();
	if(TMeshBuffer->getVertexType() == EVT_STANDARD) {
		
		S3DVertex *Vertices = (S3DVertex *)TMeshBuffer->getVertices();
		for(u32 i = 0; i < TMeshBuffer->getIndexCount(); i += 3) {
			TriangleVertices[0] = btVector3(Vertices[Indices[i]].Pos.X * TScale.X,
											Vertices[Indices[i]].Pos.Y * TScale.Y,
											Vertices[Indices[i]].Pos.Z * TScale.Z);
			TriangleVertices[1] = btVector3(Vertices[Indices[i+1]].Pos.X * TScale.X,
											Vertices[Indices[i+1]].Pos.Y * TScale.Y,
											Vertices[Indices[i+1]].Pos.Z * TScale.Z);
			TriangleVertices[2] = btVector3(Vertices[Indices[i+2]].Pos.X * TScale.X,
											Vertices[Indices[i+2]].Pos.Y * TScale.Y,
											Vertices[Indices[i+2]].Pos.Z * TScale.Z);

			TCollisionMesh->addTriangle(TriangleVertices[0], TriangleVertices[1], TriangleVertices[2]);
		}
	}
	else {
		
		S3DVertex2TCoords *Vertices = (S3DVertex2TCoords *)TMeshBuffer->getVertices();
		for(u32 i = 0; i < TMeshBuffer->getIndexCount(); i += 3) {
			TriangleVertices[0] = btVector3(Vertices[Indices[i]].Pos.X * TScale.X,
											Vertices[Indices[i]].Pos.Y * TScale.Y,
											Vertices[Indices[i]].Pos.Z * TScale.Z);
			TriangleVertices[1] = btVector3(Vertices[Indices[i+1]].Pos.X * TScale.X,
											Vertices[Indices[i+1]].Pos.Y * TScale.Y,
											Vertices[Indices[i+1]].Pos.Z * TScale.Z);
			TriangleVertices[2] = btVector3(Vertices[Indices[i+2]].Pos.X * TScale.X,
											Vertices[Indices[i+2]].Pos.Y * TScale.Y,
											Vertices[Indices[i+2]].Pos.Z * TScale.Z);

			TCollisionMesh->addTriangle(TriangleVertices[0], TriangleVertices[1], TriangleVertices[2]);
		}
	}
}
I see here the Debug, and in the Dev-Cpp it's show a linker error, i can user another function or something from irrlicht to render the debug?

i found in others topics, that the bullet didn't collid 2 mesh, and you say to use the Gimpact, and they show a linker error here too:

[Linker error] undefined reference to `vtable for btBU_Simplex1to4'
[Linker error] undefined reference to `btBU_Simplex1to4::btBU_Simplex1to4()'

And others, i didn't know why, i create the .a file to gimpact and only the funcion btGImpactCollisionAlgorithm::registerAlgorithm show this linker error.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Problem with Collision

Post by Erwin Coumans »

Unfortunately, we cannot support build error caused by alternative build systems, such as dev-cpp.

We only support the included generic cmake build system or Visual Studio projectfiles,
Thanks,
Erwin
jcfalcone
Posts: 11
Joined: Mon Mar 03, 2008 12:09 pm

Re: Problem with Collision

Post by jcfalcone »

i know :D

but in visual C++ this don't work too.

the meshs only collide with simple objects like spheres and boxs.

with meshs and terrain don't have collision
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Problem with Collision

Post by Erwin Coumans »

Best is to try to get debug rendering up and running, so you can see what is wrong.

Alternatively, create a simulation snapshot, using BulletColladaExporter, and attached a zipped .dae file.

Code: Select all

#include "ColladaConverter.h"
and just after your stepSimulation:

Code: Select all

             m_physicsWorld->stepSimulation(TIMESTEP);

	static bool justOnce = true;
	if (justOnce)
	{
		justOnce = false;
		ColladaConverter* converter = new ColladaConverter(m_physicsWorld);
		converter->save("file:///c:/test.dae");
	}
Zip the file c:\test.dae and attach to this issue, so we can check it out,
Thanks,
Erwin
jcfalcone
Posts: 11
Joined: Mon Mar 03, 2008 12:09 pm

Re: Problem with Collision

Post by jcfalcone »

I see some demo codes and i found this in bullet btIDebugDraw and i'm trying to do this debug the bullet with irrlicht tools, to put here the error and others things.

I put this in my Mesh Class

Code: Select all

Engine::PhysicEngine::Instance()->Get_World()->setDebugDrawer(Engine::Debug::Instance());
      Engine::Debug::Instance()->setDebugMode(btIDebugDraw::DBG_ProfileTimings);
And create this debug class, but the bullet didn't call the debug functions....

Code: Select all

    #include <irrlicht.h>
    #include "LinearMath/btIDebugDraw.h"
    #include "global.h"
    #include "Receiver.Engine.h"
    #include "camera.class.h"
    
    // Irrlicht Namespaces
    
    using namespace irr;
    
    using namespace core;
    
    using namespace scene;
    
    using namespace video;
    
    using namespace io;
    
    using namespace gui;
    
    namespace Engine
    {
      class Debug : public btIDebugDraw
      {
      	    int m_debugMode;
      	    
      	    int $Sum_Num_Mesh,
      	        $Sum_Num_Terrain,
                $Sum_Num_Light;
      	    
      	    stringw $Intermediaria;
                
      	    bool $World_Light,
      	         $World_Physic;
      	         
         	gui::IGUIListBox            *$DebugList;
         	gui::IGUIEditBox            *$LightText,
                                        *$DriveUsed,
                                        *$PhysicWold,
                                        *$NumMesh,
                                        *$NumTerrain,
                                        *$NumLight,
                                        *$CamType,
                                        *$CamX,
                                        *$CamY,
                                        *$CamZ,
                                        *$Teste1,
                                        *$Teste2;
      	         
// 	        vector3df $Cam;
      	         
          public:
                 
            bool CheckDebugKeys(EKEY_CODE $Key);
            void SetDebugKeys();
                 
            bool $Enable;
                 
    	    void InitDebugWindow(void);
    	    void UpdateDebugWindow(void);
    	    void AddMesh();
            void AddTerrain();
    	    void AddMensage(const c8 *$Mensagem);
    	    void Set_Debug(bool $DebugMode){this->$Enable = $DebugMode;};
    	    bool Get_Debug(){return this->$Enable;};
         	
            static Debug* Instance()
            {
                  static Engine::Debug *instance = 0;

                  return instance ? instance : (instance = new Engine::Debug());
            }
            
                 
     	    Debug(void){};
    	    virtual ~Debug(void) {};
    	    void	drawLine(const btVector3& from,const btVector3& to,const btVector3& color) {};
    	    void	drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,btScalar distance,int lifeTime,const btVector3& color) 
            {
                   $GUIEnv->addStaticText(L"Hello World! This is the Irrlicht software engine!",
                                          rect<int>(10,10,200,30), true, true, 0, -1);
                this->$Intermediaria =  PointOnB.getX();
            	this->$Teste1->setText(this->$Intermediaria.c_str());
            };
    	    void	reportErrorWarning(const char* warningString) 
            {
                $GUIEnv->addStaticText(L"Hello World! This is the Irrlicht software engine!",
                                          rect<int>(10,10,200,30), true, true, 0, -1);
                this->$Intermediaria =  warningString;
            	this->$Teste1->setText(this->$Intermediaria.c_str());
            };
    	    void	draw3dText(const btVector3& location,const char* textString){};
    	
    	    int		getDebugMode() const { return m_debugMode; };
    	    void	setDebugMode(int debugMode) { m_debugMode = debugMode; };
      };
    }
jcfalcone
Posts: 11
Joined: Mon Mar 03, 2008 12:09 pm

Re: Problem with Collision

Post by jcfalcone »

And i try withe Collada but show this errors

Code: Select all

no matching function for call to `ColladaConverter::ColladaConverter(btDiscreteDynamicsWorld*)' 

candidates are: ColladaConverter::ColladaConverter(const ColladaConverter&) 

'class ColladaConverter' has no member named 'save' 
reltham
Posts: 66
Joined: Fri Oct 12, 2007 6:28 pm
Location: San Diego

Re: Problem with Collision

Post by reltham »

Just a comment on the debug draw stuff. It doesn't appear to ever do any debug drawing if you don't use Dynamics (i.e. CollisionWorld only).
jcfalcone
Posts: 11
Joined: Mon Mar 03, 2008 12:09 pm

Re: Problem with Collision

Post by jcfalcone »

Sorry to the sequence post, but now i can see some debug infos

with terrain the mesh don't have collision
Image

but with a box this have
Image

Edit1:I see here and don't have warning too
jcfalcone
Posts: 11
Joined: Mon Mar 03, 2008 12:09 pm

Re: Problem with Collision

Post by jcfalcone »

now i think the admin don't want to help me XD

but i study the gimpact engine and i put this to work.

But another problems:

Mesh Collide with Mesh
But with my terrain they collide and in the render the mesh cross the terrain, in my render code i get the Bullet position and update the Irrlicht position, i need help admin T_T

before collide with the terrain:
Image

After Collide with the terrain:
Image

you can see in the debug window the mesh position and the physic infos: the mesh collide but cross the terrain X(

The Render Code:

Code: Select all

Engine::Debug::Instance()->UpdatePhysicMeshPosition(TObject->getCenterOfMassPosition());

      ISceneNode *Node = static_cast<ISceneNode *>(TObject->getUserPointer());

	  // Position
	  btPoint3 Point = TObject->getCenterOfMassPosition();
	  Node->setPosition(vector3df((f32)Point[0], (f32)Point[1], (f32)Point[2]));
	
	  // Rotation
	  btVector3 EulerRotation;
	  btQuaternion RigidRotation = TObject->getOrientation();
	  Engine::PhysicEngine::Instance()->QuaternionToEuler(RigidRotation, EulerRotation);
	  Node->setRotation(vector3df(EulerRotation[0], EulerRotation[1], EulerRotation[2]));
The new Physic Terrain Code:

Code: Select all

       btTriangleMesh* $CollisionMesh = new btTriangleMesh();
       
   	   Engine::PhysicEngine::Instance()->ConvertCollisionMeshBuffer((IMeshBuffer *)(&$MeshBuffer), $CollisionMesh, this->$Scale);

       btGImpactMeshShape* gimpactShape = new btGImpactMeshShape($CollisionMesh);
       gimpactShape->updateBound();
       this->$Shape = gimpactShape;
    
       // Orientation
       btTransform $Transform;
       this->$MotionState = Engine::PhysicEngine::Instance()->GetBulletTransform($Transform,  btVector3(this->$TPosition.X, this->$TPosition.Y, this->$TPosition.Z));
      
       // Body
       this->$RigidBody = new btRigidBody(btRigidBody::btRigidBodyConstructionInfo(0.0f,this->$MotionState,this->$Shape));
                                          
       this->$RigidBody->setUserPointer((void *)(this->$Node));
      
       Engine::PhysicEngine::Instance()->Get_World()->addRigidBody(this->$RigidBody);
Edit1:Only to say the Position of the terrain is 0.0f,-800.0f,-300.0f and the scale is 40.0f,4.4f,40.0f
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Problem with Collision

Post by Erwin Coumans »

Please do not enable GIMPACT, it should work without first.

Did you already have time to try the 2 suggestions I asked for?

1) did you implement debug rendering, wireframe?

2) can you zip a .dae file, using the BulletColladaConverter?

We need 1 or 2 in order to help. We can't digg into source code.
Thanks,
Erwin
jcfalcone
Posts: 11
Joined: Mon Mar 03, 2008 12:09 pm

Re: Problem with Collision

Post by jcfalcone »

Its here i put collada to works too XD.

now you can help me
Attachments
test.zip
(1.22 KiB) Downloaded 286 times
jcfalcone
Posts: 11
Joined: Mon Mar 03, 2008 12:09 pm

Re: Problem with Collision

Post by jcfalcone »

Found some error?
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Problem with Collision

Post by Erwin Coumans »

There is only one object in the COLLADA file, a static box. Did you use GIMPACT shape?

Please implement debug rendering, just like all Bullet demos do, it should show the problem.

Unfortunately there are no further resources to help with your issue right now, you will have to do some work to locate the problem by comparing with other Bullet demos.
Hope that helps,
Erwin
Post Reply