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

4.21.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 (iCamera* camera, int mousex, int mousey)
{
  // Setup a 2D vector with our mouse position.  We invert the y
  // (based on vertical screen dimension) because CS assumes y=0
  // is down for 3D calculations.
  csVector2 v2d (mousex, camera->GetShiftY () * 2 - 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;
  camera->InvPerspective (v2d, 100, v3d);

  // 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.
  csVector3 startbeam = camera->GetTransform ().GetOrigin ();
  csVector3 endbeam = camera->GetTransform ().This2Other (v3d);
  csVector3 intersect;

  // Now do the actual intersection.
  int poly = -1;
  iSector* beamsector = camera->GetSector ();
  iMeshWrapper* mesh =
    beamsector->HitBeamPortals(startbeam, endbeam, intersect, &poly);
  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>

[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated using texi2html 1.76.