How to get XZ with mouse?[Solved]

Problems building or running the engine, queries about how to use features etc.
Post Reply
mrmclovin
Goblin
Posts: 287
Joined: Sun May 11, 2008 9:27 pm
x 7

How to get XZ with mouse?[Solved]

Post by mrmclovin » Tue Dec 02, 2008 9:36 pm

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.

Image
Last edited by mrmclovin on Wed Dec 03, 2008 9:15 pm, edited 1 time in total.
0 x

milacao
Goblin
Posts: 231
Joined: Thu May 08, 2008 10:39 am
Location: Barcelona, Spain

Post by milacao » Wed Dec 03, 2008 9:23 am

You can use a ray:

Code: Select all

Ray mouseRay = mCamera->getCameraToViewportRay( e->getX(), e->getY() ); 
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:

Code: Select all

std::pair< bool, Real > 	intersects (const Plane &p) const 
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

Code: Select all

getPoint(Real d)
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.
0 x

mrmclovin
Goblin
Posts: 287
Joined: Sun May 11, 2008 9:27 pm
x 7

Post by mrmclovin » Wed Dec 03, 2008 11:14 am

Hey, thanks, that should probably do it! I have a question though. In this case I have a Plane, but what if I didnt? Lets say I'd want to place an object in empty space (no Planes, terrain and so on) on Y = 0 level. Is that not possible?
0 x

milacao
Goblin
Posts: 231
Joined: Thu May 08, 2008 10:39 am
Location: Barcelona, Spain

Post by milacao » Wed Dec 03, 2008 11:19 am

That's the same. You can do the same operations although the plane is not "physically" there. You can just "imagine" it.
0 x

mrmclovin
Goblin
Posts: 287
Joined: Sun May 11, 2008 9:27 pm
x 7

Post by mrmclovin » Wed Dec 03, 2008 11:48 am

Okey, so you mean that I have to create a Plane (obviously because intersects() takes one as param), but I do not attach a mesh to it?

Another question:
It seems that my scenenodes dont get postioned where the mouse intersect. In what vectorspace does getPoint() return, the camera's?
0 x

milacao
Goblin
Posts: 231
Joined: Thu May 08, 2008 10:39 am
Location: Barcelona, Spain

Post by milacao » Wed Dec 03, 2008 12:23 pm

I have to create a Plane (obviously because intersects() takes one as param), but I do not attach a mesh to it?
Right.

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.
0 x

mrmclovin
Goblin
Posts: 287
Joined: Sun May 11, 2008 9:27 pm
x 7

Post by mrmclovin » Wed Dec 03, 2008 2:14 pm

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?

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;
Here's the code when about to ray:

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;
		}
0 x

User avatar
volca
Gnome
Posts: 393
Joined: Thu Dec 08, 2005 9:57 pm
Contact:

Post by volca » Wed Dec 03, 2008 2:23 pm

Maybe the mPlane pointer is to a local variable? Then if you use it is is already pointing to invalid data? Try storing Plane mPlane; instead of Plane* mPlane; - you get the point...
0 x
Image

mrmclovin
Goblin
Posts: 287
Joined: Sun May 11, 2008 9:27 pm
x 7

Post by mrmclovin » Wed Dec 03, 2008 2:55 pm

Oh, you've got a point there.. Ehm, how should I do then? The plane is created in the constructor ! Should I create a new plane every time mouse is moved?

[edit]Of course. Store a Plane mPlane instead.:oops: [/edit]
0 x

mrmclovin
Goblin
Posts: 287
Joined: Sun May 11, 2008 9:27 pm
x 7

Post by mrmclovin » Wed Dec 03, 2008 3:03 pm

Okey, fix the mPlane now. Should be valid. My object are placed on the ground not in where the mouse is pointing? More hints ..?
0 x

User avatar
volca
Gnome
Posts: 393
Joined: Thu Dec 08, 2005 9:57 pm
Contact:

Post by volca » Wed Dec 03, 2008 3:13 pm

