Hi.
I try to write a simple program but I have a few problems.
As you can see from the codes I have a big part of it but I still have two problems:
- when the ‘missile’ hits any of the edge it should return like a ball hitting a wall.
- When the ‘missile’ hits any of the targets it should disappear and when all the targets are hit the program should disappear.
I insert the source code to make it easier to understand my point
Please help if u can.
Regards
Tim
/* file: gm1.c
2D graphic game
created by: Tim
date: 19/10/200
*/
#include <GL/glut.h>
#include <math.h>
#define X_CENTRE 0.0 /* centre point of square /
#define Y_CENTRE 0.0
#define LENGTH 1.0 / lengths of sides of square */
#define MISSILE_MOVE 0.05
GLfloat angle = 0.0; /* angle of rotation (in degrees) */
GLdouble objx, objy, objz;
GLfloat MISSILE_X= 0.0;
GLfloat MISSILE_Y= -0.9;
void idle(void)
{
float kawa ;
kawa = angle *(3.14159)/180;
MISSILE_X = MISSILE_X - MISSILE_MOVE * sin(kawa);
MISSILE_Y = MISSILE_Y + MISSILE_MOVE * cos(kawa);
glutPostRedisplay();
}
/* graphics initialisation /
void init(void)
{
glClearColor (1.0, 0.0, 0.0, 0.0); / window will be cleared to black */
}
/* define object to be drawn as a square polygon */
void draTarget(void)
{
glBegin(GL_POLYGON);
glVertex2f(0.1,0.1);
glVertex2f(-0.1,0.1);
glVertex2f(-0.1,-0.1);
glVertex2f(0.1,-0.1);
glEnd();
/* execute drawing commands in buffer */
}
/*Define object to be drawn as a ‘missile’ polygon */
void draMissile(void)
{
glBegin(GL_POLYGON);
glVertex2f(0.0, 0.08);
glVertex2f(-0.02,0.03);
glVertex2f(-0.02,-0.08);
glVertex2f(0.02,-0.08);
glVertex2f(0.02,0.03);
glEnd();
}
/*Define the objects boundry lines */
void draLine(void)
{
glBegin(GL_LINES);
glVertex2f(0.5, -0.6);
glVertex2f(1.0,-0.6);
glVertex2f(-0.5,-0.6);
glVertex2f(-1.0, -0.6);
glEnd();
}
/* display callback function
called whenever contents of window need to be re-displayed */
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT); /* clear window /
glColor3f(1.0, 1.0, 0.0); / white drawing objects */
//glRotatef( 10.0, 0.0, 0.0, 1.0 ); /* modelling transformation */
glLoadIdentity();
glTranslatef(-0.5, 0.75, 0.0); /translate target to the left/
draTarget();
glLoadIdentity();
glTranslatef(0.0, 0.75, 0.0); /translate target ot middle/
draTarget();
glLoadIdentity();
glTranslatef(0.5, 0.75, 0.0); /translate target to right/
draTarget(); /call the draw target funtion/
glLoadIdentity();
glColor3f(1.0, 1.0, 1.0); /sets the missile’s colour to wite/
glTranslatef(MISSILE_X, MISSILE_Y, 0.0);
glRotatef( angle, 0.0,0.0, 1.0 ); /rotate the missile/
draMissile(); /* call missile function*/
glLoadIdentity();
glColor3f(1.0, 1.0, 1.0); /* set the colour ot teh line to wite*/
draLine(); /* call the draw line function*/
glutSwapBuffers();
}
/* reshape callback function
executed when window is moved or resized /
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
/ use orthographic (parallel) projection
use xmin = -1, xmax = 1
ymin = -1, ymax = 1
znear = -1, zfar = 1 - not relevant here (2D) */
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode( GL_MODELVIEW );
}
/* mouse callback function
called when mouse button clicked */
void mouse(int button, int state, int wx, int wy)
{
GLint realwy;
int wz= 0;
switch ( button )
{
case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN )
{
GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realwy;
// GLdouble wx, wy, wz;
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
realwy=viewport[3] - wy -1;
gluUnProject( wx, realwy, wz, mvmatrix, projmatrix, viewport, &objx, &objy, &objz);
if (objy < -0.68)
{
MISSILE_X= objx;
MISSILE_Y= objy;
glutIdleFunc(NULL);
glutPostRedisplay();
}
}
break;
case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN )
{
if (( objy < 1.0) | | ( objx < 1.0 && objx > -1.0))
{
glRotatef( angle, 0.0,0.0, 1.0 );
glutIdleFunc(idle);
}
}
break;
default: break;
}
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case’r’: case ‘R’: angle = angle-5.0;
glutPostRedisplay();
break;
case'l': case 'L': angle = angle+5.0;
glutPostRedisplay();
break;
case'e': case 'E': exit(0);
break;
default: break;
}
}
int main(int argc, char** argv)
{
/* window management code … /
/ initialises GLUT and processes any command line arguments /
glutInit(&argc, argv);
/ use single-buffered window and RGBA colour model /
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
/ window width = 400 pixels, height = 400 pixels /
glutInitWindowSize (600, 600);
/ window upper left corner at (100, 100) /
glutInitWindowPosition (100, 100);
/ creates an OpenGL window with command argument in its title bar */
glutCreateWindow (argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
/* register mouse callback function */
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
}