Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I can not get it to work with Particle any help would be create Description In t

ID: 3677031 • Letter: I

Question

I can not get it to work with Particle any help would be create

Description In this assignment we will simulate gravity and elastic collisions on solid objects and draw them using the Simple DirectMedia Layer 2 (SDL2) library. I will provide most of the code that interacts with SDL2 itself so you should focus on implementing the physics. In order to use SDL2, you must have the library installed. You can obtain it from https://www.libsdl.org/. Note: in some of the provided code I have used C++11 features. If you do not have access to C++11, please contact me and I will show you what needs to be modified. If you have been compiling on Cal Poly’s servers, you will need a way of compiling locally. I can provide a virtual machine image that you can use to compile and test the program if you need it. Simple Game Engine We will be implementing a simple game engine but for this assignment it is not necessary to handle any user input. Extra credit will be given to students that add more than is required for this project such as handling user input and adding game features like keeping player score, etc. In general, game engines follow a structure that we can describe in pseudo-code as: initialize(); while (running) { handleEvents(); 1 update(); render(); } cleanup(); Each function will be described below: 1. The initialize() function will do any setup required by the game. For example, in our assignment one step here is to randomly generate particles for the game. We also need to initialize the SDL2 library. 2. The running condition should stay true while the game is still being played. When the game should exit for whatever reason switch this condition to allow for proper cleanup. 3. The handleEvents() function will handle any user input events such as moving a character or maybe even network events for a multiplayer game. 4. The update() function has all of the game logic updates. For example, all physics calculations for our assignment should be handled here. 5. The render() function renders the game objects to the screen. For example, in our assignment this will render the list of particles. 6. The cleanup() function handles cleaning up any resources allocated for the game. In our case, this is used to cleanup the SDL2 library. I have provided the code for the initialize(), render(), and cleanup() functions because they deal mostly with the SDL2 library. Our handleEvents() function will be mostly empty, but I have provided handling for closing the game window. This “game loop” already exists in the operator() function in the Game class. Your main task is to implement the update() function, which will handle the physics calculations between particles for a single fraem. You are passed the amount of time in seconds since the last frame as a parameter to the update function. You must also implement a Particle class and Point class. The Point class The Point class should be a class for a two dimensional point and have two double instance variables named x and y. We can use this class for both the position and velocity (in which case, we’re really using this as a vector, but we’ll still call it Point so we don’t get it confused with std::vector). Include getters for x and y. The Particle class Your Particle class must have a two dimensional position and velocity (x and y dimensions), radius, and mass. The position and velocity should be stored using Point objects and the mass and radius should be doubles. Include getters for all four instance variables. The Game class Most of this class has already been provided. You must implement the update function to handle updating Particle positions and velocities based on Newton’s law of universal gravitation. When two particles collide, you should use the physics of elastic collisions to handle the collision. 2 For extra credit, you may also include event handling or any other additional features. Credit will be awarded based on creativity and functionality. Newton’s law of universal gravitation Newton’s second law of motion for constant-mass systems says that the net force (F) experienced by an object of mass m and acceleration a is: F = ma Newton’s law of universal gravitation says that the force due to gravity (F) between two objects is calculated as: F = G m1m2 r 2 where: • G is the universal gravitational constant with value 6.674 × 1011 • m1 is the mass of the first object • m2 is the mass of the second object • r is the distance between the centers of the masses If we want to determine how much one object (m1) is accelerated due to gravity with another object (m2), we can apply both laws: F1 = G m1m2 r 2 (1) m1a = G m1m2 r 2 (2) a = G m2 r 2 (3) However, we may have many different objects all applying gravitational force to each other. To simulate this, we will calculate the effect of gravity in discrete chunks (once per frame). We will do this with the following process: 1. Iterate over our list of objects: • For each other object in the list, calculate the acceleration due to gravity between our first object and this other object • Accelerate the first object by this amount (change its velocity based on the acceleration) 2. Iterate over our list of objects and shift each object’s position based on its velocity (which has the accumulated acceleration changes from gravitational force with all other objects) By doing it this way, we only change the position of each object after we have accounted for the effect of gravitation from every other object. If we moved an object immediately after calculating the effect of gravitation from one other object, it would change the result of the later calculations (because gravitational force is affected by the distance between the objects). Make sure that when you apply velocity or position changes to an object, you account for the fact that each frame is not exactly one second after the previous frame. To handle this, I have included calculating the 3 number of milliseconds between each frame and provided this value in seconds as a double as an argument to the update function. Hitting the edges After you move a particle based on its velocity, we also need to check if our object has gone past one of the window borders (left, right, top, or bottom). In any of these cases, we should “bounce” the object off the border so that it stays within our window. Elastic collisions Another task we should handle after all of the gravity calculations is object collision. Otherwise, our objects would be able to fly through each other. We will do this using elastic collisions. An elastic collision is a collision where the total kinetic energy before and after the collision does not change. To handle collisions, we must first determine if two objects are colliding. Once this has been determined, we need to calculate the new velocity x and y values based on the collision. The following are the calculations for the first object. To calculate the values for the second object, replace all 1 subscripts with 2 and vice versa. Note: first calculate the new velocities for each object before making any changes or else your second object will not receive the correct calculations. v1x = v1 cos (1 )(m1 m2) + 2m2v2 cos (2 ) m1 + m2 cos + v1 sin (1 ) cos + 2 v1y = v1 cos (1 )(m1 m2) + 2m2v2 cos (2 ) m1 + m2 sin + v1 sin (1 ) sin + 2 where: • v1 and v2 are the magnitudes of the velocities of each object before the collision • m1 and m2 are the masses of each object • 1 and 2 are the angles of movement of each object • is the angle of contact for the collision Once you implement the elastic collisions, I would recommend placing the collision handling between the new velocity calculations due to gravity and the position changes, so we can update object positions once after all other physics calculations. Compiling We can generally use the following command to compile the program on Linux as long as we have installed SDL2: clang++ -std=c++11 -o Gravity main.cpp Game.cpp Particle.cpp Point.cpp $(pkg-config --cflags sdl2) $(pkg-config --libs sdl2) 4 Note: the backslash above is just used to mark the end of the line. In a Unix-like terminal, you can continue a command on to a second line by placing a backslash at the end of the previous line. If you are typing the above command in to a terminal as a single line, omit the backslash. If you are using Windows, there are many tutorials online for properly using SDL with Visual Studio or MinGW. As mentioned at the beginning of this document, I have a virtual machine image available that you can use to test your program.

