Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SFML具有随机值的多个对象及其碰撞 我现在很喜欢SMFL和C++,我在用球创建非常简单的物理模拟器时卡住了。_C++_Sfml - Fatal编程技术网

SFML具有随机值的多个对象及其碰撞 我现在很喜欢SMFL和C++,我在用球创建非常简单的物理模拟器时卡住了。

SFML具有随机值的多个对象及其碰撞 我现在很喜欢SMFL和C++,我在用球创建非常简单的物理模拟器时卡住了。,c++,sfml,C++,Sfml,这是my main.cpp: #include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Window/Event.hpp> #include "ball.hpp" int main() { /*create window settings*/ sf::ContextSettings settings; /*create window*/ sf::RenderWindow windo

这是my main.cpp:

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>
#include "ball.hpp"

int main()
{
    /*create window settings*/
    sf::ContextSettings settings;

    /*create window*/
    sf::RenderWindow window;
    window.create(sf::VideoMode(600, 600), "Simple Physics", sf::Style::Default, settings);

    /*create ball(s)*/
    Ball ball;

    /*Main loop*/
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear(sf::Color::Black);

        // call ball.update(); and ball.draw();
        balls.update();
        balls.draw(window);
        window.display();
    }
}
#包括
#包括
#包括“ball.hpp”
int main()
{
/*创建窗口设置*/
sf::上下文设置;
/*创建窗口*/
sf::RenderWindow窗口;
创建(sf::VideoMode(600600),“简单物理”,sf::Style::Default,设置);
/*创建球(s)*/
球;
/*主回路*/
while(window.isOpen())
{
sf::事件;
while(window.pollEvent(事件))
{
如果(event.type==sf::event::Closed)
window.close();
}
窗口。清除(sf::颜色::黑色);
//调用ball.update();和ball.draw();
更新();
球。画(窗);
window.display();
}
}
和ball.hpp:

#include <SFML/Graphics/CircleShape.hpp>

class Ball
{
public:

int minXY = 0;
int maxXY = 600;
int ballRadius = rand() % 50 + 5;        
int random = rand() % maxXY + 1;   //random XY position

    // vector for positions
    //sf::Vector2f pos{random, random};  ->  I Guess this should be included into loop
    sf::Vector2f pos{100, 300};
    // vector for velocity
    sf::Vector2f vel{ 0.1, 0.1 };

    void update()
    {
        // factors influence velocity
       // update position based on velocity
        pos.x += vel.x;
        pos.y += vel.y;

        if (pos.x + ballRadius*2 > maxXY || pos.x < minXY) vel.x = -vel.x; //boundary cond
        if (pos.y + ballRadius*2 > maxXY || pos.y < minXY) vel.y = -vel.y; //boundary cond
    }

    void draw(sf::RenderWindow& window)
    {
        // draw ball to the window using position vector
        sf::CircleShape circle(ballRadius);
        circle.setPosition(pos.x, pos.y);
        circle.setFillColor(sf::Color::White);

        window.draw(circle);
    }
};
#包括
班级舞会
{
公众:
int minXY=0;
int maxXY=600;
int ballRadius=rand()%50+5;
int random=rand()%maxXY+1;//随机XY位置
//位置向量
//向量2f pos{random,random};->我想这应该包括在循环中
sf::Vector2f pos{100300};
//速度矢量
向量2f层{0.1,0.1};
无效更新()
{
//影响速度的因素
//基于速度更新位置
位置x+=水平x;
位置y+=水平y;
如果(位置x+ballRadius*2>maxXY | |位置xmaxXY | |位置y
现在,我想画多个圆环,大小、速度和颜色都是随机的。 它们应该在碰撞时反弹,我对此有一些想法,但如果没有多个球,就不能真正尝试它们。 我曾尝试使用SimpleFor循环实现它,但它不起作用。
在阅读和观看了许多教程之后,我仍处于这一点上,因此如果有人能帮助我提供任何工作示例或对代码进行一些改进,那就太好了。

我不会给您提供工作示例,但我想我可以为您指出正确的方向:

  • 您需要为您的Ball类使用某种构造函数,在这个示例中,您可能需要了解更多关于类的一般信息(例如)
  • 然后你应该把你从主循环中产生的球以某种形式储存起来。如果你确实知道你想要繁殖的球的数量,那么你可以选择,否则就是最好的选择
  • 您需要一些函数来检查碰撞,Ball类的函数应该可以做到这一点