3 Replies - 1241 Views - Last Post: 03 December 2008 - 12:53 PM Rate Topic: -----

#1 gamegirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-November 08

open gl

Posted 26 November 2008 - 01:45 PM

#include <GL/glut.h>
#include <math.h>


#define X_CENTRE_R 4.0      /* centre point of square */
#define Y_CENTRE_R 4.0
#define LENGTH_R   1.0 


#define X_CENTRE 0.0      /* centre point of square */
#define Y_CENTRE 4.0
#define LENGTH   1.0 


#define X_CENTRE_L -4.0      /* centre point of square */
#define Y_CENTRE_L 4.0
#define LENGTH_L   1.0      /* lengths of sides of square */ 

//down 
#define X_CENTRE_D 0.0      /* centre point of square */
#define Y_CENTRE_D -4.0
#define LENGTH_D   1.0 

//triangle down

#define X_CENTRE_T 0.0      /* centre point of square */
#define Y_CENTRE_T -4.0
#define LENGTH_T   1.0 
 

#define LENGTH_Anim  0.1 
GLfloat angle = 0.0;
float xmove = 0.0;
float ymove = 0.1;



/* 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(-7.0, 7.0, -7.0, 7.0, -7.0, 7.0);
   glMatrixMode( GL_MODELVIEW ); 
} 


/* 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, 1.0);        /* yellow  drawing objects */ 
   glLoadIdentity();
 

 {
   glLoadIdentity();
 
 
  glTranslatef( xmove, ymove, 0.0);

  glTranslatef(  X_CENTRE_D , Y_CENTRE_D , 0.0);
  glRotatef(angle, 0.0, 0.0, 1.0);
  glTranslatef( -X_CENTRE_D , -Y_CENTRE_D , 0.0);



  // glClear (GL_COLOR_BUFFER_BIT); 
  // glTranslatef( 0.0, -4.0, 0.0 ); 
  /* X_CENTRE_T= 
   Y_CENTRE_T=
      if (X_CENTRE_T>X_CENTRE - LENGTH /1 && X_CENTRE_T <X_CENTRE + LENGTH /1 &&  Y_CENTRE_T>Y_CENTRE - LENGTH /1 &&  Y_CENTRE_T< Y_CENTRE + LENGTH /1)
              {
                 //target hit
              }
     else
                 {
                                //draw target
                 }
  
       /* white drawing objects */ 


glBegin(GL_POLYGON);


 
      glVertex2f( X_CENTRE_D - LENGTH_D /4 , Y_CENTRE_D - LENGTH_D /4 );
      glVertex2f( X_CENTRE_D - LENGTH_D /4 , Y_CENTRE_D + LENGTH_D /4 );
      glVertex2f( X_CENTRE_D + LENGTH_D /4 , Y_CENTRE_D + LENGTH_D /4 );
      glVertex2f( X_CENTRE_D + LENGTH_D /4 , Y_CENTRE_D - LENGTH_D /4 );
     

 glEnd();

}
glBegin(GL_POLYGON);


 
      glVertex2f( X_CENTRE_D - LENGTH_D /4 , Y_CENTRE_D + LENGTH_D/4 );
      glVertex2f( X_CENTRE_T , Y_CENTRE_D + LENGTH_T /2 );
      glVertex2f( X_CENTRE_D + LENGTH_D /4 , Y_CENTRE_D + LENGTH_D/4 );
      
     