#include <SDL.h>

int main(int argc, char ** argv)
{
   SDL_Init(SDL_INIT_VIDEO);

   // game code eventually goes here

   SDL_Quit();

   return 0;
}
#include <SDL.h>
int main(int argc, char ** argv)
{
   SDL_Init(SDL_INIT_VIDEO);
   // game code eventually goes here
   SDL_Quit();
   return 0;
}
#ifndef GAME_H
#define   GAME_H
//#include <SDL2>
#include <SDL.h>
#include <string>
#include <iostream>
#include <vector>
#include "Constants.h"
#include "Particle.h"
class Game
{
private:
// screen info
int width;
int height;
  
// for timing frames
unsigned int start;
unsigned int last;
unsigned int current;
  
// for game status
bool good;
bool running;
  
// SDL managed
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Texture* particleTexture;
  
// game data
std::vector<Particle> particles;
  
// Handle game logic updates here
void update(double dt);
  
// Render the game contents to the screen here
void render();
  
// Handle any user input events here
void handleEvent(const SDL_Event& event);
// helper function for drawing particles to screen
void drawParticle(const Particle& p);
  
// helper function for generating random particles
Particle randomParticle() const;
   void math();
public:
// This will act as our initialize function
Game();
  
// We don't want to be able to copy a game
Game(const Game& other) = delete;
Game& operator=(const Game& right) = delete;
  
// This will act as our cleanup function
~Game();
  
// Will handle actually running the game (the gameplay loop)
int operator()();
};
#endif
#include <cmath>
#include <cstdlib>
#include <ctime>
#include "Game.h"
Game::Game()
: width(DEFAULT_WIDTH), height(DEFAULT_HEIGHT),
start(0), last(0), current(0),
good(true), running(false),
particles(std::vector<Particle>())
{
// Seed the random number generator
srand(time(0));
  
// initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
good = false;
return;
}
  
