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; } 
  • With a certain value of 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 corresponding alpha value by practical consideration) go back to the usual drawing order. - ߊߚ߀ߘ
  • In other words, 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(); - ߊߚ߀ߘ

1 answer 1

You need to check the coordinates of the electron and the atom, and when they are approximately the same, then first draw an electron, and then an atom or not draw an electron at all. For example:

 if( !herosprite.getGlobalBounds().intersects( circle.getGlobalBounds() ) || circle.getPosition().x <herosprite.getPosition().x ) draw(circle);