Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Commit

Permalink
added BallInHole
Browse files Browse the repository at this point in the history
  • Loading branch information
thongnt99 committed May 17, 2015
1 parent e94f706 commit 7060537
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 45 deletions.
22 changes: 21 additions & 1 deletion 3D-Billiards.dev
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ IncludeVersionInfo=0
SupportXPThemes=0
CompilerSet=4
CompilerSettings=0000000100000000001000000
UnitCount=7
UnitCount=9

[VersionInfo]
Major=1
Expand Down Expand Up @@ -120,3 +120,23 @@ Priority=1000
OverrideBuildCmd=0
BuildCmd=

[Unit8]
FileName=Hole.cpp
CompileCpp=1
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

[Unit9]
FileName=Hole.h
CompileCpp=1
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

Binary file modified 3D-Billiards.exe
Binary file not shown.
22 changes: 11 additions & 11 deletions 3D-Billiards.layout
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[Editors]
<<<<<<< HEAD
Focused=0
Focused=5
Order=5,4,6,3,1,0
[Editor_0]
Open=1
Top=1
<<<<<<< HEAD
CursorCol=2
CursorRow=147
TopLine=283
CursorCol=17
CursorRow=200
TopLine=181
=======
CursorCol=17
CursorRow=164
Expand Down Expand Up @@ -68,22 +68,22 @@ LeftChar=1
[Editor_3]
Open=1
Top=0
CursorCol=18
CursorRow=49
TopLine=33
CursorCol=14
CursorRow=52
TopLine=25
LeftChar=1
[Editor_4]
CursorCol=7
CursorRow=6
TopLine=1
LeftChar=1
[Editor_5]
CursorCol=12
CursorRow=4
CursorCol=1
CursorRow=2
TopLine=1
LeftChar=1
[Editor_6]
CursorCol=23
CursorRow=8
CursorCol=20
CursorRow=11
TopLine=1
LeftChar=1
8 changes: 4 additions & 4 deletions Ball.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "Ball.h"
Ball::Ball(char* objFile, bool hasTexture, map<string, texture>* textures):Model_OBJ(objFile, hasTexture, textures){
this->radius = 0.02;
this->mass = 1;
memset(col,0,sizeof(col));
this->mass = 1;
}