// initialize SDL window
window = SDL_CreateWindow("Gravity", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
if (window == NULL)
{
good = false;
return;
}
  
// initialize SDL renderer
renderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED);
if (renderer == NULL)
{
good = false;
return;
}
  
// initialize particle texture
SDL_Surface* bmp = SDL_LoadBMP("particle.bmp");
if (bmp == NULL)
{
good = false;
return;
}
particleTexture = SDL_CreateTextureFromSurface(renderer, bmp);
SDL_FreeSurface(bmp);
if (particleTexture == NULL)
{
good = false;
return;
}
  
// initialize our particles
for (int i = 0; i < PARTICLE_COUNT; ++i)
{
particles.push_back(randomParticle());
}
}
Game::~Game()
{
if (!good)
{
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
}
if (particleTexture != NULL)
{
SDL_DestroyTexture(particleTexture);
}
if (renderer != NULL)
{
SDL_DestroyRenderer(renderer);
}
if (window != NULL)
{
SDL_DestroyWindow(window);
}
SDL_Quit();
}
int Game::operator()()
{
if (!good)
{
return -1;
}
running = true;
SDL_Event event;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
start = SDL_GetTicks();
last = start;
while (running) // every iteration is 1 frame
{
current = SDL_GetTicks();
while (SDL_PollEvent(&event))
{
handleEvent(event);
}
update((current - last) / 1000.0);
render();
last = current;
}
return 0;
}
// dt is time since last frame in seconds
void Game::update(double dt)
{
std::cout << dt << " sec since last frame. ";
   math();
// Replace with your game logic!
}
void Game::math() {
   double m1, m2;
   double g = 6.67 * 0.00000000001;
   for (int i = 0; i < PARTICLE_COUNT; ++i) {
       for (int j = 0; j < PARTICLE_COUNT; ++j) {
       }
   }
}
void Game::render()
{
SDL_RenderClear(renderer);
  
// rendering here would place objects beneath the particles
  
for (const Particle& p : particles)
{
drawParticle(p);
}
  
// rendering here would place objects on top of the particles
  
SDL_RenderPresent(renderer);
}
void Game::handleEvent(const SDL_Event& event)
{
switch (event.type)
{
// Add your own event handling here if desired
case SDL_QUIT:
running = false;
break;
default:
break;
}
}
void Game::drawParticle(const Particle& p)
{
SDL_Rect dst;
double shift = p.getRadius();
dst.x = (int) (p.getPos().getX() - shift);
dst.y = (int) (p.getPos().getY() - shift);
dst.w = shift * 2;
dst.h = shift * 2;
SDL_RenderCopy(renderer, particleTexture, NULL, &dst);
}
Particle Game::randomParticle() const
{
Point pos(rand() % width, rand() % height);
  
// using random percentage of MAX_MASS
double mass = ((double) rand() / RAND_MAX) * MAX_MASS + 1.0;
  
return Particle(pos, mass);
}
#ifndef CONSTANTS_H
#define CONSTANTS_H
const double PI = 3.14159265358979323846264338;
const double G = 6.674e-11;
const int DEFAULT_WIDTH = 800;
const int DEFAULT_HEIGHT = 600;
const int PARTICLE_COUNT = 50;
const double MAX_MASS = 1.0e14;
#endif
#include "Particle.h"
class Point;
Particle::Particle(double x, double y, double m, double r) {
   p.setX(x);
   p.setY(y);
   mass = m;
   radius = r;
}


