Collision between Terrain and Mesh

Problems building or running the engine, queries about how to use features etc.
Post Reply
tomy14
Gnoblar
Posts: 9
Joined: Sat May 10, 2014 7:06 pm

Collision between Terrain and Mesh

Post by tomy14 »

Hi guys ,

I try to realize a collision system with some Ogre::Ray but it seems that it doesn't work ...

Here's is the code :

Code: Select all

Ogre::Vector3 robotPos = mRobotNode->getPosition();
Ogre::Ray robotRay(Ogre::Vector3(robotPos.x,5000.0f,robotPos.z),Ogre::Vector3::NEGATIVE_UNIT_Y);

mRaySceneQuery->setRay(robotRay);
mRaySceneQuery->setSortByDistance(false);

Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute();
Ogre::RaySceneQueryResult::iterator itr;

for(itr = result.begin();itr != result.end(); itr++)
{
    if(itr->worldFragment)
    {

        Ogre::Real terrainHeight = itr->worldFragment->singleIntersection.y;
        if(terrainHeight!=robotPos.y)
        {
            mRobotNode->setPosition(Ogre::Vector3(robotPos.x,terrainHeight,robotPos.z));
            mCameraNode->setPosition(Ogre::Vector3(robotPos.x,terrainHeight,robotPos.z));
            break;
        }
    }
}
I've tried also another code with the function getHeightAtWorldPosition and it works a little bit better but still not what i want to have as a result .

Code: Select all

Ogre::Vector3 robotPos = mRobotNode->getPosition();
	Ogre::Ray robotRay(Ogre::Vector3(robotPos.x,5000.0f,robotPos.z),Ogre::Vector3::NEGATIVE_UNIT_Y);

	mRaySceneQuery->setRay(robotRay);
	mRaySceneQuery->setSortByDistance(false);

	Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute();
	Ogre::RaySceneQueryResult::iterator itr = result.begin();

	if(itr != result.end() && itr->movable)
	{
		float terrainHeight = mTerrain->getHeightAtWorldPosition(Ogre::Vector3(robotPos.x,0.0f, robotPos.z));
			if(terrainHeight > robotPos.y)
			{
				mRobotNode->setPosition(robotPos.x,terrainHeight,robotPos.z);
				mCameraNode->setPosition(robotPos.x,terrainHeight,robotPos.z);

			}
	}
Image

This code is putting in the function : framerenderingqueued :)

but my mesh stays below the terrain

Thanks for help
tomy14
Gnoblar
Posts: 9
Joined: Sat May 10, 2014 7:06 pm

Re: Collision between Terrain and Mesh

Post by tomy14 »

up...:s
User avatar
Herb
Orc
Posts: 412
Joined: Thu Jun 04, 2009 3:21 am
Location: Kalamazoo,MI
x 38

Re: Collision between Terrain and Mesh

Post by Herb »

I can't tell what you're having trouble with exactly... The image shows the sinbad ogre mesh on the terrain, so seems to show it's okay.

I'm making the assumption that you mean the mesh is always "partly" below the terrain. This can happen as the entity's position from the scene node might not be at exactly the right height for the model. Usually you can just plug in an offset per model you use for that. So, you can just add the offset desired to the "y" value in your node set position call.
tomy14
Gnoblar
Posts: 9
Joined: Sat May 10, 2014 7:06 pm

Re: Collision between Terrain and Mesh

Post by tomy14 »

Thanks for reply but actually the picture doesn't show clearly the mesh is always at the same height and doesn't change when the terrain is going down or up . I dont know if you understand cause i just speak à little english sorry
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: Collision between Terrain and Mesh

Post by tod »

You must use getheight... when you move the character also. You cannot just set it once at the start.
tomy14
Gnoblar
Posts: 9
Joined: Sat May 10, 2014 7:06 pm

Re: Collision between Terrain and Mesh

Post by tomy14 »

I put this code in the function frameRenderingQueued so it should work at every frame isnt it ?
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: Collision between Terrain and Mesh

Post by tod »

Should work, but it wouldn't be very nice code. You should put it where you calculate the position for your character.
tomy14
Gnoblar
Posts: 9
Joined: Sat May 10, 2014 7:06 pm

Re: Collision between Terrain and Mesh

Post by tomy14 »

I dont have my code under my hands but i think i put the position of the mesh in createscene so it shouldnt work because createscene runs once time in the program . Framerenderingqueued should be the best solution no ?
tomy14
Gnoblar
Posts: 9
Joined: Sat May 10, 2014 7:06 pm

Re: Collision between Terrain and Mesh

Post by tomy14 »

up..
Post Reply