2015-12-12 115 views
0

所以我試圖做一個簡單的池球模擬,當試圖檢查球之間的碰撞時,我的bounce函數在循環中被跳過。在控制檯上應該有一個顯示,PoolTable.cpp文件中的函數bounce中有隨機字母,但其跳過並不會處理命中或將文本輸出到控制檯。不知道爲什麼它沒有運行該功能。沒有警告。沒有錯誤。編譯好。我在Windows機器上,使用代碼塊和GLUT庫/項目。正在跳過C++函數

演練 所以我初始化並將球放置在構造函數中。然後用drawBalls函數在屏幕上畫球。繪製球后,我更新它們的位置並使用moveBalls函數移動它們。在移動每個球之後,仍然在移動球功能中,我使用checkCollisions功能檢查碰撞。 checkCollisions然後啓動兩個for循環,但從不運行反彈功能,因爲球不會互相反彈,而且cout不會打印在終端中。由於某種原因它被跳過。

PoolTable.cpp

#include "PoolTable.h" 
#include "poolball.h" 
#include "Graphics.h" 
#include <iostream> 
using namespace std; 
#include <cmath> 

PoolTable::PoolTable(int x){ 
placeBalls(x); 
} 

void PoolTable::placeBalls(int x){ 
number_of_balls = x; 
for(int i = 0; i < x; i++){ 
    balls[i].setX(balls[i].getRadius() + i * 20); 
    balls[i].setY(balls[i].getRadius() + i * 30); 
} 
} 

double find_angle(double vx, double vy) { 
// determine the angle between poolballs when they collide 
double t; double PI = acos(-1.0); 
if(vx < 0) // vertical collision 
    t = PI + atan(vy/vx); 
else if(vx > 0.0 && vy >= 0.0) // 1st quardant collision 
    t = atan(vy/vx); 
else if(vx > 0.0 && vy < 0.0) // 
    t = 2.0*PI + atan(vy/vx); 
else if(vx == 0.0 && vy == 0.0) 
    t = 0.0; 
else if(vx == 0 && vy >= 0.0) 
    t = PI/2.0; 
else 
    t = 1.5 * PI; 
return t; 
} 


void PoolTable::bounce(int i, int j) { 
cout << "klasdjflkadsjflkasjfsadk" << endl; 
double PI = acos(-1.0); 
double x1 = balls[i].getX(); 
double y1 = balls[i].getY(); 
double x2 = balls[j].getX(); 
double y2 = balls[j].getY(); 
double dx = x2 - x1; 
double dy = y2 - y1; 
double dist = sqrt(dx*dx+dy*dy); 

// did a collision occur 
if(dist <= 2 * balls[i].getRadius()) { 

    double phi; // angle between the two ball centers 
    if(dx == 0.0) 
     phi = PI/2.0; 
    else 
     phi = atan2 (dy, dx); 
    // now compute the total velocities of the two balls 
    double vx1 = balls[i].xSpeed; 
    double vy1 = balls[i].getYSpeed(); 
    double v1total = sqrt(vx1*vx1 + vy1*vy1); 
    double vx2 = balls[j].getXSpeed(); 
    double vy2 = balls[j].getYSpeed(); 
    double v2total = sqrt(vx2*vx2 + vy2*vy2); 

    // find the angle of each ball's velocity 
    double ang1 = find_angle(vx1,vy1); 
    double ang2 = find_angle(vx2,vy2); 

    // transform velocities into normal.tangential components 
    double v1xr = v1total * cos(ang1 - phi); 
    double v1yr = v1total * sin(ang1 - phi); 
    double v2xr = v2total * cos(ang2 - phi); 
    double v2yr = v2total * sin(ang2 - phi); 

    // now find the final velocities (assuming equal mass) 
    double v1fxr = v2xr; 
    double v2fxr = v1xr; 
    double v1fyr = v1yr; 
    double v2fyr = v2yr; 

    // reset the velocities 
    balls[i].setXSpeed(cos(phi)*v1fxr + cos(phi+PI/2)*v1fyr); 
    balls[i].setYSpeed(sin(phi)*v1fxr + sin(phi+PI/2)*v1fyr); 
    balls[j].setXSpeed(cos(phi)*v2fxr + cos(phi+PI/2)*v2fyr); 
    balls[j].setYSpeed(sin(phi)*v2fxr + sin(phi+PI/2)*v2fyr); 
} 
} 

void PoolTable::checkCollisions(void){ 
for(int i = 0; i < number_of_balls; i++){ 
    for(int j = i + 1; j < number_of_balls; j++){ 
     bounce(i, j); 
    } 
} 
} 

void PoolTable::moveBalls(void){ 
for(int i = 0; i < number_of_balls; i++){ 
    balls[i].move(); 
    void checkCollisions(); 
} 

} 

void PoolTable::drawBalls(void){ 
for(int i = 0; i < number_of_balls; i++){ 
    balls[i].draw(); 
} 
} 
+1

這是太多的代碼。請發佈[MCVE](http://stackoverflow.com/help/mcve)。 –

回答

1

void checkCollisions();(在moveBalls)是一個函數原型,而不是函數調用。刪除void