basic raycast question

sdragou

06-04-2006 09:06:07

A simple one.
Basic raycast returns the hits ordered by distance ?
GetFirstHit equals with GetInfoAt(0)?

How do I get the second Hit ?

sdragou

06-04-2006 15:34:37

While trying to figure out how to do this, I wrote the code below


OgreNewt::BasicRaycast* ray = new OgreNewt::BasicRaycast( SFL->mCamera->mWorld, start, end );
int hits=ray->getHitCount();
if (hits!=0)
{
OgreNewt::BasicRaycast::BasicRaycastInfo info = ray->getInfoAt(0);
OgreNewt::BasicRaycast::BasicRaycastInfo info_temp = ray->getInfoAt(0);
for(int i=0;i<hits;i++){
info_temp = ray->getInfoAt(i);
if(info_temp.mBody->getType()!=5){
if(info_temp.mDistance<info.mDistance) info=info_temp;
}
}

if(info.mBody->getType()==1){
CEGUI::Imageset *test=CEGUI::ImagesetManager::getSingletonPtr()->getImageset((CEGUI::utf8*)"TaharezLook");

CEGUI::Image *tt=new CEGUI::Image( test, (CEGUI::utf8*)"MouseTest",CEGUI::Rect(5,147,22,164),CEGUI::Point(CEGUI::Vector2(-8,-8)));

CEGUI::MouseCursor::getSingletonPtr()->setImage(tt);
//break;
}else{
CEGUI::MouseCursor::getSingletonPtr()->setImage((CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseTarget");
}
}


The objects have type=1 and the cylinder in which the camera is in has type=5. The walls have type=2.
If a ray hits an object and it has the smallest distance (except camera cylinder) the cursor turns green, otherwise it turns grey.

But there is something wrong happening.
Although the ray hits the object the cursor doesn't change and getHitCount() returns zero although it should be grater than 1 (since it always hits the camera cylinder)
A tiny movement of the cursor and everything is ok

see screeshots 1 and 2


and

OvermindDL1

07-04-2006 01:20:14

If you are using the Newton debug lines funcionality, hold F3 and see if it is pointing at the collision body and not just to the left of it. It may not necessarily match the bodies position depending on how it is placed.

sdragou

07-04-2006 09:12:13

I have used the debugger and the cursor is pointing at the collision body for sure.

check the following screenshots.

The number you see in the debugtext is the HitCount().
the vertical lines are the edges from the cylinder body of the camera.



OvermindDL1

07-04-2006 23:42:18

Perhaps the mouse location to 3d-ray conversion is offsetting it. What is the code for that?

pfo

08-04-2006 09:17:11

A simple one.
Basic raycast returns the hits ordered by distance ?
GetFirstHit equals with GetInfoAt(0)?
Not quite, there's a parameter in the raycast function for a callback. Newton shoots the ray into the scene and calls the callback with the results of each intersection with a body. It is up to you to filter these results in whatever way you want. A common way to do this is to pick the closest object. Take a look at the SDK examples as most of them do this. If you wanted to get more than one object, you could build a data structure that would order bodies returned from a raycast in the order they were hit.

sdragou

09-04-2006 19:10:34

Perhaps the mouse location to 3d-ray conversion is offsetting it. What is the code for that?

If I undestand this correctly you mean that the ray may not go through the mouse pointer but a bit near it..

For the creation of this ray I use Ogre's camera to viewport ray and CEGUI's getPosition()
here is the code
CEGUI::System::getSingleton().injectMouseButtonDown(
convertOgreButtonToCegui(e->getButtonID()));
Ogre::Vector3 start, end;
CEGUI::Point mouse = CEGUI::MouseCursor::getSingleton().getPosition();
CEGUI::Renderer* rend = CEGUI::System::getSingleton().getRenderer();
Ogre::Real mx,my;
mx = mouse.d_x / rend->getWidth();
my = mouse.d_y / rend->getHeight();
Ogre::Ray camray = SFL->mCamera->mCamera->getCameraToViewportRay(mx,my);
start = camray.getOrigin();
end = camray.getPoint( 900.0 );