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

Commit

Permalink
fix balls rolling error
Browse files Browse the repository at this point in the history
  • Loading branch information
thongnt99 committed May 19, 2015
1 parent 66e8e64 commit 40fc83d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 37 deletions.
Binary file modified 3D-Billiards.exe
Binary file not shown.
8 changes: 8 additions & 0 deletions 3D-Billiards.layout
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Editors]
Order=0
Focused=0
[Editor_0]
CursorCol=37
CursorRow=406
TopLine=397
LeftChar=1
11 changes: 7 additions & 4 deletions Ball.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#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;
this->mass = 1;
this->orientation = glm::vec3(0,0,0);
}

bool Ball::isBallHit(Ball* ball){
Expand Down Expand Up @@ -37,12 +38,14 @@ void Ball::resToBallHit(Ball *ball){
ball->pos = ball->pos - normal*tmp;

}
void Ball::draw(){
void Ball::draw(){
if (glm::length(vel) > 0) orientation = vel;
if ( isInHole ) orientation = glm::vec3(0,0,0);
glPushMatrix();
glTranslatef(pos.x, pos.y , pos.z);
if (angle >= 360) angle =0;
if (!isInHole && glm::length(vel) >0 ){
glRotatef(-angle,-vel.z,0,vel.x);
if (!isInHole && glm::length(orientation) != 0 ){
glRotatef(-angle,-orientation.z,0,orientation.x);
}
Model_OBJ::draw();
glPopMatrix();
Expand Down
3 changes: 2 additions & 1 deletion Ball.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class Ball: public Model_OBJ{
float radius;
float mass;
glm::vec3 pos;
glm::vec3 vel;
glm::vec3 vel;
glm::vec3 orientation;
float angle = 0;
bool isInHole = false;
};
6 changes: 3 additions & 3 deletions Hole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ bool Hole::isBallInHole(Ball *ball){
if ( length < (this->radius + ball->radius))
return true;
else
return false;
return false;
}

void Hole::resToBallInHole(Ball *ball){
void Hole::resToBallInHole(Ball *ball){
ball->pos = this->pos;
ball->isInHole = true;
ball->vel = glm::vec3(0,-0.05,0);
ball->vel = glm::vec3(0,-0.05,0);
}
8 changes: 4 additions & 4 deletions Makefile.win
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Project: 3D-Billiards
# Makefile created by Dev-C++ 5.7.1
# Makefile created by Dev-C++ 5.8.3

CPP = g++.exe -D__DEBUG__
CC = gcc.exe -D__DEBUG__
WINDRES = windres.exe
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/MinGW32/lib" -L"C:/Program Files (x86)/Dev-Cpp/MinGW32/mingw32/lib" -static-libstdc++ -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/MinGW32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW32/mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW32/lib/gcc/mingw32/4.8.1/include" -I"C:/Users/Tuan/Desktop/3D-Billiards-Game-OpenGL/lib"
CXXINCS = -I"C:/Program Files (x86)/Dev-Cpp/MinGW32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW32/mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW32/lib/gcc/mingw32/4.8.1/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW32/lib/gcc/mingw32/4.8.1/include/c++" -I"C:/Users/Tuan/Desktop/3D-Billiards-Game-OpenGL/lib"
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++"
BIN = 3D-Billiards.exe
CXXFLAGS = $(CXXINCS) -m32 -g3 -fpermissive
CFLAGS = $(INCS) -m32 -g3
Expand Down
41 changes: 16 additions & 25 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,14 @@ void checkInHoles(){
break;
}
}
}
flag = false;
}
if (balls[0]->isInHole){
for (int i=0; i< numOfBall; ++i){
if (glm::length(balls[i]->vel) != 0){
flag = true;
break;
}
}
if ( flag == false){
Sleep (200);
balls[0]->pos = glm::vec3(-0.3, table.heigh + balls[0]->radius, 0);
balls[0]->vel = glm::vec3(0, 0, 0);
balls[0]->isInHole = false;
}
for (int i=0; i< numOfBall; ++i)
if (glm::length(balls[i]->vel) != 0) return;
Sleep(200);
balls[0]->pos = glm::vec3(-0.3, table.heigh + balls[0]->radius, 0);
balls[0]->vel = glm::vec3(0, 0, 0);
balls[0]->isInHole = false;
}
}

Expand All @@ -216,18 +209,16 @@ void checkColisions(){

void updateBalls(){
float dt= 0.05;
for (int i=0; i< numOfBall; ++i){
float stepLength = glm::length(balls[i]->vel)*dt;
float rotateAngle = stepLength*180/(M_PI*balls[i]->radius);
balls[i]->angle += rotateAngle;

for (int i=0; i< numOfBall; ++i){
if ( glm::length(balls[i]->vel) < 0.01)
balls[i]->vel = glm::vec3(0,0,0);
else {
glm::vec3 acc = glm::normalize(balls[i]->vel)*-0.1f;
balls[i]->vel = balls[i]->vel + acc*dt;
}

float stepLength = glm::length(balls[i]->vel)*dt;
float rotateAngle = stepLength*180/(M_PI*balls[i]->radius);
balls[i]->angle += rotateAngle;
if (balls[i]->pos.y > 0.22)
balls[i]->pos = balls[i]->pos + balls[i]->vel*dt;
}
Expand Down Expand Up @@ -334,12 +325,12 @@ void draw3DScence()
checkColisions();

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

void draw2DHUD(){
Expand Down

0 comments on commit 40fc83d

Please sign in to comment.