#include <freeglut.h>
#include <iostream>
#include<SOIL.h>
using namespace std;
GLuint texture[8];
float move_plane = 0.0f;
float up = 0.0f;
float down = 0.0f;
float screen = 0.001f;
bool checkCollide(float x, float y, float oWidth, float oHeight, float xTwo, float yTwo, float oTwoWidth, float oTwoHeight)
{
// AABB 1
float x1Min = x;
float x1Max = x + oWidth;
float y1Max = y + oHeight;
float y1Min = y;
// AABB 2
float x2Min = xTwo;
float x2Max = xTwo + oTwoWidth;
float y2Max = yTwo + oTwoHeight;
float y2Min = yTwo;
// Collision tests
if (x1Max < x2Min || x1Min > x2Max) return false;
if (y1Max < y2Min || y1Min > y2Max) return false;
return true;
}
void update(int value)
{
down--;
if (down <= -180.0f)
{
down = 0.0f;
}
screen += 0.1667f;
if(screen >= 1.0f)
{
screen = 1.0f;
}
glutPostRedisplay();
glutTimerFunc(50, update, 0);
}
void drawcollision_one()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[3]);
glBegin(GL_POLYGON);
glTexCoord3f(0.0f + screen, 0.0f, 0.0f);
glVertex3f(10.0f, -10.0f, 0.0f);
glTexCoord3f(0.167f + screen, 0.0f, 0.0f);
glVertex3f(10.0f, 10.0f, 0.0f);
glTexCoord3f(0.167f + screen, 1.0f, 0.0f);
glVertex3f(-10.0f, 10.0f, 0.0f);
glTexCoord3f(0.0f + screen, 1.0f, 0.0f);
glVertex3f(-10.0f, -10.0f, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void coll_plane_one()
{
//draw bullet
float x = -2.5f + move_plane;
float y = -75.0f + up;
float oWidth = 5.0f;
float oHeight = 5.0f;
//draw plane
float xTwo = -10.0f + move_plane;
float yTwo = 100.0f + down;
float oTwoWidth = 20.0f;
float oTwoHeight = 20.0f;
if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
{
drawcollision_one();
}
}
GLuint loadTex(const char* texname)
{
/* load an image file directly as a new OpenGL texture */
GLuint texture = SOIL_load_OGL_texture
(
texname,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_POWER_OF_TWO
);
return texture;
}
void init()
{
texture[0] = loadTex("C:\\Users\\Owner\\Desktop\\plane.png");
texture[1] = loadTex("C:\\Users\\Owner\\Desktop\\bullet.png");
texture[2] = loadTex("C:\\Users\\Owner\\Desktop\\enemy.png");
texture[3] = loadTex("C:\\Users\\Owner\\Desktop\\coll.png");
}
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[2]);
for (float i = -130.0f; i <= 130.0f; i += 40.0f)
{
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(i, 100.0f + down);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(i+20.0f, 100.0f + down);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(i+20.0f, 80.0f + down);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(i, 80.0f + down);
glEnd();
}
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(-10.0f+move_plane, -80.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(10.0f+move_plane, -80.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(10.0f+move_plane, -100.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(-10.0f+move_plane, -100.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(-2.5f+move_plane, -75.0f+up);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(2.5f+move_plane, -75.0f+up);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(2.5f+move_plane, -80.0f+up);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(-2.5f+move_plane, -80.0f+up);
glEnd();
coll_plane_one();
glPopMatrix();
glutSwapBuffers();
glDisable(GL_TEXTURE_2D);
}
void ChangeSize(GLsizei w, GLsizei h)
{
GLfloat aspectRatio;
if (h == 0)
h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspectRatio = (GLfloat)w / (GLfloat)h;
if (w <= h)
glOrtho(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
else
glOrtho(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0, 1.0, -1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void shoot()
{
up++;
if (up >= 175.0f)
{
up = 0.0f;
glutIdleFunc(NULL);
}
glutPostRedisplay();
}
void handleKeypress(unsigned char key, int x, int y)
{
int animate = 0;
switch(key)
{
case 27:
exit(0);
break;
case 32:
animate = !animate;
if (animate)
glutIdleFunc(shoot);
else
glutIdleFunc(NULL);
break;
}
glutPostRedisplay();
}
void handleSpecialKeypress(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT:
move_plane--;
if (move_plane <= -125.0f)
{
move_plane = -125.0f;
}
break;
case GLUT_KEY_RIGHT:
move_plane++;
if (move_plane >= 125.0f)
{
move_plane = 125.0f;
}
break;
}
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(400, 300);
glutInitWindowSize(800, 600);
glutTimerFunc(50, update, 0);
glutCreateWindow("Combat");
glutDisplayFunc(renderScene);
glutReshapeFunc(ChangeSize);
glutKeyboardFunc(handleKeypress);
glutSpecialFunc(handleSpecialKeypress);
init();
glutMainLoop();
return 0;
}
this is my code, what I want is that when I draw a bullet that when it hits a plane it draws a animated collision sprite.