0
최근 SFML 라이브러리와 간단한 게임을 만드는 법을 배우기 시작했습니다. 그런 다음 아주 간단한 애니메이션을 만들기로했습니다. 원자 주위를 이동하는 전자. 객체의 좌표, X0, Y0 - - 나는 x는, y는이 공식 C++ OpenGL SFML
x = x0 + radius*sin(angle);
y = y0 + radius*cos(angle);
사용 중심
의 좌표가 뭔가를 만들어을하지만 몇 가지 문제가있다. 원자 주위에서 전자와이 움직임을 볼 수 없었습니다. 나는 이동을 위해 루프를 사용해야한다고 생각하지만 확실하지 않습니다.
#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);
죄와 왜냐하면 일반적으로 라디안이 걸릴 그래서 각도를 증가 '알파
이
내 코드입니다 '를 1로 돌리면 단위계가 바뀝니다. – StarShine@StarShine 오, 내가 섞였다. 왜냐하면 나는'alpha + = deltaTime * k'를 사용해야하기 때문에 시간에 따른 각도 변화입니다. 그리고 시계를 사용하려고했는데 오류 cuase alpha 값 float 및 clock - Clock이 있습니다. 하지만 수식 사용법을 이해하지 못합니다. 나는 이것을 위해 루프를 사용 하는가? – Lado
https://www.sfml-dev.org/documentation/2.0/classsf_1_1Clock.php의 문서에 따르면 루프 전에 한 번 restart() 함수를 호출하고 getElapsedTime()을 사용하여 경과 된 시간 (초)을 폴링 할 수 있다고합니다. 'angle = clock.getElapsedTime() * rotationSpeed;'라고 쓸 수 있습니다. – StarShine