Raycast with Mouse coords

__bf_ddc__

15-10-2007 14:54:31

Hello again.

I'm trying to port old code to new on mouse-picking in a 3D space. It was originally working rather similarly to what I've seen on the new samples. But for some reason it never hits.

Real x = (CEGUI::MouseCursor::getSingleton().getPosition().d_x / m_pWindow->getWidth());
Real y = (CEGUI::MouseCursor::getSingleton().getPosition().d_y / m_pWindow->getHeight());
Ray mouseRay = m_pCamera->getCameraToViewportRay(x, y);

m_pMouseRaycast = new RayCaster(mouseRay.getOrigin(), mouseRay.getDirection().normalisedCopy(), MAX_RAYCAST, RayCaster::RCT_CLOSEST, m_pScene);
m_pMouseRaycast->castShape(RayCaster::AF_NONE);

Actor *actor = m_pMouseRaycast->getClosestActor();
.
.
.


Does anything need to be initialised along with when the bodies are being constructed to be able to be picked? Or am I just doing something silly?

The old code was rather similar, in creating a Ray with the x&y coords of the current mouse [0-1floating point] then calling getCameraToViewportRay on the camera. And the old function createRayCaster pretty much takes the same params as the new, an origin, direction and length of raycast -- getClosestRaycastHit then usually netted a perfectly fine result.

Though now everything works fine, just after castShape() call it doesn't net any results and the mReport.count() is usually zero.

Thanks in advance. This will be the last part of converting to new code on my project. :P

__bf_ddc__

16-10-2007 17:44:41

Here's some test/debug info:

I have a camera hovering over a floorplan, and you click inside different rooms to go into that room...

When clicking into a room:

Ray mouseRay = m_pApplication->m_pFrameListener->m_pCamera2->getCameraToViewportRay(arg.state.X.rel / (float)arg.state.width, arg.state.Y.rel / (float)arg.state.height);

mouseRay:
...mOrigin:
......x = -773.19861
......y = 100.00004
......z = -579.89911
...mDirection:
......x = 0.00000
......y = -1.00000
......z = -6.0663744e-008

m_pMouseRaycast = new RayCaster(mouseRay.getOrigin(), mouseRay.getDirection().normalisedCopy(), MAX_RAYCAST, RayCaster::RCT_CLOSEST, m_pScene);

m_pMouseRaycast:
...mReport:
......size = 0
...etc.

Hope this helps a bit...this is driving me nuts.

betajaen

16-10-2007 17:55:58

Is mDirection = mDirection.normalisedCopy()? Because that's impossible if it is.

If it helps I've been playing around with RayCasting all day and it's worked fine; better I expected and remember.

__bf_ddc__

16-10-2007 18:05:27

Is mDirection = mDirection.normalisedCopy()? Because that's impossible if it is.

Yes. This is my RayCast creation, with the .normalisedCopy()
m_pMouseRaycast = new RayCaster(mouseRay.getOrigin(), mouseRay.getDirection().normalisedCopy(), MAX_RAYCAST, RayCaster::RCT_CLOSEST, m_pScene);

__bf_ddc__

16-10-2007 18:28:56

Another test:

Changed mouseRay to:
CEGUI::Point mousePos = CEGUI::MouseCursor::getSingleton().getPosition();
Ray mouseRay = m_pCamera->getCameraToViewportRay(mousePos.d_x / float(arg.state.width), mousePos.d_y / float(arg.state.height));
m_pMouseRaycast = new RayCaster(mouseRay.getOrigin(), mouseRay.getDirection().normalisedCopy(), MAX_RAYCAST, RayCaster::RCT_CLOSEST, m_pScene);


mousePos:
...d_x = 554
...d_y = 328

mouseRay:
...mOrigin:
......x = 63.426
......y = 100.000
......z = -84.568
...mDirection:
......x = 0.0
......y = -1.0
......z = -5.97

...looks a bit more promising, as far as the position and ray origins, but still not hitting any bodies and reporting contacts. I wish I had debug results of my old working code to see what values should have been spitting out...

Just a note, the camera is quite far above the scene, as it needs to show a full floorplan view of an entire house with all the rooms and their entities, so I'm not sure if these values are correct or not.

__bf_ddc__

16-10-2007 19:31:28

I can also verify there's bodies being created, as per the PhysX Remote Debugger:

(This is my scene, attached the camera to the debugger so you can see how high up it is to view it all as a whole)