bool Ball::isBallHit(Ball* ball){
Expand Down Expand Up @@ -47,8 +46,9 @@ void Ball::resToBallHit(Ball *ball){
void Ball::draw(){
glPushMatrix();
cout << angle << endl;
glTranslatef(pos.x, pos.y , pos.z);
glRotatef(-angle,-vel.z,0,vel.x);
glTranslatef(pos.x, pos.y , pos.z);
if (!isInHole)
glRotatef(-angle,-vel.z,0,vel.x);
Model_OBJ::draw();
glPopMatrix();
}
Expand Down
9 changes: 4 additions & 5 deletions Ball.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ class Ball: public Model_OBJ{
Ball(){}
Ball(char* objFile, bool hasTexture, map<string, texture>* textures);
bool isBallHit(Ball *ball);
void resToBallHit(Ball *ball);
void resToBallHit(Ball *ball);
void draw();
public:
public:
float radius;
float mass;
glm::vec3 pos;
glm::vec3 vel;
glm::vec3 acc;
float angle = 0;
float col[4];

float angle = 0;
bool isInHole = false;
};
Binary file modified Ball.o
Binary file not shown.
19 changes: 19 additions & 0 deletions Hole.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "Hole.h"
Hole::Hole(glm::vec3 pos,float radius){
this->pos = pos;
this->radius = radius;
}
bool Hole::isBallInHole(Ball *ball){
glm::vec3 distance = this->pos - ball->pos;
float length = glm::length(distance);
if ( length <= (this->radius + ball->radius+ 0.01) )
return true;
else
return false;
}

void Hole::resToBallInHole(Ball *ball){
ball->pos = this->pos;
ball->isInHole = true;
ball->vel = glm::vec3(0,-0.05,0);
}
10 changes: 10 additions & 0 deletions Hole.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "Ball.h"
class Hole{
public:
Hole(glm::vec3 pos, float radiuss);
bool isBallInHole(Ball *ball);
void resToBallInHole(Ball *ball);
public:
glm::vec3 pos;
float radius;
};
Binary file added Hole.o
Binary file not shown.
7 changes: 5 additions & 2 deletions Makefile.win
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
CPP = g++.exe -D__DEBUG__
CC = gcc.exe -D__DEBUG__
WINDRES = windres.exe
OBJ = main.o Model_OBJ.o Ball.o Table.o
LINKOBJ = main.o Model_OBJ.o Ball.o Table.o
OBJ = main.o Model_OBJ.o Ball.o Table.o Hole.o
LINKOBJ = main.o Model_OBJ.o Ball.o Table.o Hole.o
LIBS = -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib32" -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib32" -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib" -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib" -static-libgcc -L"C:/Users/Tuan/Desktop/3D-Billiards-Game-OpenGL/lib" -lglut32 -lglu32 -lSOIL -lopengl32 -lwinmm -lgdi32 -m32 -g3
INCS = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.8.1/include"
CXXINCS = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.8.1/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++"
Expand Down Expand Up @@ -35,3 +35,6 @@ Ball.o: Ball.cpp

Table.o: Table.cpp
$(CPP) -c Table.cpp -o Table.o $(CXXFLAGS)

Hole.o: Hole.cpp
$(CPP) -c Hole.cpp -o Hole.o $(CXXFLAGS)
15 changes: 10 additions & 5 deletions Table.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#include "Table.h"

Table::Table(char* objFile, bool hasTexture, map<string, texture>* textures):Model_OBJ(objFile, hasTexture, textures){
this->left = -0.42;
this->right = 0.42;
this->top = -0.17;
this->bottom = 0.18;
this->left = -0.46;
this->right = 0.46;
this->top = -0.19;
this->bottom = 0.2;
this->heigh = 0.2574;
holes[0] = new Hole(glm::vec3(-0.46388,heigh,0.22166), 0.04);
holes[1] = new Hole(glm::vec3(-0.46388, heigh, -0.20933), 0.04);
holes[2] = new Hole(glm::vec3(0.00469, heigh, -0.22465), 0.04);
holes[3] = new Hole(glm::vec3(0.00479, heigh, 0.23079), 0.04);
holes[4] = new Hole(glm::vec3(0.47007, heigh, -0.20933), 0.04);
holes[5] = new Hole(glm::vec3(0.47007, heigh, 0.22153), 0.04);
}

bool Table::isBallHitLeftBorder(Ball *ball){
Expand Down
3 changes: 2 additions & 1 deletion Table.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "Ball.h"
#include "Hole.h"
class Table: public Model_OBJ{
public:
Table(){}
Expand All @@ -11,4 +11,5 @@ class Table: public Model_OBJ{
public:
float left, right, top , bottom;
float heigh;
Hole* holes[6];
};
Binary file modified Table.o
Binary file not shown.
52 changes: 36 additions & 16 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <windows.h>
#include <windows.h>
#include <GL/glut.h>
#include <iostream>
#include <fstream>
Expand Down Expand Up @@ -48,7 +48,7 @@ class ball {
ball testball(0.000,0.000, 0.000);

float PI = 3.14;
Vector3f position(0, 0, 2);
Vector3f position(1,2.1,0);
float distanceFromObject = 5;
float angleAroundObject = 0;
float pitch = 20;
Expand Down Expand Up @@ -192,39 +192,62 @@ void drawFloor()
glVertex3f(table.right,table.heigh, table.top);
glVertex3f(table.left,table.heigh, table.top);
glEnd();
for (int i=0; i< 6; i++){
glPushMatrix();
glm::vec3 pos = table.holes[i]->pos;
glTranslatef(pos.x, pos.y, pos.z);
glutWireSphere(0.04,10,10);
glPopMatrix();
}
}


void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// gluLookAt( -1.1,1.1,0, 0.3,0,0, 0,1,0);
gluLookAt( position.x,position.y,position.z, 0.3,0,0, 0,1,0);
gluLookAt( -1,2.0,0, 0.1,0,0, 0,1,0);
drawFloor();
// gluLookAt( position.x,position.y,position.z, -0.2,0,0, 0,1,0);

// Draw objects
// check in holes

for (int i=0; i< numOfBall; ++i){
if (!balls[i]->isInHole)
for (int j=0; j< 6; ++j){
Hole* hole = table.holes[j];
if (hole->isBallInHole(balls[i])){
hole->resToBallInHole(balls[i]);
break;
}
}
}
// check colisions
for (int i=0; i< numOfBall; ++i){
for (int j=i+1; j< numOfBall; ++j){
if (balls[i]->isBallHit(balls[j]) ){
if ( !balls[i]->isInHole && !balls[j]->isInHole && balls[i]->isBallHit(balls[j]) ){
balls[i]->resToBallHit(balls[j]);
}
}
table.resToBallHitTable(balls[i]);
if ( !balls[i]->isInHole ) table.resToBallHitTable(balls[i]);
}


for (int i=0; i< numOfBall; ++i) balls[i]->draw();
table.draw();
chairs.draw();
room.draw();
// table.draw();
// chairs.draw();
// room.draw();

// update position and calculate rotate angle
float dt= 0.1;
for (int i=0; i< numOfBall; ++i){
balls[i]->pos = balls[i]->pos + balls[i]->vel*dt;
if (balls[i]->pos.y > 0.1)
balls[i]->pos = balls[i]->pos + balls[i]->vel*dt;
float stepLength = glm::length(balls[i]->vel*dt);
float rotateAngle = stepLength*180/(M_PI*balls[i]->radius);
balls[i]->angle += rotateAngle;
}
Sleep(10);
// Sleep(10);
glutSwapBuffers();
}

Expand Down Expand Up @@ -321,10 +344,7 @@ int main(int argc, char **argv)
balls[2] = new Ball("resource/Ball10.obj", 0, &textures);
balls[2]->pos = glm::vec3(0.4, 0.2774, -0.15);
balls[2]->vel = glm::vec3(-0.05, 0, -0.05);
balls[2]->acc = glm::vec3(-0.01, 0, -0.01);
balls[0]->col[0] = 1.0f;
balls[1]->col[1] = 1.0f;
balls[2]->col[2] = 1.0f;
balls[2]->acc = glm::vec3(-0.01, 0, -0.01);



Expand Down
Binary file modified main.o
Binary file not shown.

0 comments on commit 7060537

Please sign in to comment.