[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ] [ Search: ]

4.23.1 Clicking on Objects

This section describes how you can find out on which object a mouse has clicked.

The Code

Best way to explain this is by showing code:

 
iMeshWrapper* ClickedObject (iView* view, iGraphics2D* g2d, int mousex, int mousey)
{
  // Setup a 2D vector with our mouse position.
  csVector2 v2d (mousex, mousey);

  // We calculate the inverse perspective of this 2D point at
  // z=100.  This results in a 3D position in camera space at
  // z=100 that directly corresponds to the 2D position we
  // clicked on.  We use z=100 to ensure that we will at least
  // hit all objects that are before that distance.
  csVector3 v3d = view->InvProject (v2d, 100);

  // We are going to cast a beam in the current sector of the
  // camera from our camera position in the direction of the
  // 'v3d' point.  First we transform the v3d camera space
  // location to world space.
  iCamera* camera = view->GetCamera ();
  csVector3 startbeam = camera->GetTransform ().GetOrigin ();
  csVector3 endbeam = camera->GetTransform ().This2Other (v3d);
  iSector* beamsector = camera->GetSector ();

  // Now do the actual intersection.
  csSectorHitBeamResult result =
   beamsector->HitBeamPortals (startbeam, endbeam);

  //From the result object, return the intersected mesh.
  iMeshWrapper* mesh = result.mesh;
  return mesh;
}

Include Files

The include files useful for this section are:

 
#include <csgeom/transfrm.h>
#include <iengine/camera.h>
#include <iengine/sector.h>
#include <iengine/mesh.h>

This document was generated using texi2html 1.76.