[SOLVED]Help - Need Camera to stick to terrain!

abhinavxrai

21-03-2006 11:34:19

In the Ogre demos, there is a demo called Terrain. The code implemented in that demo for the camera to stick to the terrain is quite simple.


static Ray updateRay;
updateRay.setOrigin(mMainNode->getPosition());
updateRay.setDirection(Vector3::NEGATIVE_UNIT_Y);
raySceneQuery->setRay(updateRay);
RaySceneQueryResult& qryResult = raySceneQuery->execute();
RaySceneQueryResult::iterator i = qryResult.begin();

if (i != qryResult.end() && i->movable)
{
SceneQuery::WorldFragment* wf = i->worldFragment;
mCamera->setPosition(mCamera->getPosition().x, i->worldFragment->singleIntersection.y+10, mCamera->getPosition().z);
}


Now, I want to do exactly the same with more precision using RayCast method in OgreNewt

The major difference is that now my terrain is not a cfg file but a mesh file. How can this be achieved.

I have written the following code, but it doesn't seem to work. Any suggestions would be awesome!


Ogre::Vector3 start = mCamera->getPosition();
Ogre::Vector3 end = mCamera->getPosition() + (100 * Ogre::Vector3::NEGATIVE_UNIT_Y);

OgreNewt::BasicRaycast* ray = new OgreNewt::BasicRaycast(m_World, start, end);
OgreNewt::BasicRaycast::BasicRaycastInfo info = ray->getFirstHit();

Ogre::Vector3 point = start + ( (end-start) * info.mDistance);

if(info.mDistance != 0.015)
{
mCamera->setPosition(point.x, point.y+10, point.z)
mWindow->setDebugText("Something seems to be working");
}


This code doesnt work as required! Any suggestions where I am going wrong or any implementation error. I think there might be a problem with local and global points, etc.

Thanks for your help!

walaber

21-03-2006 17:35:29

if(info.mDistance != 0.015)

what are you trying to do with this if statement here?

abhinavxrai

22-03-2006 05:30:38

Hi Walaber

Well I am trying to cast a ray from the camera going vertically downward. And then through my statement

if(info.mDistance ! = 0.015)

I am trying to check if the camera is at the required position or not. That is 15 units from the terrain (or maybe it should be 0.15). But none of it seems to work.

I tried changing the if statements. If the distance is less than 0.015 then only move the camera upward... etc. All fails.

Im just trying to keep the camera stuck to the terrain at a constant height. And that too using rayCast. Any other methods that could come in handy? Or how to implement rayCast effectively?

abhinavxrai

22-03-2006 10:00:38

This is a edit to everything! Thanks walaber for your help - but I solved the problem.

I was using the Ray Casting example from the Ogre Newt and I figured I was using the wrong node to move the camera. From mCamera I changed it to msnCam and everything seemed to work just a-ok.

Also, I used the line3d class to draw the ray lines... and everything was perfect. Using RayCast I am also detecting other mesh bodies present! Its exactly what I needed.