私はC ++でのプログラミングに非常に慣れていないので、SFMLを使用して、跳ね返るボールを作成できる簡単なプログラムを作成しようとしています。しかし、私は物理学のシムに着く前に立ち往生してしまいました。ボールクラスが機能していないようです。これを使用して、作成された各ボールの変数を格納し、関数を更新および描画したいと思いました。しかし、ある種のエラーが発生するたびに、明らかに非常に単純なものについてのヘルプを見つけることができません。
#include <iostream>
#include <SFML\Audio.hpp>
#include <SFML\Graphics.hpp>
#include <SFML\Network.hpp>
#include <SFML\System.hpp>
#include <SFML\Window.hpp>
class Ball
{
public:
// vector for position
sf::Vector2 pos(100,100);
// vector for velocity
sf::Vector2 vel(0,0);
void update()
{
// factors influence velocity
// update position based on velocity
pos.x += vel.x;
pos.y += vel.y;
}
void draw()
{
// draw ball to the window using position vector
sf::circleShape circle(10);
circle.setPosition(pos.x,pos.y);
circle.setFillColor(sf::Color::White);
window.draw(circle);
}
};
int main()
{
/*create window settings*/
sf::ContextSettings settings;
settings.antialiasingLevel = 8; // set the antialiasing level
/*create window*/
sf::RenderWindow window;
window.create(sf::VideoMode(800, 600), "Simple Physics", sf::Style::Default, settings);
/*create ball(s)*/
Ball ball01;
/*Main loop*/
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: close the window
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::White);
// call ball.update(); and ball.draw();
ball01.update();
ball01.draw();
window.display();
}
}
事前に感謝します、どんな助けや批評も大歓迎です!
このスニペットを試してください:
1)背景色はボールの色とは異なる必要があります。
2)window.setFramerateLimit()を使用して、アニメーションの速度を落とします。
3)ボールが視界から外れないように境界条件を使用します。
4)Draw()も修正が必要です
class Ball
{
public:
// vector for position
sf::Vector2f pos{ 100, 100 };
// vector for velocity
sf::Vector2f vel{ 1, 1 };
void update()
{
// factors influence velocity
// update position based on velocity
pos.x += vel.x;
pos.y += vel.y;
if (pos.x > 800 || pos.x < 0) vel.x = -vel.x; //boundary cond
if (pos.y > 600 || pos.y < 0) vel.y = -vel.y; //boundary cond
}
void draw(sf::RenderWindow& window)
{
// draw ball to the window using position vector
sf::CircleShape circle(10);
circle.setPosition(pos.x, pos.y);
circle.setFillColor(sf::Color::White);
window.draw(circle);
}
};
int main()
{
/*create window settings*/
sf::ContextSettings settings;
settings.antialiasingLevel = 8; // set the antialiasing level
/*create window*/
sf::RenderWindow window;
window.create(sf::VideoMode(800, 600), "Simple Physics", sf::Style::Default, settings);
/*create ball(s)*/
Ball ball01;
window.setFramerateLimit(60); //slow down speed
/*Main loop*/
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: close the window
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::Black); // ball is white so make backgnd black
// call ball.update(); and ball.draw();
ball01.update();
ball01.draw(window);
window.display();
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加