How do I Get the X,Z Coordinate from the Mouse Position

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Fingers
Posts: 38
Joined: Thu Feb 10, 2005 10:17 am
Location: South Africa

How do I Get the X,Z Coordinate from the Mouse Position

Post by Fingers »

Hi All,

If I'm looking at a spot in space, how do I get the x,z coordinates of the intersection of the Mouse cursor on the Y-Plane set at 0??

Thx in Advance.
Guest

Post by Guest »

ehm, what??

get 2d mouse position:

Code: Select all

position2d<s32> position = yourCursorControl->getPosition();
get 3d mouse position:

Code: Select all

vector3df get3DCursorPosition(ITriangleSelector* TriangleSelector, ICursorControl* CursorControl, ISceneManager* SceneManager)
{
	vector3df	Result;
	triangle3df	Triangle;
	line3d<f32>	Ray;

	Ray = SceneManager->getSceneCollisionManager()->
		  getRayFromScreenCoordinates(CursorControl->getPosition(),
		  SceneManager->getActiveCamera());

	CollisionManager->getCollisionPoint(Ray, TriangleSelector, Result, Triangle);

	return Result;
}
i hope that can help you
Fingers
Posts: 38
Joined: Thu Feb 10, 2005 10:17 am
Location: South Africa

Post by Fingers »

well it sorta pointed me in the right direction, and after looking around the internet, I came up with this,

Code: Select all

bool Runner::OnEvent( SEvent event ){
	bool retValue = false;
	if( event.EventType == EET_MOUSE_INPUT_EVENT ){
		if( event.MouseInput.Event == EMIE_MOUSE_MOVED ) {
			ray = smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(
				position2d<s32>(event.MouseInput.X, event.MouseInput.Y) );
			point = lineIntersectWithPlane( ray, vector3df(0,0,0), vector3df(0,1,0) );
			retValue=true;
		}
	}

	return retValue;
}

vector3df Runner::lineIntersectWithPlane( line3d<f32> S, vector3df origin, vector3df Pn ) {
	vector3df u = S.end - S.start;
	vector3df w = S.start - origin;

    float D = Pn.dotProduct(u);
    float N = -Pn.dotProduct(w);

    if(fabs(D) < 0.00000001) {
        if (N == 0)
            return vector3df(0,0,0);
        else
            return vector3df(0,0,0);
    }
    float sI = N / D;
    if (sI < 0 || sI > 1)
        return vector3df(0,0,0);

	u.X *= sI;
	u.Y *= sI;
	u.Z *= sI;

	return S.start + u;
}
Which gives me the X,Z coordinates on the Y-Plane without having any scene nodes created yet.
Post Reply