Sample_Terrain with Physics/Collision

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
Post Reply
umitum
Gnoblar
Posts: 12
Joined: Mon Dec 02, 2013 1:42 pm

Sample_Terrain with Physics/Collision

Post by umitum »

Hi All,
I am new to this Ogre3d and trying to integrate Sample_Terrain and Physics..
when i try integrating sample_terrain and physics,
I can see the physics body of the terrain but it is flat body ( i mean at elevation part the physics body is not elevated ).
Added code is given below

Code: Select all


		// setup the terrain physics:
		Vector3 terrainScale( TERRAIN_WORLD_SIZE / ( TERRAIN_SIZE - 1 ), 1, TERRAIN_WORLD_SIZE / ( TERRAIN_SIZE - 1 ) );

		float* terrainData = mTerrainGroup->getTerrain( 0, 0 )->getHeightData();
		float* heights = new float[ TERRAIN_SIZE * TERRAIN_SIZE ];
		// The following works.
		// At least boxes stop at this point but keep shaking...
		for ( int i = 0; i < (TERRAIN_SIZE-1) * (TERRAIN_SIZE-1); i++ )
			terrainData[ i ] = terrainData[ i ] / 128.0f;

		// Fix the terrain physics alignment:
		for ( int i = 0; i < TERRAIN_SIZE; i++ )
		{
			memcpy( heights + TERRAIN_SIZE * i, terrainData + TERRAIN_SIZE * (TERRAIN_SIZE-i-1), sizeof(float) * TERRAIN_SIZE );
		}
		// create a terrain shape from a HeightmapCollisionShape
		OgreBulletCollisions::HeightmapCollisionShape*  terrainShape = new OgreBulletCollisions::HeightmapCollisionShape(
			TERRAIN_SIZE,
			TERRAIN_SIZE,
			terrainScale,
			heights,
			true
			);
		//Create a body for the physics world
		OgreBulletDynamics::RigidBody*  terrainBody = new OgreBulletDynamics::RigidBody( "Terrain", mWorld );

		const float   terrainBodyRestitution   = 0.1f;
		const float   terrainBodyFriction      = 0.8f;

		Ogre::SceneNode* pTerrainNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
		terrainBody->setStaticShape(
			pTerrainNode,
			terrainShape,
			terrainBodyRestitution,
			terrainBodyFriction,
			mTerrainGroup->getTerrain( 0, 0 )->getPosition()
			);
		mBodies.push_back( terrainBody );
		mShapes.push_back( terrainShape );
#define TERRAIN_WORLD_SIZE 12000.0f
#define TERRAIN_SIZE 513

hope you can understand my problem from attached file.( i am unable to apply physics at the elevation parts of the Terrain).
At Yellow colour ,physics is fine ,where as at red colour circled place physics is not elevated.

Anybody suggest me where i made mistake. or am i need to do anything else.

Thanks and Regards
Umitum.
Attachments
TerrainPhysics.png
TerrainPhysics.png (193.43 KiB) Viewed 879 times
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: Sample_Terrain with Physics/Collision

Post by tod »

I don't use bullet, but you should check the documentation for the heightmap collision, maybe it expects something else. In newton for example, height data is unsigned short, if I remember correctly. Also I see you divide the heights by 128, are you sure this is ok?
umitum
Gnoblar
Posts: 12
Joined: Mon Dec 02, 2013 1:42 pm

Re: Sample_Terrain with Physics/Collision

Post by umitum »

Hi Tod,
thanks for your reply,

Yes you are right, i removed dividing with 128.
Now i can able to get it.

How can i update this physicsBody during elevation and depression. For Sure it will not update automatically for this code.where and How should i update this physics body with respect to elevation and depression?


Regards
umitum
Attachments
Terrain.png
Terrain.png (198.33 KiB) Viewed 789 times
umitum
Gnoblar
Posts: 12
Joined: Mon Dec 02, 2013 1:42 pm

Re: Sample_Terrain with Physics/Collision

Post by umitum »

Hi ,
i have updated Physics to deformation terrain and introduced Sinbad with Physics,i have used

BulletCollision :BoxCollisionShape
BulletDynamics :RigidBody

Code: Select all

Vector3 position = (mCamera->getDerivedPosition() + mCamera->getDerivedDirection().normalisedCopy() * 10);

				OgreBulletCollisions::BoxCollisionShape *sceneBoxShape = new OgreBulletCollisions::BoxCollisionShape(size);
				// and the Bullet rigid body
				OgreBulletDynamics::RigidBody *defaultBody = new OgreBulletDynamics::RigidBody(
					"defaultBoxRigid" + StringConverter::toString(mNumEntitiesInstanced), 
					mWorld);
				defaultBody->setShape(	mBodyNode,//node,
					sceneBoxShape,
					0.6f,			// dynamic body restitution
					0.6f,			// dynamic body friction
					1.0f, 			// dynamic bodymass
					position,		// starting position of the box
					Quaternion(0,0,1,0));//Quaternion(0,0,0,1));// orientation of the box
				mNumEntitiesInstanced++;				

				defaultBody->setLinearVelocity(
					mCamera->getDerivedDirection().normalisedCopy() * 7.0f ); // shooting speed
				defaultBody->getBulletRigidBody()->setAngularFactor(btVector3(0.0f,0.0f,0.0f));//0.0f,1.0f,0.0f)
				// push the created objects to the dequese
				mShapes.push_back(sceneBoxShape);
				mBodies.push_back(defaultBody);		
you can find terrain Physics code in above post.

i can see the physics body by enabling the debugdrawer but unable to detect collision between terrain and Character.....

Am i doing any mistake?
Am I need to include anything?'
Iam attaching the sample file also
Attachments
Untitled3.png
Untitled3.png (178.19 KiB) Viewed 738 times
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: Sample_Terrain with Physics/Collision

Post by tod »

umitum wrote:but unable to detect collision between terrain and Character.....
What do you mean by that? Does the body move through the terrain? Or are you expecting some kind of collision notification?
From the image I see the body is floating, I think you should apply a gravity force to it. Also, as I understand from Newton Dynamics, it is best to use forces instead of velocities, which is pretty logical as the velocity is an effect, not the cause, so it make physics behave less realistic.
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: Sample_Terrain with Physics/Collision

Post by tod »

Also, don't use boxes for movable bodies, use something with rounded corners, like an ellipsoid or capsule.
umitum
Gnoblar
Posts: 12
Joined: Mon Dec 02, 2013 1:42 pm

Re: Sample_Terrain with Physics/Collision

Post by umitum »

Hi tod,

Thanks for your prompt response,

what i mean by collision is
when we apply physics to both terrain and character .whenever we introduced a character it should fall down on terrain right?
but it is not falling on terrain, it is floating in the air and character is moving through the Terrain at elevation part of the terrain.

As you mentioned, i will try with applying gravity and i will let you know..
As time being iam using collision shape as box ,for next time i will use ellipsoid or capsule.


Regards
umitum
Post Reply