被跳过的 C++ 函数

标签 c++ function class codeblocks

<分区>

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

演练 所以我用构造函数初始化并放置球。然后我使用 drawBalls 函数在屏幕上绘制球。绘制球后,我更新它们的位置并使用 moveBalls 函数移动它们。移动完每个球后,仍然在 moveball 函数中,我使用 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();
}
}

最佳答案

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

关于被跳过的 C++ 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34236259/

相关文章:

c# - 为什么接口(interface)支持多重继承而类在 C# 中不支持

java - MySQL 和 SQLite 类

C++文件读取错误

Javascript 工厂函数无法正常工作

c++ - 创建将模板类作为构造函数中的参数的模板类

python - 为字典列表创建过滤功能的方法

javascript - 在 javascript 中为数组和数组数组创建函数。

c++ - 尝试打开位于 win2k8 共享上的文件时,文件打开最初失败,但最终可以成功

c++ - 如何获取 vector::reserve() 分配的缓冲区地址?

c++ - 集合中的智能指针多态性