Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
unit_click.h
Go to the documentation of this file.
1 #ifndef __UNIT_CLICK_CPP__
2 #define __UNIT_CLICK_CPP__
3 
4 #include "vegastrike.h"
5 #include "gfx/mesh.h"
6 #include "unit.h"
7 
8 extern Vector mouseline;
9 extern vector< Vector >perplines;
10 Vector MouseCoordinate( int mouseX, int mouseY );
11 
12 template < class UnitType >
13 bool GameUnit< UnitType >::querySphereClickList( int mouseX, int mouseY, float err, Camera *activeCam )
14 {
15  int i;
16  Matrix vw;
17  _Universe->AccessCamera()->GetView( vw );
18  Vector mousePoint;
19 #ifdef VARIABLE_LENGTH_PQR
20  Vector TargetPoint( cumulative_transformation_matrix[0],
21  cumulative_transformation_matrix[1],
22  cumulative_transformation_matrix[2] );
23  float SizeScaleFactor = sqrtf( TargetPoint.Dot( TargetPoint ) );
24 #else
25  Vector TargetPoint;
26 #endif
27 
28  Vector CamP, CamQ, CamR;
29  for (i = 0; i < nummesh(); i++) {
30  TargetPoint = Transform( this->cumulative_transformation_matrix, this->meshdata[i]->Position() );
31 
32  mousePoint = Transform( vw, TargetPoint );
33  if (mousePoint.k > 0) //z coordinate reversed - is in front of camera
34  continue;
35  mousePoint = MouseCoordinate( mouseX, mouseY );
36 
37  activeCam->GetPQR( CamP, CamQ, CamR );
38  mousePoint = Transform( CamP, CamQ, CamR, mousePoint );
39  CamP = activeCam->GetPosition().Cast();
40  mousePoint += CamP;
41 
42  TargetPoint = TargetPoint-mousePoint;
43  if (TargetPoint.Dot( TargetPoint )
44  < err*err
45  +this->meshdata[i]->rSize()*this->meshdata[i]->rSize()
46 #ifdef VARIABLE_LENGTH_PQR
47  *SizeScaleFactor*SizeScaleFactor
48 #endif
49  +
50 #ifdef VARIABLE_LENGTH_PQR
51  SizeScaleFactor*
52 #endif
53  2*err*this->meshdata[i]->rSize()
54  )
55  return true;
56  }
57  Unit *su;
58  for (un_iter ui = this->getSubUnits(); (su = *ui); ++ui)
59  if ( (su)->querySphereClickList( mouseX, mouseY, err, activeCam ) )
60  return true;
61  return false;
62 }
63 
64 #endif
65