Skip to content

Instantly share code, notes, and snippets.

@JeffCX
Created October 21, 2018 21:39
Show Gist options
  • Save JeffCX/2624120547d24a257749a22bd235f89a to your computer and use it in GitHub Desktop.
Save JeffCX/2624120547d24a257749a22bd235f89a to your computer and use it in GitHub Desktop.
collision detectiono
#ifdef _WINDOWS
#include <GL/glew.h>
#endif
#include <SDL.h>
#include <SDL_opengl.h>
#include <SDL_image.h>
#ifdef _WINDOWS
#define RESOURCE_FOLDER ""
#else
#define RESOURCE_FOLDER "NYUCodebase.app/Contents/Resources/"
#endif
#include "ShaderProgram.h"
#include "string"
#include "vector"
#include "glm/mat4x4.hpp"
#include "glm/gtc/matrix_transform.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
SDL_Window* displayWindow;
bool gan=false;
bool CollisionDetection(float w1, float h1,float w2,float h2, float x1,float y1,float x2,float y2){
float sub_x;
float sub_y;
if(x1-x2<0){
sub_x = -(x1-x2);
}else{
sub_x = x1-x2;
}
if(y1-y2<0){
sub_y = -(y1-y2);
}else{
sub_y = y1-y2;
}
float X = sub_x - ((w1+w2)/2);
float Y = sub_y - ((h1+h2)/2);
if(X<0&&Y<0){
return true;
}else{
return false;
}
}
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
glewInit();
#endif
glViewport(0, 0, 640, 360);
ShaderProgram program;
program.Load(RESOURCE_FOLDER"vertex.glsl",RESOURCE_FOLDER"fragment.glsl");
ShaderProgram program1;
program1.Load(RESOURCE_FOLDER"vertex.glsl",RESOURCE_FOLDER"fragment.glsl");
glm::mat4 projectionMatrix = glm::mat4(1.0f);
glm::mat4 modelMatrix_easy = glm::mat4(1.0f);
glm::mat4 modelMatrix_hard = glm::mat4(1.0f);
glm::mat4 viewMatrix = glm::mat4(1.0f);
projectionMatrix = glm::ortho(-1.777f, 1.777f, -1.0f, 1.0f, -1.0f, 1.0f);
glUseProgram(program.programID);
//setup time
float lastFrameTicks = 0.0f;
glm::mat4 modelMatrix1 = glm::mat4(1.0f);
SDL_Event event;
bool done = false;
while (!done) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
done = true;
}else if(event.type==SDL_MOUSEBUTTONDOWN){
float UnitX = (((float)event.button.x / 640.0f) * 3.554f ) - 1.777f;
float UnitY = (((float)(360-event.button.y) / 360.0f) * 2.0f ) - 1.0f;
if(UnitX>=0.3 && UnitX <=0.5 && UnitY>0 && UnitY<0.3){
program.SetColor(0.5f,0.4f,0.3f,0.9f);
}
if(UnitX>=-0.5 && UnitX <=-0.3 && UnitY>0 && UnitY<0.3){
program1.SetColor(0.3f,0.2f,0.9f,0.7f);
gan=true;
}
}
}
glClear(GL_COLOR_BUFFER_BIT);
program.SetColor(0.5f,0.4f,0.3f,0.9f);
program1.SetColor(0.3f,0.2f,0.9f,0.7f);
float ticks = (float)SDL_GetTicks();
float elapsed = ticks - lastFrameTicks;
lastFrameTicks = ticks;
program.SetModelMatrix(modelMatrix_easy);
program.SetProjectionMatrix(projectionMatrix);
program.SetViewMatrix(viewMatrix);
float vertices_hard[] = {
0.3f, 0.3f, // Top-left
0.5f, 0.3f, // Top-right
0.5f, 0.0f, // Bottom-right
0.5f, 0.0f, // Bottom-right
0.3f, 0.0f, // Bottom-left
0.3f, 0.3f // Top-left
};
glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices_hard);
glEnableVertexAttribArray(program.positionAttribute);
glDrawArrays(GL_TRIANGLES, 0, 6);
program1.SetModelMatrix(modelMatrix_hard);
program1.SetProjectionMatrix(projectionMatrix);
program1.SetViewMatrix(viewMatrix);
float vertices_easy[] = {
-0.3f, 0.3f, // Top-left
-0.5f, 0.3f, // Top-right
-0.5f, 0.0f, // Bottom-right
-0.5f, 0.0f, // Bottom-right
-0.3f, 0.0f, // Bottom-left
-0.3f, 0.3f // Top-left
};
glVertexAttribPointer(program1.positionAttribute, 2, GL_FLOAT, false, 0, vertices_easy);
glEnableVertexAttribArray(program1.positionAttribute);
glDrawArrays(GL_TRIANGLES, 0, 6);
float w1 = 0.2f;
float h1 = 0.3f;
float w2 = 0.2f;
float h2 = 0.3f;
float x1= 0.4f;
float y1 = 0.15f;
float x2 = -0.4f;
float y2 = 0.15f;
bool collision = CollisionDetection(w1,h1,w2,h2,x1-elapsed*0.0001,y1,x2+elapsed*0.0001,y2);
if(!collision){
modelMatrix_easy = glm::translate(modelMatrix_easy,glm::vec3(-elapsed*0.0001 ,0.0f, 0.0f));
modelMatrix_hard = glm::translate(modelMatrix_hard,glm::vec3(+elapsed*0.0001, 0.0f, 0.0f));
}else{
modelMatrix_easy = glm::translate(modelMatrix_easy,glm::vec3(0.0f, 0.0f, 0.0f));
modelMatrix_hard = glm::translate(modelMatrix_hard,glm::vec3(0.0f, 0.0f, 0.0f));
}
if(gan){
ShaderProgram program3;
program3.Load(RESOURCE_FOLDER"vertex.glsl",RESOURCE_FOLDER"fragment.glsl");
modelMatrix1 = glm::translate(modelMatrix1,glm::vec3(0.0f,elapsed*0.001,0.0f));
program3.SetModelMatrix(modelMatrix1);
program3.SetProjectionMatrix(projectionMatrix);
program3.SetViewMatrix(viewMatrix);
float vertices_created[] = {
-0.7f, 0.5f, // Top-left
-0.9f, 0.5f, // Top-right
-0.9f, 0.0f, // Bottom-right
-0.9f, 0.0f, // Bottom-right
-0.7f, 0.0f, // Bottom-left
-0.7f, 0.5f // Top-left
};
glVertexAttribPointer(program3.positionAttribute, 2, GL_FLOAT, false, 0, vertices_created);
glEnableVertexAttribArray(program3.positionAttribute);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
glDisableVertexAttribArray(program.positionAttribute);
SDL_GL_SwapWindow(displayWindow);
}
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment