Ced666 wrote:Strong99, you are wrong. First you need to convert the screen coordinate (where the mouse cursor is) to a ray (starting from the screen and going out of the screen) using ISceneCollisionManager::GetRayFromScreenCoordinates. Then, getting the triangle selector from your terrain (using ISceneManager::createTerrainTriangleSelector) and then getting the collision point between the two by using IColisionManager::getColisionPoint.
HantaCore was right because the 2D mouse coordinates are not equal to a 2D position on your terrain (they correspond only if your camera is above your terrain and is looking perfectly vertically at it, and the orientaion of the camera must be zero also).
You are entirely right, there's just a little problem : we are working with Irrlicht .NET. and it's not that easy. I suppose you are working with C# if not, it's exactly the same way (if you are working with C++, switch to native Irrlicht
).
First, in C# .NET, bool GetCollisionPoint(Core::Line3D ray,
Scene::ITriangleSelector* selector,
[PARAMFLAG::Out] Core::Vector3D& outCollisionPoint,
[PARAMFLAG::Out] Core::Triangle3D& outTriangle);
does not work... Don't know why, probably the Out param wich fails, I'm not familiar enough with C++.NET... Thus you can add a very simple function in "public __gc class ISceneCollisionManager" :
Code: Select all
Core::Vector3D GetCollisionPoint(Core::Line3D ray,
Scene::ITriangleSelector* selector)
{
Core::Vector3D vector;
irr::core::vector3df outCP;
irr::core::triangle3df outTr;
bool ret = SCM->getCollisionPoint(
irr::NativeConverter::getNativeLine(ray),
selector ? selector->get_NativeTriangleSelector() : 0,
outCP,
outTr);
vector = (irr::NativeConverter::getNETVector(outCP));
return vector;
};
Then you need to create the terrain (we'll name it "node"...).
Then you create the terrain selector :
Code: Select all
ITriangleSelector sel = SceneManager.CreateTerrainTriangleSelector(node, 0); //With a huge terrain, 0 can be very slow (depending on patch size).
Then when the player clicks on the ground you just have to do :
Code: Select all
Line3D line = SceneManager.SceneCollisionManager.GetRayFromScreenCoordinates(Device.CursorControl.Position, null);
Vector3D y = SceneManager.SceneCollisionManager.GetCollisionPoint(line, sel);
And y contains the place where the user clicked !