I want to have a mouse that when I click somewhere on the screen, I need a way to figure out where in the 3d world they clicked. Is there an easy way of doing this? or some tutorial that already exsists?
Help is appreciated.
Oh, and unlike the tutorial thats included, the camera is independent of mouse movement.
Mouse location to 3d
get the cursor location on screen. There's than a function to get a 3d ray from a 2d location. Consult ISceneCollisionManager documentation
You do a lot of programming? Really? I try to get some in, but the debugging keeps me pretty busy.
Crucible of Stars
Crucible of Stars
-
WalterWzK
Well i was figuring this one out myself a couple of days ago.
The point is: im using .NET irrlicht so i have little irrlicht funtions to use.
What I did was hooking the cursor and get its X and Y when pressed.
After you have these you can 'create' a virtual point in the 3D Envoirment and set its position like a 3DVector. Than compare the position of the virtual point (wich is virtual cuz i dont really let people see m) to all possible hits in the 3D world, cuz ur missing a third position (height we have, width we have but no depth). After this i looped trough the results and assume that the object wich has the most hits in this test (crossed the most) is the object the user would like to 'select'.
The point is: im using .NET irrlicht so i have little irrlicht funtions to use.
What I did was hooking the cursor and get its X and Y when pressed.
After you have these you can 'create' a virtual point in the 3D Envoirment and set its position like a 3DVector. Than compare the position of the virtual point (wich is virtual cuz i dont really let people see m) to all possible hits in the 3D world, cuz ur missing a third position (height we have, width we have but no depth). After this i looped trough the results and assume that the object wich has the most hits in this test (crossed the most) is the object the user would like to 'select'.
-
Guest
Code: Select all
vector3df target;
vector3df location;
triangle3df triangle;
line3d<f32> raytrace;
ITriangleSelector* selector;
ISceneCollisionManager* colmgr;
Cursor_Position = cursor->getPosition();
raytrace = colmgr->getRayFromScreenCoordinates(Cursor_Position, camera);
colmgr->getCollisionPoint(raytrace, selector, target, triangle);
let me explain:
first you have to setup a triangleselector named selector.
1. get the cursor coordinates
2. get the position in 3d space from Cursor_Position to "camera" (replace your camera name)
3. the collisionmanager gets the point on the terrain, ground etc. so that the point is not placed in the air.
now you can accsess this point via "target" (its vector3df), example:
playernode->setPosition(target);
hope that could help you
EDIT:
if you want to move the camera with the mouse, look into this (but i wont explain the code for now, should be quite easy and selfexplaining):
Code: Select all
IAnimatedMeshSceneNode* player;
ICameraSceneNode* camera;
ITriangleSelector* selector;
vector3df Camera_Position;
vector3df Camera_Target;
vector3df Player_Position;
position2d<s32> Cursor_Position;
position2d<s32> ScreenResolution;
float CameraSpeed;
void createCamera()
{
camera = smgr->addCameraSceneNode(0, vector3df(1000,1000,1000), vector3df(1000,1000,1000));
camera->setFOV(90);
camera->setFarValue(14000.0f);
driver->setFog(SColor(0, 150, 120, 100), true, 2000, 20000);
player = smgr->addAnimatedMeshSceneNode(smgr->getMesh("data/models/dummy.x"));
player->setPosition(vector3df(2000,500,2000));
player->setVisible(false);
selector = smgr->createOctTreeTriangleSelector(
terrain->getMesh(0), terrainnode, 64);
terrainnode->setTriangleSelector(selector);
selector->drop();
CameraSpeed = 15;
ScreenResolution.X = 1024;
ScreenResolution.Y = 768;
}
void updateCamera()
{
Player_Position = player->getPosition();
Camera_Position = camera->getPosition();
Cursor_Position = cursor->getPosition();
CameraSpeed = 30;
if(Cursor_Position.X < 1) // LEFT
{
Player_Position.Z += CameraSpeed;
player->setPosition(Player_Position);
}
if(Cursor_Position.X > ScreenResolution.X - 2) // RIGHT
{
Player_Position.Z -= CameraSpeed;
player->setPosition(Player_Position);
}
if(Cursor_Position.Y > ScreenResolution.Y - 2) // DOWN
{
Player_Position.X -= CameraSpeed;
player->setPosition(Player_Position);
}
if(Cursor_Position.Y < 1) // UP
{
Player_Position.X += CameraSpeed;
player->setPosition(Player_Position);
}
Camera_Position.X = Player_Position.X - 2500;
Camera_Position.Y = Player_Position.Y + 2500;
Camera_Position.Z = Player_Position.Z;
camera->setTarget(Player_Position);
camera->setPosition(Camera_Position);
}
Gotta ressurect a dead thread...
This is the code I am using:
Alright, when the program executes the getCollisionPoint it crashes with a seg fault. Anyone see a problem with this code? I've checked and the colMgr, Ray, cam and cPos are not 0/NULL.
Code: Select all
vector3df target,location;
triangle3df tri;
line3d<f32> ray;
ITriangleSelector *tSelector;
ISceneCollisionManager *colMgr = smgr->getSceneCollisionManager();
position2d<s32> cPos;
ICameraSceneNode *cam;
if (event.isLeftMouseDown() == true)
{
cam = smgr->getActiveCamera();
cPos = rwnd->getCursorControl()->getPosition();
ray = colMgr->getRayFromScreenCoordinates(cPos,cam);
colMgr->getCollisionPoint(ray,tSelector,target,tri);
}
Umm, don't look at me that way. I'M not the dead one here.
--The One True Marshmellow
--The One True Marshmellow
-
deprecated
- Posts: 62
- Joined: Fri Jan 07, 2005 4:37 pm
- Location: California
Hey Munku:
Your code is almost identical to mine. But mine works fine....
In the beginning, I got seg faults when my camera or something else doesnt exist, and I call the code.
I put in a simple check and could work for you,
I know, Not much help, but I can say that I used this thread to get mine working... Check all your objects again..
Your code is almost identical to mine. But mine works fine....
In the beginning, I got seg faults when my camera or something else doesnt exist, and I call the code.
I put in a simple check and could work for you,
Code: Select all
if (cam)
{
ray = colMgr->getRayFromScreenCoordinates(cPos,cam);
colMgr->getCollisionPoint(ray,tSelector,target,tri);
}
-
Joshu
I have a question along these same lines about how to get node selection to work from a top-down or isometric view. If anyone knows how please answer it at http://irrlicht.sourceforge.net/phpBB2/ ... php?t=5247
-
Suliman
Hi I try this and it works but:
Is it reasonable that my program freezes for 6 to 12 entire seconds first time I run the code
It seemes strange, that is ten times as long as my map takes to build...
Well thanks
Suliman[/code]
Is it reasonable that my program freezes for 6 to 12 entire seconds first time I run the code
Code: Select all
raytrace = colManager->getRayFromScreenCoordinates(m, mainCamera);
colManager->getCollisionPoint(raytrace, selector, target, triangle);
Well thanks
Suliman[/code]
-
Suliman