Hello. I have a task to develop the motion of an electron in the circle of an atom, it should be like an animation. I used the SFML library for this. I sort of realized it, but at some point the electron goes off the screen. Then I donβt know how to implement it so that the object (electron) would not be visible when it is behind an atom. Thank you in advance.
I used this formula to go around
x = x0 + radius*sin(angle); y = y0 + radius*cos(angle); alpha += deltaTime * k; where x0 and y0 are the coordinates of the center; x, y - the coordinates of the object, and alpha - the angle.
alpha += deltaTime * k it is a change in angle over time.
Here is my code:
#include "stdafx.h" #include <iostream> #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 = 2000.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); //Π²ΡΠ²ΠΎΠ΄ΠΈΠΌ ΡΠΏΡΠ°ΠΉΡ Π² ΠΏΠΎΠ·ΠΈΡΠΈΡ xy (output the sprite to the position xy) 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; clock.restart(); k = k / 800; float deltaTime = clock.restart().asSeconds(); alpha += deltaTime*(float)clock.getElapsedTime().asSeconds(); float Radius = (float)herosprite.getPosition().x - (float)circle.getPosition().x; 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*sin(alpha);//ΡΠΎΡΠΌΡΠ»Π° Π΄Π»Ρ Π΄Π²ΠΈΠΆΠ΅Π½ΠΈΡ ΠΏΠΎ ΠΎΠΊΡΡΠΆΠ½ΠΎΡΡΠΈ(formula for motion along a circle) y = yo + Radius*cos(alpha);//ΡΠΎΡΠΌΡΠ»Π° Π΄Π»Ρ Π΄Π²ΠΈΠΆΠ΅Π½ΠΈΡ ΠΏΠΎ ΠΎΠΊΡΡΠΆΠ½ΠΎΡΡΠΈ(formula for motion along a circle) Vector2f direction(x, y); circle.move(direction * deltaTime * speed); //circle.move(direction); //circle.move(x, y); window.clear(); window.draw(herosprite); window.draw(circle); window.display(); } return 0; }
alpha(when the electron begins to go behind the nucleus, pick up the value by experience) you need to draw objects in the reverse order. As soon as the electron is completely out of the nucleus (also select the correspondingalphavalue by practical consideration) go back to the usual drawing order. - ίίί€ίwindow.clear(); if(alpha > XXX && alpha < YYY) {window.draw(circle); window.draw(herosprite);} else {window.draw(herosprite); window.draw(circle);} window.display();window.clear(); if(alpha > XXX && alpha < YYY) {window.draw(circle); window.draw(herosprite);} else {window.draw(herosprite); window.draw(circle);} window.display();- ίίί€ί