
How to get XZ with mouse?[Solved]
-
- Gnome
- Posts: 324
- Joined: Sun May 11, 2008 9:27 pm
- x 20
How to get XZ with mouse?[Solved]
How can I get the XZ point the mouse is pointing at where intersection Y = 0. Just like if I wanted to place some object at the "floor". I'll show in a picture.


Last edited by mrmclovin on Wed Dec 03, 2008 9:15 pm, edited 1 time in total.
-
- Goblin
- Posts: 231
- Joined: Thu May 08, 2008 10:39 am
- Location: Barcelona, Spain
You can use a ray:
Where e is a mouseEvent object.
Then, with this ray you can calculate if it actually intersects the plane Y=0 with its member function:
The result pair means:
- bool: true if the ray intersects the plane
- Real: the distance along the ray where the intersection is located.
Then, if the intersection is true, you can use the function with the distance to get a Vector3 object with the coordinates of the intersection point, that will be in the form (X, 0, Z).
I hope this is what you need.
Regards.
Code: Select all
Ray mouseRay = mCamera->getCameraToViewportRay( e->getX(), e->getY() );
Then, with this ray you can calculate if it actually intersects the plane Y=0 with its member function:
Code: Select all
std::pair< bool, Real > intersects (const Plane &p) const
- bool: true if the ray intersects the plane
- Real: the distance along the ray where the intersection is located.
Then, if the intersection is true, you can use the function
Code: Select all
getPoint(Real d)
I hope this is what you need.
Regards.
-
- Gnome
- Posts: 324
- Joined: Sun May 11, 2008 9:27 pm
- x 20
-
- Gnome
- Posts: 324
- Joined: Sun May 11, 2008 9:27 pm
- x 20
-
- Goblin
- Posts: 231
- Joined: Thu May 08, 2008 10:39 am
- Location: Barcelona, Spain
Right.I have to create a Plane (obviously because intersects() takes one as param), but I do not attach a mesh to it?
Regarding spaces, I suppose getPoint returns coordinates in world space. Your scenenodes position will depend on their parent. If this parent is not in (0, 0, 0), your scenenodes will not appear in the right place.
-
- Gnome
- Posts: 324
- Joined: Sun May 11, 2008 9:27 pm
- x 20
The getPoint() returns all kind of funny positions. Even if I don't point at the floor it thinks that the mouse intersects ground floor. Perhaps I've not created the plane properly?
Here's the code when about to ray:
Code: Select all
Plane plane(Vector3::UNIT_Y, 0);
Ogre::MeshManager::getSingleton().createPlane("ground",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
plane,
1500,1500,
20,20,
true,
1,
1500,1500,
Vector3::UNIT_Z
);
Entity* ent = mSceneMgr->createEntity("Ground", "ground");
ent->setMaterialName("GameProject/GroundPlane");
mPlane = &plane;
Code: Select all
bool LevelEditor::mouseMoved(const OIS::MouseEvent &arg){
Ray mouseRay = mCamera->getCameraToViewportRay(arg.state.X.abs, arg.state.Y.abs);
std::pair<bool, Real> result = mouseRay.intersects((*mPlane));
if(result.first) {
Vector3 point = mouseRay.getPoint(result.second);
mCurrentObject->setPosition(point);
}
return true;
}
- volca
- Gnome
- Posts: 393
- Joined: Thu Dec 08, 2005 9:57 pm
- x 1
- Contact:
- volca
- Gnome
- Posts: 393
- Joined: Thu Dec 08, 2005 9:57 pm
- x 1
- Contact:
- volca
- Gnome
- Posts: 393
- Joined: Thu Dec 08, 2005 9:57 pm
- x 1
- Contact:
-
- Gnome
- Posts: 324
- Joined: Sun May 11, 2008 9:27 pm
- x 20
Sure! I thought this would be a good time to set up my first wiki page: http://www.ogre3d.org/wiki/index.php/Mouse_ray. I didnt have time to correct the code and spellning so feel free to edit! And I did'nt create a link from the HOWTO and Snippets content page until the code is confirmed working. Night!
-
- Gnome
- Posts: 324
- Joined: Sun May 11, 2008 9:27 pm
- x 20
Because, at least I think, relative won't tell you where the cursor actually is on the screen but tell you where the cursor is relative to its last position before it was moved. Another reason I used absolute is because the GUI I'm using (MyGUI), the mousecursor's position is injected by absolute values. That means I can be sure of that OIS and MyGUI are synchronized.
- Jdog
- Gnoblar
- Posts: 12
- Joined: Sun Apr 05, 2009 12:02 pm
- Location: Pretoria, South Africa
Re: How to get XZ with mouse?[Solved]
Help! My mouse cursor just seems to jump to the top-left corner and stays there. Why could this be?
My code is below - I'm doing this in the FrameStarted function, but when I tried to reproduce the code in MouseMoved, it cause the exact same effect.
I created the plane in another method and I believe this might be the culprit too (becuase well...I don't understand it!) But why would the mouse cursor jump to the top left??
I'm using myGUI and my mouse cursor moves around properly there (at least?)
Also, before I was using a pointer to the plane, as well as the following line:
... which created the node on the plane, but it was a bit off - not on the correct spot. Please help, I'm so frustrated right now! 
My code is below - I'm doing this in the FrameStarted function, but when I tried to reproduce the code in MouseMoved, it cause the exact same effect.
Code: Select all
OIS::MouseState mouseState = mMouse->getMouseState();
const MyGUI::IntPoint& mousePos = MyGUI::InputManager::getInstance().getMousePosition();
Ogre::Real offsetX = mouseState.X.abs / mRoot->getAutoCreatedWindow()->getWidth();
Ogre::Real offsetY = mouseState.Y.abs / mRoot->getAutoCreatedWindow()->getHeight();
Ray mouseRay = mCamera->getCameraToViewportRay(offsetX, offsetY);
std::pair<bool, Real> result = mouseRay.intersects(mPlane);
if (result.first)
{
Vector3 point = mouseRay.getPoint(result.second);
char1->getSightNode()->setPosition(point);
}
I'm using myGUI and my mouse cursor moves around properly there (at least?)
Also, before I was using a pointer to the plane, as well as the following line:
Code: Select all
Ray mouseRay = mCamera->getCameraToViewportRay(mousePos.left / float(mouseState.width) ,mousePos.top / float(mouseState.height) );

- Jdog
- Gnoblar
- Posts: 12
- Joined: Sun Apr 05, 2009 12:02 pm
- Location: Pretoria, South Africa
Re: How to get XZ with mouse?[Solved]
Fixed! The problem was solved by these two lines:
Code: Select all
Ogre::Real offsetX = float(mouseState.X.abs) / mRoot->getAutoCreatedWindow()->getWidth();
Ogre::Real offsetY = float(mouseState.Y.abs) / mRoot->getAutoCreatedWindow()->getHeight();
-
- Gnoblar
- Posts: 3
- Joined: Sun May 13, 2012 11:27 am
How to get XZ with mouse?[Solved]
Hi guys
I am very new to Ogre and have kind of been dropped in the deep end. (Time constraints)
So basically i made use of this code and it works perfectly.
The only problem is that for some reason it only works in the top left corner of the screen. My camera is at
The plane i am using is
I have tried setting the object's starting point to but this did not help. There are other things that i am confused about such as what is going to happen when i change the view but that i will learn when i get there.
Am i correct in assuming that if i set my camera to 250 on the Z axis i should be able to see negative 125 to positive 125 on both the x and the y axis?
Any help or pointers to what might be wrong will be much appreciated!
I am very new to Ogre and have kind of been dropped in the deep end. (Time constraints)
So basically i made use of this code and it works perfectly.
The only problem is that for some reason it only works in the top left corner of the screen. My camera is at
My object's starting point is0, 0, 250
If i click anywhere else in the screen the object moves to the bottom right corner of the block that’s in the top left corner, sorry of that sounds complicated or does not read well, but if i click in the block the object moves to my cursor like its meant to.50, 50, 50
The plane i am using is
Code: Select all
Ogre::Plane mPlane(Ogre::Vector3::UNIT_Y, 50);
and50, 0, 50
Code: Select all
Ogre::Plane mPlane(Ogre::Vector3::UNIT_Y, 0);
Am i correct in assuming that if i set my camera to 250 on the Z axis i should be able to see negative 125 to positive 125 on both the x and the y axis?
Any help or pointers to what might be wrong will be much appreciated!
-
- Gnoblar
- Posts: 3
- Joined: Sun May 13, 2012 11:27 am
Re: How to get XZ with mouse?[Solved]
Sorted it out.
Turns out it was because i was not using a mouse event, so the mouse object did not know how big my screen was. It defaults to 50.
So if anyone else is doing this the same way i was just add
after
Turns out it was because i was not using a mouse event, so the mouse object did not know how big my screen was. It defaults to 50.
So if anyone else is doing this the same way i was just add
Code: Select all
_Mouse->getMouseState().height = screenHeight;
_Mouse->getMouseState().width = screenWidth;
Code: Select all
Ogre::Real screenWidth = Ogre::Root::getSingleton().getAutoCreatedWindow()->getWidth();
Ogre::Real screenHeight = Ogre::Root::getSingleton().getAutoCreatedWindow()->getHeight();