double Particle::getX() {
   return p.getX;
}
double Particle::getY() {
   return p.getY;
}
double Particle::getMass() {
   return mass;
}
double Particle::getRadius() {
   return radius;
}
using namespace std;
#ifndef   PARTICLE_H
#define PARTICLE_H


class Particle {
private:
   double mass, radius;
public:
  
   Particle(double x, double y, double m, double r);
   double getX();
   double getY();
   double getMass();
   double getRadius();
};
#endif
#include <iostream>
using namespace std;
class Point {
   friend class Particle;
private:
   double x, y;
public:
   Point(double nx, double ny) {
       x = nx;
       y = ny;
   }

   double getX() {
       return x;
   }
   double getY() {
       return y;
   }
   void setX(double nx) {
       x = nx;
   }
   void setY(double ny) {
       y = ny;
   }
};

Explanation / Answer

Game.cpp
#include <cmath>
#include <cstdlib>
#include <ctime>

#include "Game.h"

Game::Game() : start(0), last(0), current(0), good(true), running(false),
    width(DEFAULT_WIDTH), height(DEFAULT_HEIGHT),
    particles(std::vector<Particle>())
{
    // Seed the random number generator
    srand(time(0));

    // initialize SDL
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        good = false;
        return;
    }

    // initialize SDL window
    window = SDL_CreateWindow("Gravity", SDL_WINDOWPOS_UNDEFINED,
                              SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
    if (window == NULL)
    {
        good = false;
        return;
    }

    // initialize SDL renderer
    renderer = SDL_CreateRenderer(window, -1,
                                  SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (renderer == NULL)
    {
        good = false;
        return;
    }

    // initialize particle texture
    SDL_Surface* bmp = SDL_LoadBMP("particle.bmp");
    if (bmp == NULL)
    {
        good = false;
        return;
    }
    particleTexture = SDL_CreateTextureFromSurface(renderer, bmp);
    SDL_FreeSurface(bmp);
    if (particleTexture == NULL)
    {
        good = false;
        return;
    }

    // initialize our particles
    for (int i = 0; i < PARTICLE_COUNT; ++i)
    {
        particles.push_back(randomParticle());
    }
}

Game::~Game()
{
    if (!good)
    {
        std::cout << "SDL Error: " << SDL_GetError() << std::endl;
    }
    if (particleTexture != NULL)
    {
        SDL_DestroyTexture(particleTexture);
    }
    if (renderer != NULL)
    {
        SDL_DestroyRenderer(renderer);
    }
    if (window != NULL)
    {
        SDL_DestroyWindow(window);
    }
    SDL_Quit();
}

int Game::operator()()
{
    if (!good)
    {
        return -1;
    }
    running = true;
    SDL_Event event;
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);
    start = SDL_GetTicks();
    last = start;
    while (running) // every iteration is 1 frame
    {
        current = SDL_GetTicks();
        while (SDL_PollEvent(&event))
        {
            handleEvent(event);
        }
        update((current - last) / 1000.0);
        render();
        last = current;
    }
    return 0;
}

