Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become an Expert!

Join 244,295 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 877 people online right now. Registration is fast and FREE... Join Now!




open gl

 
Reply to this topicStart new topic

open gl

gamegirl
26 Nov, 2008 - 12:45 PM
Post #1

New D.I.C Head
*

Joined: 26 Nov, 2008
Posts: 1

cpp
#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]

User is offlineProfile CardPM
+Quote Post


gabehabe
RE: Open Gl
27 Nov, 2008 - 05:14 AM
Post #2

i > u
Group Icon

Joined: 6 Feb, 2008
Posts: 7,381



Thanked: 154 times
Dream Kudos: 2850
Expert In: Lots of things.

My Contributions
1) code.gif
2) Moved to Game Programming
3) Welcome to DIC.
User is offlineProfile CardPM
+Quote Post

Lillefix
RE: Open Gl
30 Nov, 2008 - 11:36 AM
Post #3

D.I.C Head
Group Icon

Joined: 19 Sep, 2008
Posts: 131



Thanked: 17 times
Dream Kudos: 50
My Contributions
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 Nov, 2008 - 11:44 AM
User is offlineProfile CardPM
+Quote Post

WolfCoder
RE: Open Gl
3 Dec, 2008 - 11:53 AM
Post #4

ヒヒヒー
Group Icon

Joined: 5 May, 2005
Posts: 4,776



Thanked: 22 times
Dream Kudos: 1575
Expert In: ゲームのプログラム、かわいい

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 04:34PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month