This code simulates a simple gravity system using the SFML library. It creates two objects that exert a gravitational pull on a number of particles. The particles move according to the laws of classical mechanics, specifically using Verlet integration. The code also includes a function to map a value to a color, which is used to color the particles based on an index.
The so called 'star' declaration.
It has the following attributes:
pos
: The position of the gravity source as a 2D vector.
gravStrength
: The strength of the gravity exerted by the source.
circle
: A circle shape used to represent the source visually. Made using SFML's CircleShape.
It has the following methods:
GravitySource(float posX, float posY, float gravStrength)
: Constructor that initializes the position and gravity strength of the source.
render(sf::RenderWindow& wind)
: Draws the circle representing the source on the window.
getPosition()
: Returns the position of the source as a 2D vector.
getGravStrength() const
: Returns the strength of the gravity exerted by the source.
It has the following attributes:
pos
: The position of the particle as a 2D vector.
velo
: The velocity of the particle as a 2D vector.
circle
: A circle shape used to represent the particle visually. Made using SFML's CircleShape.
It has the following methods:
Particles(float posX, float posY,float velX, float velY)
: Constructor that initializes the position and velocity of the particle.
render(sf::RenderWindow& wind)
: Draws the circle representing the particle on the window.
updatePhysics(GravitySource& circle)
: Updates the position and velocity of the particle based on the gravitational pull of the source.
setColour(sf::Color colour)
: Sets the color of the particle.
The main
function creates a window, initializes two gravity sources, and a number of particles.
It then enters a loop that:
Handles window closing events.
Clears the window.
Updates the position and velocity of each particle based on the gravity from each source.
Draws all the gravity sources and particles on the window.
Displays the window contents.
Additional Notes:
The code uses Verlet integration to update the position and velocity of the particles.
The color of each particle is based on its index in the particle list.