void Game::update(double dt)
{
    std::cout << dt << " sec since last frame. ";
    std::vector<Point> prev;
    double forceX;
    double forceY;
    double force;
    double ang = 0;
    double dist;
    // test loop
    /*
    for(int i = 0; i < PARTICLE_COUNT; i++)
    {
        double temp = particles[i].getPos().getY();
        particles[i].setYPos(temp - 1);
    }
    */

// calculating acceleration/velocity
    for(int i = 0; i< PARTICLE_COUNT; i++)
    {
        force = 0;
        forceX = 0;
        forceY = 0;
        prev.push_back(particles[i].getPos());
        particles[i].setForce(forceX, forceY);
        for(int j = 0; j < PARTICLE_COUNT; j++)
        {
            if(i != j)
            {
                ang = atan2(particles[j].getYPos()-particles[i].getYPos(),
                            particles[j].getXPos()-particles[i].getXPos());
                dist = particles[i].getPos().distanceFrom(particles[j].getPos());
                // std::cout<<particles[j].getPos().getY()-particles[i].getPos().getY()<<" ";
                // std::cout<<particles[j].getPos().getX()-particles[i].getPos().getX()<<" ";
                force = G*particles[i].getMass()*particles[j].getMass()/(dist*dist);
                //std::cout<<force<<" ";

                forceX += cos(ang) * force;
                forceY += sin(ang) * force;
            }
        }
        // std::cout<<forceX<<" ";
        //std::cout<<forceY<<" ";
        //std::cout<<force<<" ";
        particles[i].setForce(forceX, forceY);
    }

    for(int i = 0; i < PARTICLE_COUNT; i++)
    {
        //std::cout<<particles[0].getVeloc().getY()<< " ";
        particles[i].updateVel(dt);
        particles[i].updatePos(dt);

    }


    //collision loop
    //make sure collisions aren't calculated twice
    double phi1;
    double phi2;
    double theta1;
    double theta2;
    double xV1 = 0;
    double yV1 = 0;
    double xV2 = 0;
    double yV2 = 0;
    double v1;
    double v2;
    double m1;
    double m2;
    bool collision[PARTICLE_COUNT][PARTICLE_COUNT] = {0};
    for(int i = 0; i< PARTICLE_COUNT; i++)
    {
        for(int j = 0; j < PARTICLE_COUNT; j++)
        {
            if(i != j && !collision[i][j] && particles[i].getPos().distanceFrom(particles[j].getPos()) <= particles[i].getRadius()
                    + particles[j].getRadius())
            {
                m1 = particles[i].getMass();
                m2 = particles[j].getMass();
                collision[i][j] = true;
                collision[j][i] = true;
                particles[i].setXPos(prev[i].getX());
                particles[i].setYPos(prev[i].getY());
                particles[j].setXPos(prev[j].getX());
                particles[j].setYPos(prev[j].getY());
                xV1 = particles[i].getVeloc().getX();
                yV1 = particles[i].getVeloc().getY();
                xV2 = particles[j].getVeloc().getX();
                yV2 = particles[j].getVeloc().getY();
                v1 = pow(xV1*xV1 + yV1*yV1, .5);
                v2 = pow(xV2*xV2 + yV2*yV2, .5);
                phi1 = atan2(particles[j].getYPos()-particles[i].getYPos(),
                             particles[j].getXPos()-particles[i].getXPos());
                phi2 = atan2(particles[i].getYPos()-particles[j].getYPos(),
                             particles[i].getXPos()-particles[j].getXPos());
                theta1 = atan2(particles[i].getVeloc().getY(),particles[i].getVeloc().getX());
                theta2 = atan2(particles[j].getVeloc().getY(),particles[j].getVeloc().getX());

                xV1 = ((v1*cos(theta1-phi1)*(m1-m2) + 2*m2*v2*cos(theta2-phi1))/(m1+m2))
                      *cos(phi1) + v1*sin(theta1-phi1)*cos(phi1 + (M_PI)/2);
                yV1 = ((v1*cos(theta1-phi1)*(m1-m2) + 2*m2*v2*cos(theta2-phi1))/(m1+m2)
                       *sin(phi1) + v1*sin(theta1-phi1)*sin(phi1+ (M_PI)/2));

                xV2 = ((v2*cos(theta2-phi2)*(m2-m1) + 2*m1*v1*cos(theta1-phi2))/(m1+m2))
                      *cos(phi2) + v1*sin(theta2-phi2)*cos(phi2 + (M_PI)/2);
                yV2 = ((v2*cos(theta2-phi2)*(m2-m1) + 2*m1*v1*cos(theta1-phi2))/(m1+m2)
                       *sin(phi2) + v2*sin(theta2-phi2)*sin(phi2+ (M_PI)/2));

                particles[i].setVeloc(xV1,yV1);
                particles[j].setVeloc(xV2,yV2);

            }
        }
    }

    //bounce off walls

    for(int i = 0; i < PARTICLE_COUNT; i++)
    {
        if(particles[i].getPos().getX() >= DEFAULT_WIDTH + particles[i].getRadius())
        {
            particles[i].bounceR(DEFAULT_WIDTH);
        }
        if(particles[i].getPos().getX() <= particles[i].getRadius())
        {
            particles[i].bounceL();
        }

        if(particles[i].getPos().getY() >= DEFAULT_HEIGHT + particles[i].getRadius())
        {
            particles[i].bounceD(DEFAULT_HEIGHT);
        }
        if(particles[i].getPos().getY() <= particles[i].getRadius())
        {
            particles[i].bounceU();
        }
    }

    // Replace with your game logic!
}