glTranslatef(  X_CENTRE_D , Y_CENTRE_D , 0.0);
 glEnd();


 glLoadIdentity();
 glColor3f(1.0, 1.0, 0.0);
 
   glBegin(GL_POLYGON);

  
 
      glVertex2f( X_CENTRE_R - LENGTH_R /1 , Y_CENTRE_R - LENGTH_R /1 );
      glVertex2f( X_CENTRE_R - LENGTH_R /1 , Y_CENTRE_R + LENGTH_R /1);
      glVertex2f( X_CENTRE_R + LENGTH_R /1 , Y_CENTRE_R + LENGTH_R /1  );
      glVertex2f( X_CENTRE_R + LENGTH_R /1 , Y_CENTRE_R - LENGTH_R /1  );
     
     
   glEnd();
       glColor3f(1.0, 1.0, 0.0);
 glBegin(GL_POLYGON);


 
      glVertex2f( X_CENTRE_L - LENGTH_L /1 , Y_CENTRE_L - LENGTH_L /1 );
      glVertex2f( X_CENTRE_L - LENGTH_L /1 , Y_CENTRE_L + LENGTH_L /1 );
      glVertex2f( X_CENTRE_L + LENGTH_L /1 , Y_CENTRE_L + LENGTH_L /1 );
      glVertex2f( X_CENTRE_L + LENGTH_L /1 , Y_CENTRE_L - LENGTH_L /1 );

   glEnd(); 
 glColor3f(1.0, 1.0, 0.0);
 glBegin(GL_POLYGON);


 
      glVertex2f( X_CENTRE - LENGTH /1 , Y_CENTRE - LENGTH /1  );
      glVertex2f( X_CENTRE - LENGTH /1 , Y_CENTRE + LENGTH /1  );
      glVertex2f( X_CENTRE + LENGTH /1 , Y_CENTRE + LENGTH /1  );
      glVertex2f( X_CENTRE + LENGTH /1 , Y_CENTRE - LENGTH /1  );
     
     
   glEnd();



   glutSwapBuffers();
}


/* graphics initialisation */ 
void init(void)
{
   glClearColor (0.0, 0.0, 0.0, 0.0);   /* window will be cleared to black */
}

void idle(void)
{ 
 xmove =  xmove - LENGTH_Anim * sin(angle * M_PI / 180.0); 
 ymove =  ymove + LENGTH_Anim * cos(angle * M_PI / 180.0); 
   // if (xmove>1)
//{
//hit target
//}
//if (y>1)
  glutPostRedisplay();
  
}


/* mouse callback function called when mouse button clicked */                                
void mouse(int button, int state, int x, int y) 
{ 
  switch (button) 
  { 
  case GLUT_LEFT_BUTTON:    if (state == GLUT_DOWN) 
                            { 
			      glutIdleFunc(idle); 
                              glutPostRedisplay();
                            }
  }
}  




/* keyboard callback function called when mouse

 button clicked */ 
void keyboard(unsigned char key, int x,int y) 
{ 
  switch (key) 
  {
              case 'r':  angle = angle - 2.0;
          
              glutPostRedisplay();
break;     

               case 'l':  angle = angle + 2.0;

               glutPostRedisplay();
                     break;                                                                                                                                                                           
              case 'e':  exit(0); 
                           

                           break; 
            case 'm':  angle =  0 ; 
               xmove = 0.0;
               ymove = 0.0;
                 glutIdleFunc(0);  
            glutPostRedisplay();
                           
                        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_DOUBLE | GLUT_RGB);
   /* window width = 400 pixels, height = 400 pixels */
   glutInitWindowSize (400, 400);
   /* 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 keyboard callback fonction */
   glutKeyboardFunc (keyboard);/* keyboard*/
   glutMouseFunc(mouse); /* register mouse callback function */
   
   glutMainLoop();
}





im just traying to get the target to desepear when hit by the mesile [code]

Is This A Good Question/Topic? 0
  • +

Replies To: open gl

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: open gl

Posted 27 November 2008 - 06:14 AM

1) :code:
2) Moved to Game Programming
3) Welcome to DIC.
Was This Post Helpful? 0
  • +
  • -

#3 Lillefix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: open gl

Posted 30 November 2008 - 12:36 PM

One, you should comment your code, we do not always understand what you think.
Second, the only piece of code which may look like collision-detection is commented out, why would you do that?
Third, excactly what is your problem?

This post has been edited by Lillefix: 30 November 2008 - 12:44 PM

Was This Post Helpful? 0
  • +
  • -

#4 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 789
  • View blog
  • Posts: 7,623
  • Joined: 05-May 05

Re: open gl

Posted 03 December 2008 - 12:53 PM

Hm.. That's an odd way to do bounding box collision detection.

Try a simple width and height algorithm (where width and height is the size of the bounding box for collision detection). Then, check the left side of the first rectangle with the left position of the second rectangle plus it's width (player.x <= object.x+object.width) and so on. Try implementing it as a function that simply takes two rectangles and returns a nonzero number of they touch each other.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1