12,406,124 members (70,899 online)
alternative version

27.3K views
16 bookmarked
Posted

# Target Following Control with OpenGL

, 4 May 2013 CPOL
 Rate this:
A simple target control to show us the direction of the enemy, useful especially for computer games

## Introduction

The Follow Target Control implemented in the attached C++ project represents a simple method to follow a known target especially if the target is out of sight.

## Background

Many 3D games implement this control, but still articles describing this method of following targets are scarce.

## Using the Code

A simple camera model was implemented to prove the concept. We use a simple cube to follow as target.

Image description: Shows a screen capture of the application.The white cube represent the target and the black dot represent the target following control.

For the actual method, we obtain the matrix that transforms the word coordinate system to the camera coordinate system. The matrix can be calculated using OpenGL command:

`glGetFloatv(GL_MODELVIEW_MATRIX, Matrix)  `

After that, we multiply this matrix with the coordinate of our target and we obtain the target coordinate in the camera space, which is 2D.

```TargetPosition[0] = Position[0]*Matrix[0]+Position[1] *
Matrix[4] + Position[2] * Matrix[8] + 1.0f * Matrix[12];
TargetPosition[1] = Position[0]*Matrix[1]+Position[1] *
Matrix[5] + Position[2] * Matrix[9] + 1.0f * Matrix[13];
TargetPosition[2] = Position[0]*Matrix[2]+Position[1]*
Matrix[6] + Position[2] * Matrix[10] + 1.0f * Matrix[14]; ```

To display a target point close to the screen edge, we do some simple geometry calculation to determine the intersection point between the screen rectangle frame and the target.

To actually display the following target, we switch from 3D mode to 2D mode and we use OpenGL `GL_POINTS` to indicate the direction of our target:

```glMatrixMode(GL_PROJECTION);
glPushMatrix();
glOrtho(0, 500, 500, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(250, 250, 0.0);
glPointSize(10.0f);
glBegin(GL_POINTS);
glVertex2f((IPoint[1] -250)/1.1, (-IPoint[0] +250)/1.1);
glEnd();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();  ```

## Points of Interest

The implementation of this method is quite easy, it requires combining 2D and 3D mode with OpenGL.

To control the camera movement, the following keys are used:

• w, s: zoom in and out
• q, z: up and down movement
• a, d: left and right movement

## History

In the first implementation, I used a simple circle targeting. This implementation is inside the source code, but is currently commented.

## Share

 Software Developer Romania
No Biography provided

## You may also be interested in...

 First Prev Next
 My vote of 3 Bartlomiej Filipek17-Apr-13 3:10 Bartlomiej Filipek 17-Apr-13 3:10
 My Vote: 0 Roger6516-Apr-13 1:45 Roger65 16-Apr-13 1:45
 Re: My Vote: 0 ionutcelgroaznic3-May-13 20:57 ionutcelgroaznic 3-May-13 20:57
 A better version [modified] ionutcelgroaznic11-Apr-10 21:21 ionutcelgroaznic 11-Apr-10 21:21
 I'm working on a improved version that multiplies the current position not only with modelview matrix, but also with projection matrix and viewport matrix. I'm not sure which way is better. Anyway here is an example on multipliying with all matrices and is based on gluProject: GLdouble pos3D_x, pos3D_y, pos3D_z,win_x,win_y,win_z; // arrays to hold matrix information GLdouble model_view[16]; glGetDoublev(GL_MODELVIEW_MATRIX, model_view); GLdouble projection[16]; glGetDoublev(GL_PROJECTION_MATRIX, projection); GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); // get window coordinate based on s3D coordinates pos3D_x=0.0; pos3D_y=0.0; pos3D_z=0.0; gluProject(pos3D_x,pos3D_y,pos3D_z, model_view, projection, viewport, &win_x, &win_y, &win_z); So we don't need to multiply the matrices manually modified on Monday, April 12, 2010 3:27 AM
 [My vote of 1] my vote of 1 Member 306826-Mar-10 13:31 Member 3068 26-Mar-10 13:31
 Re: [My vote of 1] my vote of 1 Saurabh.Garg27-Mar-10 0:35 Saurabh.Garg 27-Mar-10 0:35
 Re: [My vote of 1] my vote of 1 Druuler30-Mar-10 22:27 Druuler 30-Mar-10 22:27
 Re: [My vote of 1] my vote of 1 imagiro11-Oct-13 20:57 imagiro 11-Oct-13 20:57
 Last Visit: 31-Dec-99 18:00     Last Update: 29-Jul-16 21:58 Refresh 1