正在跳过的C ++函数

时间:2015-12-12 04:30:48

标签: c++ function class codeblocks

所以我正在尝试进行一个简单的池球模拟,当我试图检查球之间的碰撞时,我的bounce函数在循环中被跳过。控制台上应该显示bounce文件中函数PoolTable.cpp中的随机字母,但它会被跳过并且不处理命中或将文本输出到控制台。不知道为什么它没有运行该功能。没有警告。没有错误。编译好。我在Windows机器上,使用代码块和GLUT库/项目。

操作实例 所以我初始化并用构造函数放置球。然后我用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 个答案:

答案 0 :(得分:1)

void checkCollisions();(在moveBalls中)是函数原型,而不是函数调用。删除void