Get Position From Screen-Coordinates Problem

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Get Position From Screen-Coordinates Problem

Post by Acki »

Hi, I hope you're all doing well !!! :)

I wrote my own function to get a line from screen coordinates straight forward...
to create a ray for ray casting (node picking)...

it works so far...
but the screen coordinates are not calculated, it seems...
the node always stays at the center of the screen, as if I used the center coordinates...
even if I use coordinates that are way out of screen bounds !!! :shock:

below is an example code...
can someone spot the error I made?
thanks :D

Code: Select all

#include <irrlicht.h>

irr::core::line3df getRayFromScreen(irr::IrrlichtDevice* irrDevice, float screenX, float screenY, float distance){
   irr::core::line3df ln(0,0,0,0,0,0);
   float DegToRad = irr::core::PI / 180.0;

   irr::scene::ICameraSceneNode* camera = irrDevice->getSceneManager()->getActiveCamera();

   irr::core::vector3df vecView = camera->getTarget() - camera->getPosition();
   vecView.normalize();
   irr::core::vector3df up = camera->getUpVector() * -1.0;
   irr::core::vector3df position = camera->getPosition();
   irr::core::dimension2du sSize = irrDevice->getVideoDriver()->getScreenSize();
   float CAMERA_FOV = camera->getFOV();
   float CAMERA_NEAR = camera->getNearValue();
   float Ratio = camera->getAspectRatio();

   irr::core::vector3df screenHoritzontally = vecView.crossProduct(up);
   screenHoritzontally.normalize();

   irr::core::vector3df screenVertically = screenHoritzontally.crossProduct(vecView);
   screenVertically.normalize();

   float halfHeight = tan(CAMERA_FOV * DegToRad * 0.5) * CAMERA_NEAR;
   float halfScaledAspectRatio = halfHeight * Ratio;

   screenVertically *= halfHeight;
   screenHoritzontally *= halfScaledAspectRatio;

   irr::core::vector3df PosInWorld = position + vecView;

   float shW = sSize.Width / 2;
   float shH = sSize.Height / 2;
   screenX -= shW;
   screenY -= shH;
   screenX /= shW;
   screenY /= shH;
   screenX /= -CAMERA_NEAR;
   screenY /= CAMERA_NEAR;

   PosInWorld.X += (screenHoritzontally.X * screenX + screenVertically.X * screenY);
   PosInWorld.Y += (screenHoritzontally.Y * screenX + screenVertically.Y * screenY);
   PosInWorld.Z += (screenHoritzontally.Z * screenX + screenVertically.Z * screenY);

   irr::core::vector3df Direction = PosInWorld - position;
   Direction.normalize();
   return irr::core::line3df(PosInWorld, PosInWorld + (Direction * distance));
}

//! main
int main(){
   //! create Irrlicht
   irr::SIrrlichtCreationParameters irrParameter;
   irrParameter.DriverType = irr::video::EDT_OPENGL;
   irrParameter.WindowSize = irr::core::dimension2di(800, 600);
   irr::IrrlichtDevice* device = createDeviceEx(irrParameter);
   irr::video::IVideoDriver* driver = device->getVideoDriver();
   irr::scene::ISceneManager* smgr = device->getSceneManager();
   //! create the camera
   irr::scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS();
   device->getCursorControl()->setVisible(false);

   //! the test node
   irr::scene::ISceneNode* node1 = smgr->addSphereSceneNode(1);
   node1->setMaterialFlag(irr::video::EMF_LIGHTING, false);

   //! main loop
   while(device->run()){
      driver->beginScene(true, true, irr::video::SColor(0,60,110,160));
      smgr->drawAll();
      driver->endScene();

      //! get ray from screen and update node
      // but the node stays at the center of the screen
      // even if the screen-position is way out of bounds !?!?!
      irr::core::line3df ray = getRayFromScreen(device, 2000, 1500, 20);
      node1->setPosition(ray.end);
   }

   //! close program
   device->drop();
   return 0;
}
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
CuteAlien
Admin
Posts: 9643
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Get Position From Screen-Coordinates Problem

Post by CuteAlien »

Hm, before debugging this... you know that Irrlicht has ISceneCollisionManager:: getRayFromScreenCoordinates?
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Re: Get Position From Screen-Coordinates Problem

Post by Acki »

yes, I know, of course... ;)
but I don't use it anywhere else...
and I don't want to create one only for this function... :P
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
CuteAlien
Admin
Posts: 9643
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Get Position From Screen-Coordinates Problem

Post by CuteAlien »

You could still look how the function is implemented there. Sorry, reading yours is hard just like that. As I have to figure out for each variable which coordinate system it's supposed to be in etc. - like why are screen coordinates in floats instead of ints - do you convert them to some 0-1 range first or something like that. Or why calculate some kind of halfHeight when you alreawdy have a camera so you can just check the frustum (thought maybe the frustum is not calculated for some reason because matrices are not updated).
Sorry, I just miss too much information to understand quickly what's going on here.

But I suppose if it always starts from center it means nothing is added to PosInWorld. So maybe start debugging there - why the other values are around 0.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Re: Get Position From Screen-Coordinates Problem

Post by Acki »

yeah, I hear you... :lol:

then I'll have to check the source of the collision manager...
I guess I can extract just that function instead of creating a complete collision manager... :P

thanks anyway !!! ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Re: Get Position From Screen-Coordinates Problem

Post by Acki »

just an update...

I had to adapt the function to my code, and vice versa...
and now it works like a charm !!! :)

thanks for the inspiration !!! :)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
CuteAlien
Admin
Posts: 9643
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Get Position From Screen-Coordinates Problem

Post by CuteAlien »

Cool!
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply