[英]C++ OpenGL SFML
最近,我開始學習SFML庫以及如何制作簡單的游戲。 然后,我決定制作非常簡單的動畫。 在原子周圍移動的電子。 我用這個公式
x = x0 + radius*sin(angle);
y = y0 + radius*cos(angle);
其中x,y-對象坐標,x0,y0-中心坐標
我做了點事,但是有一些問題。 我看不見我的電子以及原子周圍的運動。 我認為我應該使用循環移動,但是我不確定。
這是我的代碼:
#include "stdafx.h"
#include <SFML/Graphics.hpp>
using namespace sf;
int main()
{
float xo = 250;//координаты центра(coordinates of the center)
float yo = 250;//координаты центра(coordinates of the center)
float radius = 10;//радиус
float k = 0.0f;//время(time)
float x = 130;//координаты объекта(object coordinates)
float y = 330;//координаты объекта(object coordinates)
float rotation = 5.0;
//float x = 1000 / 2;
//float y = 1000 / 2;
float alpha = 0;//угол(angle)
RenderWindow window(sf::VideoMode(1000, 1000), "Atom");
window.setFramerateLimit(60);
Clock clock;
sf::Event windowEvent;
Texture herotexture;
herotexture.loadFromFile("atom_image.png");
Sprite herosprite;
herosprite.setTexture(herotexture);
//herosprite.setTextureRect(IntRect(0, 99, 48, 51));//получили нужный нам прямоугольник с котом
herosprite.setPosition(250, 250); //выводим спрайт в позицию x y (output the sprite to the position x y)
CircleShape circle;
circle.setRadius(radius);
circle.setOutlineColor(Color::Red);
circle.setOutlineThickness(5);
circle.setPosition(x, y);
circle.setRotation(0);
bool track;
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
circle.rotate(rotation);
//alpha++;
alpha+=deltaTime*k;
x = xo + radius*sin(alpha);//формула для движения по окружности(formula for motion along a circle)
y = yo + radius*cos(alpha);//формула для движения по окружности(formula for motion along a circle)
//Vector2f direction(x, y);
//float speed = 20.0f;
//circle.move(direction * k * speed);
circle.move(x, y);
/*(if (Keyboard::isKeyPressed(Keyboard::Left)) { herosprite.move(-0.1, 0); } //первая координата Х отрицательна =>идём влево
if (Keyboard::isKeyPressed(Keyboard::Right)) { herosprite.move(0.1, 0); } //первая координата Х положительна =>идём вправо
if (Keyboard::isKeyPressed(Keyboard::Up)) { herosprite.move(0, -0.1); } //вторая координата (У) отрицательна =>идём вверх (вспоминаем из предыдущих уроков почему именно вверх, а не вниз)
if (Keyboard::isKeyPressed(Keyboard::Down)) { herosprite.move(0, 0.1); } //вторая координата (У) положительна =>идём вниз (если не понятно почему именно вниз - смотрим предыдущие уроки)
*/
window.clear();
window.draw(herosprite);
window.draw(circle);
k = clock.restart().asSeconds();
window.display();
}
return 0;
}
編輯
@StarShine的一些更正:
circle.rotate(rotation);
x = xo + radius*sin(alpha); //формула для движения по окружности(formula for motion along a circle)
y = yo + radius*cos(alpha); //формула для движения по окружности(formula for motion along a circle)
Vector2f direction(x, y);
float speed = 20.0f;
circle.move(direction * k * speed);
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
int main()
{
float xo = 250;//координаты центра(coordinates of the center)
float yo = 250;//координаты центра(coordinates of the center)
float radius = 10;//радиус
float k = 0.0f;//время(time)
float x = 130;//координаты объекта(object coordinates)
float y = 330;//координаты объекта(object coordinates)
float rotation = 5.0;
/*float x = 1000 / 2;
float y = 1000 / 2;*/
float alpha = 0;//угол(angle)
float speed = 200.0f;
RenderWindow window(sf::VideoMode(1000, 1000), "Atom");
window.setFramerateLimit(60);
Clock clock;
sf::Event windowEvent;
Texture herotexture;
herotexture.loadFromFile("atom_image.png");
Sprite herosprite;
herosprite.setTexture(herotexture);
//herosprite.setTextureRect(IntRect(0, 99, 48, 51));//получили нужный нам прямоугольник с котом
herosprite.setPosition(250, 250); //выводим спрайт в позицию x y (output the sprite to the position x y)
CircleShape circle;
circle.setRadius(radius);
circle.setOutlineColor(Color::Red);
circle.setOutlineThickness(5);
circle.setPosition(x, y);
circle.setRotation(0);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
/*circle.rotate(rotation);
alpha = (float)clock.getElapsedTime().asMicroseconds() * k;*/
float Radius = sqrt(pow(((float)circle.getPosition().x - (float)herosprite.getPosition().x), 2.0f) + pow(((float)circle.getPosition().y - (float)herosprite.getPosition().y), 2.0f));
clock.restart();
k = k / 800;
float deltaTime = clock.restart().asSeconds();
alpha += deltaTime*(float)clock.getElapsedTime().asSeconds();
cout << "\nGet Position Atom is -> " << (float)herosprite.getPosition().x << endl;
cout << "\nGet Position Circle is -> " << (float)circle.getPosition().x << endl;
cout << "\nRadius is -> " << Radius << endl;
x = xo + Radius*cos(alpha) * speed;//формула для движения по окружности(formula for motion along a circle)
y = yo + Radius*sin(alpha) * speed;//формула для движения по окружности(formula for motion along a circle)
cout << "x is -> " << x << endl;
Vector2f direction(x, y);
circle.move(direction * deltaTime * speed);
if (sqrt(pow(((float)circle.getPosition().x - (float)herosprite.getPosition().x), 2.0f) + pow(((float)circle.getPosition().y - (float)herosprite.getPosition().y), 2.0f)) > (float)herosprite.getPosition().x && sqrt(pow(((float)circle.getPosition().x - (float)herosprite.getPosition().x), 2.0f) + pow(((float)circle.getPosition().y - (float)herosprite.getPosition().y), 2.0f)) > (float)herosprite.getPosition().y)
{
circle.move(-direction * deltaTime * speed);
float cd = circle.getPosition().x + 40;
circle.move(cd, circle.getPosition().y);
circle.setPosition(130, 330);
}
/*circle.move(direction);
circle.move(x, y);*/
window.clear();
window.draw(herosprite);
window.draw(circle);
window.display();
}
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.