Hmm. The arg.state.X.abs/Y is in pixels, and getCameraToViewportRay needs 0-1 range for on-screen. Normalising to 0-1 is needed. May not solve the problem, but should be done. :)
0 x
Image

mrmclovin
Goblin
Posts: 287
Joined: Sun May 11, 2008 9:27 pm
x 7

Post by mrmclovin » Wed Dec 03, 2008 6:19 pm

Yeah, that did the trick. Thanks! :D
0 x

User avatar
volca
Gnome
Posts: 393
Joined: Thu Dec 08, 2005 9:57 pm
Contact:

Post by volca » Wed Dec 03, 2008 6:21 pm

You're welcome!
0 x
Image

User avatar
xadhoom
Minaton
Posts: 973
Joined: Fri Dec 28, 2007 4:35 pm
Location: Germany

Post by xadhoom » Wed Dec 03, 2008 9:56 pm

mrmclovin: May I ask you for the resulting code snippet? Maybe this would be a nice piece for the Wiki.
0 x

mrmclovin
Goblin
Posts: 287
Joined: Sun May 11, 2008 9:27 pm
x 7

Post by mrmclovin » Thu Dec 04, 2008 3:09 am

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!
0 x

User avatar
xadhoom
Minaton
Posts: 973
Joined: Fri Dec 28, 2007 4:35 pm
Location: Germany

Post by xadhoom » Thu Dec 04, 2008 9:51 am

Thanks! :P

BTW: Why did you divide the absolute cursor position by screen size? Can´t you just use state.X.rel ? (One comment in OIS indicates that relative values do not work with a joystick )
0 x

mrmclovin
Goblin
Posts: 287
Joined: Sun May 11, 2008 9:27 pm
x 7

Post by mrmclovin » Thu Dec 04, 2008 11:19 am

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.
0 x

User avatar
xadhoom
Minaton
Posts: 973
Joined: Fri Dec 28, 2007 4:35 pm
Location: Germany

Post by xadhoom » Thu Dec 04, 2008 5:38 pm

Got it! :P
0 x

User avatar
Jdog
Gnoblar
Posts: 12
Joined: Sun Apr 05, 2009 12:02 pm
Location: Pretoria, South Africa

Re: How to get XZ with mouse?[Solved]

Post by Jdog » Tue Jun 30, 2009 10:32 pm

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.

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 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:

Code: Select all

Ray mouseRay = mCamera->getCameraToViewportRay(mousePos.left / float(mouseState.width) ,mousePos.top / float(mouseState.height) );
... 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! :(
0 x

User avatar
Jdog
Gnoblar
Posts: 12
Joined: Sun Apr 05, 2009 12:02 pm
Location: Pretoria, South Africa

Re: How to get XZ with mouse?[Solved]

Post by Jdog » Wed Jul 01, 2009 10:09 am

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();
0 x

Russel
Gnoblar
Posts: 3
Joined: Sun May 13, 2012 11:27 am

How to get XZ with mouse?[Solved]

Post by Russel » Mon May 14, 2012 3:40 pm

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
0, 0, 250
My object's starting point is
50, 50, 50
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.
The plane i am using is

Code: Select all

Ogre::Plane mPlane(Ogre::Vector3::UNIT_Y, 50);
I have tried setting the object's starting point to
50, 0, 50
and

Code: Select all

Ogre::Plane mPlane(Ogre::Vector3::UNIT_Y, 0); 
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!
0 x

Russel
Gnoblar
Posts: 3
Joined: Sun May 13, 2012 11:27 am

Re: How to get XZ with mouse?[Solved]

Post by Russel » Tue May 15, 2012 10:21 am

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

Code: Select all

_Mouse->getMouseState().height = screenHeight;
_Mouse->getMouseState().width = screenWidth;
after

Code: Select all

Ogre::Real screenWidth = Ogre::Root::getSingleton().getAutoCreatedWindow()->getWidth();
Ogre::Real screenHeight = Ogre::Root::getSingleton().getAutoCreatedWindow()->getHeight();
0 x

Post Reply