void Game::render()
{
    SDL_RenderClear(renderer);

    // rendering here would place objects beneath the particles

    for (const Particle& p : particles)
    {
        drawParticle(p);
    }

    // rendering here would place objects on top of the particles

    SDL_RenderPresent(renderer);
}

void Game::handleEvent(const SDL_Event& event)
{
    switch (event.type)
    {
    // Add your own event handling here if desired
    case SDL_QUIT:
        running = false;
        break;
    default:
        break;
    }
}

void Game::drawParticle(const Particle& p)
{
    SDL_Rect dst;

    double shift = p.getRadius();
    dst.x = (int) (p.getPos().getX() - shift);
    dst.y = (int) (p.getPos().getY() - shift);
    dst.w = shift * 2;
    dst.h = shift * 2;
    SDL_RenderCopy(renderer, particleTexture, NULL, &dst);
}

Particle Game::randomParticle() const
{
    Point pos(rand() % width, rand() % height);
    // using some percentage of the mass of Jupiter
    double mass = ((double) rand() / RAND_MAX) * 150 + 1.0;

    return Particle(pos, mass);
}

Game.h
#ifndef GAME_H
#define   GAME_H

#include <SDL2/SDL.h>
#include <string>
#include <iostream>
#include <vector>

#include "Particle.h"

const int DEFAULT_WIDTH = 800;
const int DEFAULT_HEIGHT = 600;
const int PARTICLE_COUNT = 25;

// use this in your gravity calculations
const double G = 25;

class Game
{
private:
    // screen info
    int width;
    int height;

    // for timing frames
    unsigned int start;
    unsigned int last;
    unsigned int current;

    // for game status
    bool good;
    bool running;

    // SDL managed
    SDL_Window* window;
    SDL_Renderer* renderer;
    SDL_Texture* particleTexture;

    // game data
    std::vector<Particle> particles;

    // Handle game logic updates here
    void update(double dt);

    // Render the game contents to the screen here
    void render();

    // Handle any user input events here
    void handleEvent(const SDL_Event& event);

    void drawParticle(const Particle& p);

    Particle randomParticle() const;
public:
    // This will act as our initialize function
    Game();

    // We don't want to be able to copy a game
    Game(const Game& orig) = delete;
    Game& operator=(const Game& right) = delete;

    // This will act as our cleanup function
    ~Game();

    // Will handle actually running the game (the gameplay loop)
    int operator()();
};

#endif   /* GAME_H */


Particle.cpp
#include "Particle.h"
#include "Point.h"
#include <iostream>
#include <cmath>
#include <ctime>
Particle::Particle(Point p, double m): mass(m)
{

    center= Point(p.getX(), p.getY());
    velocity = Point(0,0);
    force = Point(0,0);
    radius = m/1;
}
double Particle::getRadius() const
{
    return radius;
}

double Particle::getMass() const
{
    return mass;
}
double Particle::getXPos()
{
    return center.getX();
}
double Particle::getYPos()
{
    return center.getY();
}
void Particle::updateVel(double t)
{
    double aX = force.getX()/mass;
    double aY = force.getY()/mass;
   // std::cout<<aX<<" ";
   // std::cout<<aY<<" ";
    double xNew = velocity.getX() + aX*t;
    double yNew = velocity.getY() + aY*t;
    /*
    if(xNew >= 40){
        xNew = 40;
    }
    if(xNew <= -40){
        xNew = -40;
    }
    if(yNew >= 40){
        yNew = 40;
    }
    if(yNew <= -40){
        yNew = -40;
    }
    */
     //std::cout<<xNew<<" ";
     // std::cout<<yNew<<" ";
    Point newVelocity(xNew, yNew);
    velocity = newVelocity;
}

