一维弹性碰撞

时间:2019-05-03 14:57:56

标签: c++

我正在尝试编写一个程序来解决x轴上2个球的弹性碰撞。

我实现了这些一维牛顿等式https://en.wikipedia.org/wiki/Elastic_collision#One-dimensional_Newtonian,但是该程序没有按预期运行,有人可以给我一个我所缺少的线索吗?谢谢 我的代码如下。

#include "ball.h"
#include <iostream>
#include <cmath>
Ball::Ball(float r, float pos_x, float pos_y, sf::Color color, sf::RenderWindow& window)
{
    m_mass =r;
    radius =r;
    c= color;
    x = pos_x;
    y = pos_y;
    vx=5;
    vy=5;
    m_window = &window;
    this->setFillColor(c);
    this->setRadius(radius);
    this->setPosition(x,y);
    this->setPointCount(20);
    this->setOrigin(radius, radius);
}

void Ball::moveBall()
{
    x+=vx;
    this->setPosition(x,300);

}

void Ball::bounce()
{
    if(x>= width - radius)
        vx = -vx;
    if(x<= 0 + radius)
        vx = -vx;
}

bool Ball::checkCollision(Ball &ball)
{
    distance_x = (x + radius) - (ball.x + ball.radius);
    distance_y = (y + radius) - (ball.y + ball.radius);
    distance = std::sqrt((distance_x*distance_x) + (distance_y*distance_y));


    if(distance <= radius + ball.radius)
    {
        std::cout << "collision detected" << "\n";
        return true;
    }
    else
        return false;
}

void Ball::resolveCollision(Ball &ball)
{
   vx  = (((m_mass- ball.m_mass) *vx) + (2*ball.m_mass*ball.vx)) /(m_mass+ball.m_mass);
   ball.vx  =  (((ball.m_mass-m_mass)*ball.vx) +(2* m_mass*vx))/(m_mass+ball.m_mass);


    std::cout << "vx " << vx << "\n";
    std::cout << "ball vx " << ball.vx << "\n";
    std::cout << "========================" << "\n";
}



void Ball::display()
{
    m_window->draw(*this);
}

1 个答案:

答案 0 :(得分:0)

您修改了import pickle # I'll use this later to test if pickle file exists import os.path # let's use a default dictionary object CURRENCIES = { "Pound-Sterling": 1, "Euro": 1, "US-Dollar": 1, "Japanese-Yen": 1 } def load_currencies(): """Load a dictionary object of currencies previously stored""" with open('Currencies.pkl', 'rb') as f: dict = pickle.load(f) return dict def dump_currencies(dict): """Dump a dictionary object of currencies""" with open('Currencies.pkl', 'wb') as f: pickle.dump(dict, f) def print_currencies(dict): for currency in dict: if currency != "Pound-Sterling": print("£1 is", dict[currency], currency) # this is a special line of code that says "Hey if run me # directly run this code but if you import me don't" # - the code that runs effectively starts from here if __name__ == '__main__': # if our pickle file does not exist create it using the default if not os.path.isfile('Currencies.pkl'): dump_currencies(CURRENCIES) print("This program allows you to check and change the currency conversion rates before using the converter program.") CURRENCIES = load_currencies() print_currencies(CURRENCIES) answer = input("Do you want to change an exchange rate for a currency (Y/N):") if answer == 'Y': for currency in CURRENCIES: # print(currency) # print(type(currency)) <-- string print("£1 is", CURRENCIES[currency], currency) New_rate = float(input("Please input the new rate for "+currency+":")) CURRENCIES[currency] = New_rate dump_currencies(CURRENCIES) ,但是在计算vx时意外地使用了vx的修改后的值,而您实际上打算使用{{1 }}在ball.vx的计算中,然后才被碰撞调整修改。

vx

只需将ball.vx的值保留在一个临时变量中,以便可以将其原始值用于计算vx = (((m_mass- ball.m_mass) *vx) + (2*ball.m_mass*ball.vx)) /(m_mass+ball.m_mass); ball.vx = (((ball.m_mass-m_mass)*ball.vx) +(2* m_mass*vx))/(m_mass+ball.m_mass); ^^

vx