[Solved] Raycasting - weird behavior

gbisocoli

04-08-2013 03:35:26

Hi, I have a problem with raycasting, I'm using OgreSDK_vc10_v1-8-0, NxOgre + Critter: 1.7.x Buggy Swires Branch, this is the code:

bool InGame::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
[...]
if (id == OIS::MB_Left)
{
mLMouseDown = true;

Ogre::Ray ogre_ray = Soccer::Instance().getCamera()->getCameraToViewportRay(evt.state.X.abs, evt.state.Y.abs);

NxOgre::Ray ray;
ray.mDirection.from(ogre_ray.getDirection());
ray.mOrigin.from(ogre_ray.getOrigin());

RaycastHit hit = mNxOgreScene->raycastClosestShape(ray, NxOgre::Enums::ShapesType_All); // ShapesType_All o ShapesType_Dynamic?

if (hit.mRigidBody)
{
std::string name = hit.mRigidBody->getName();
if (name.length())
mHitResultText->setCaption("Hit '" + hit.mRigidBody->getName() + "' at " + hit.mWorldImpact.to_s());
else
mHitResultText->setCaption("Hit something at " + hit.mWorldImpact.to_s());

}
else
{
mHitResultText->setCaption("Didn't find anything.");
}
} // if

// Right mouse button
[...]
return true;
}

This is what happens:



I already checked that the mouse is at the same position as the cursor shown and some other things, I really don't know what else to do, any ideas?

SniperBinaire

26-08-2013 11:27:07

try to check that your shapes are really where you believe they are :

NxOgre::VisualDebuggerDescription desc;
desc.mCollision.shapes = true;


maybe they are displaced.

gbisocoli

03-09-2013 21:55:17

No, that's not it, the VisualDebugger was showing shapes and they are correct, it's really weird.

I placed a Debug point at the line "if (hit.mRigidBody)" and it never enters to the if, meaning it never hits a rigidbody EXCEPT when the rigidbody is at pixel 0x0, it's F%#$&$ weird!!

I tried lots of things but I really have no idea what it can be the problem, maybe NxOgre's raycasting doesn't work propertly with Ogre v1.8? I'd appreciate any help, if you need more info about the program to help please ask.

One other thing: I've been trying Ogre's Raycasting but it only returns "CritterDebugWindow" (or something similar) and "MatchCameraNode" and nothing else (even though I clicked an actor), this is the code:

mRayScnQuery->setRay(ogre_ray);
mRayScnQuery->setSortByDistance(true);

Ogre::RaySceneQueryResult& result = mRayScnQuery->execute();
Ogre::RaySceneQueryResult::iterator iter = result.begin();

while ( iter != result.end() )
{
if ( iter->movable ) // && (iter->movable->getName() != "PlayerCam")
{
if ( iter->movable->getParentSceneNode()->getName() == Ball::Instance().getBody()->getNode()->getSceneNode()->getName() )
{
mHitResultText->setCaption("Ball Clicked");
}
else
{
Ogre::String sub= iter->movable->getParentSceneNode()->getName().substr(0,6);
if ( sub=="Player" )
{
mHitResultText->setCaption("Some Player Clicked.");
}
}
}
iter++;
}


Since the actors are handled by Critter I think it's not a good idea to use Ogre's Raycasting, maybe t's not finding any movable because of Critter's way of creating the actors so I want to use NxOgre's Raycasting but it's not working... should I try PhysX Raycasting directly? (If there exists such thing)

insider

10-09-2013 18:12:52

Hi, I have a problem with raycasting, I'm using OgreSDK_vc10_v1-8-0, NxOgre + Critter: 1.7.x Buggy Swires Branch, this is the code:

bool InGame::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
[...]
if (id == OIS::MB_Left)
{
mLMouseDown = true;

Ogre::Ray ogre_ray = Soccer::Instance().getCamera()->getCameraToViewportRay(evt.state.X.abs, evt.state.Y.abs);

NxOgre::Ray ray;
ray.mDirection.from(ogre_ray.getDirection());
ray.mOrigin.from(ogre_ray.getOrigin());

RaycastHit hit = mNxOgreScene->raycastClosestShape(ray, NxOgre::Enums::ShapesType_All); // ShapesType_All o ShapesType_Dynamic?

if (hit.mRigidBody)
{
std::string name = hit.mRigidBody->getName();
if (name.length())
mHitResultText->setCaption("Hit '" + hit.mRigidBody->getName() + "' at " + hit.mWorldImpact.to_s());
else
mHitResultText->setCaption("Hit something at " + hit.mWorldImpact.to_s());

}
else
{
mHitResultText->setCaption("Didn't find anything.");
}
} // if

// Right mouse button
[...]
return true;
}



Try if(hit.mRigidBody && hit.mWorldImpact.y>0) and see if that helps. :)
NxOgre raycasting works perfectly with Ogre 1.8, I have used it heavily and there seem to be no problems.
Note: Are you resizing the window before the mouse click, try running in full screen and see if the problem still persists.

gbisocoli

26-09-2013 19:31:11

So the problem was related to Ogre not NxOgre, it was simple, getCameraToViewportRay() was expecting values between 0 and 1 and I was giving it pixels :mrgreen: ... here is the Ogre thread.

so I changed:
Ogre::Ray ogre_ray = Soccer::Instance().getCamera()->getCameraToViewportRay( evt.state.X.abs, evt.state.Y.abs );
to:
Ogre::Real x = Ogre::Real(evt.state.X.abs) / Ogre::Real(evt.state.width);
Ogre::Real y = Ogre::Real(evt.state.Y.abs) / Ogre::Real(evt.state.height);
Ogre::Ray ogre_ray = Soccer::Instance().getCamera()->getCameraToViewportRay( x, y );

:D