void Particle::updatePos(double t)
{
    double xf = center.getX() + velocity.getX()*t + .5* (force.getX()/mass)*t*t;
    //std::cout<<xf<<" ";
    double yf = center.getY() + velocity.getY()*t + .5* (force.getY()/mass)*t*t;
    //std::cout<<yf<<" ";
    Point newCenter(xf, yf);
    center = newCenter;
}

void Particle::setForce(double fx, double fy)
{
    force.setX(fx);
    force.setY(fy);
}

Point Particle::getPos() const
{
    return center;
}
Point Particle::getVeloc() const
{
    return velocity;
}
Point Particle::getForce() const
{
    return force;
}
void Particle::setXPos(double d)
{
    center.setX(d);
}
void Particle::setYPos(double d)
{
    center.setY(d);
}
void Particle::setVeloc(double x, double y)
{
    Point newVeloc(x,y);
    velocity = newVeloc;
}
void Particle::setMass(double m){
    mass = m;
}
void Particle::bounceU()
{
    velocity = Point(velocity.getX(), -velocity.getY());
    setYPos(radius);
}
void Particle::bounceD(double h)
{
    velocity = Point(velocity.getX(), -velocity.getY());
    setYPos(h-radius);
}
void Particle::bounceL()
{
    velocity = Point(-velocity.getX(), velocity.getY());
    setXPos(radius);
}
void Particle::bounceR(double w)
{
    velocity = Point(-velocity.getX(), velocity.getY());
    setXPos(w+radius);
}


Particle.h
#ifndef PARTICLE_H
#define PARTICLE_H
#include "Point.h"
#include <cmath>
#include <ctime>
class Particle{


    private:
    double radius;
    double mass;
    Point center;
    Point velocity;
    Point force;
    public:
    double getRadius() const;
    double getMass() const;
    double getXPos();
    double getYPos();
    Point getPos() const;
    Particle(Point p, double m);
    Point getVeloc() const;
    Point getForce() const;
    void setXPos(double d);
    void setYPos(double d);
    void setForce( double fx,double fy);
    void setVeloc(double x, double y);
    void setMass(double m);
    void updateVel(double time);
    void updatePos(double time);
    void bounceU();
    void bounceD(double h);
    void bounceL();
    void bounceR(double w);
};
#endif // PARTICLE_H


Point.cpp
#include "Point.h"
#include <cmath>

Point::Point(){
x =0; y =0;
}

Point::Point(double x0, double y0):x(x0), y(y0)
{
}


double Point::setX(double d){
    x = d;
}
double Point::setY(double d){
    y = d;
}
double Point::getX() const
{
    return x;
}

double Point::getY() const
{
    return y;
}


double Point::distanceFrom(Point p) const{
    double x2 = p.getX();
    double y2 = p.getY();
    double distance = pow(((x-x2)*(x-x2) + (y-y2)*(y-y2)), .5);
    return distance;
}

double Point::distanceFrom(double x2, double y2) const{
    double distance = pow(((x-x2)*(x-x2) + (y-y2)*(y-y2)), .5);
    return distance;
}


Point.h
#ifndef POINT_H
#define POINT_H
#include <cmath>
class Point
{

private:
    double x;
    double y;

public:
    Point();
    Point(double x, double y);
    double getX() const;
    double getY() const;
    double setX(double d);
    double setY(double d);
    double distanceFrom(Point p) const;
    double distanceFrom(double x, double y) const;
};
#endif // POINT_H

main.cpp
#include "Game.h"

int main()
{
    Game game;
    return game();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote