A
AJ
AJ,
Hey everybody I am pretty knew to c++ programming and I am having alot
of trouble with a program that is suppose to create an certain number
of bouncy balls entered by the user. Right now i only get one ball.
The problem is that it creates the same ball in the same position over
and over again. I don't know what to do. PLEASE HELP ME SOLVE THIS.
Thanks in advance.
compile using g++ -Wall -o csci135p5main csci135p5main.cpp
csci135p5ball.cpp csci135p5ballpit.cpp csci135canvas.cpp $(pkg-config
--cflags --libs cairo gtk+-2.0 gthread-2.0)
main program
#############################################################################################
#include <iostream>
#include "csci135canvas.h"
#include "csci135p5ball.h"
#include "csci135p5ballpit.h"
#include <unistd.h>
using namespace std;
void CircleFunction(Canvas &canvas)
{
int ball_count;
cout<<"Enter the number of balls to place in the Ball Pit: ";
cin>>ball_count;
canvas.Erase();
BallPit ballpit(ball_count);
usleep(40000);
while (true)
{
canvas.Erase();
ballpit.draw(canvas);
ballpit.step();
canvas.Invalidate();
usleep(40000);
}
}
int main ()
{
Canvas canvas(500,500);
canvas.Go("CSCI 135 Project 5",CircleFunction);
return 0;
}
#############################################################################################
Ball.cpp File
#############################################################################################
#include "csci135p5ball.h"
#include "csci135p5ballpit.h"
#include "csci135canvas.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
Ball::Ball()
{
srand ( time(NULL) );
X_position = rand() % 500;
Y_position = rand() % 500;
X_velocity = rand() % 5 + -5;
Y_velocity = rand() % 5 + -5;
Ball_Radius = rand() % 12 + 2;
}
Ball::Ball(double x, double y, double vx, double vy, double radius)
{
X_position = x;
Y_position = y;
X_velocity = vx;
Y_velocity = vy;
Ball_Radius = radius;
}
void Ball::Step()
{
if (X_position <= 0)
X_velocity = -(X_velocity);
else if (X_position >= 500)
X_velocity = -(X_velocity);
else if (Y_position <= 0)
Y_velocity = -(Y_velocity);
else if (Y_position >= 500)
Y_velocity = -(Y_velocity);
X_position = X_position + X_velocity;
Y_position = Y_position + Y_velocity;
}
void Ball:raw(Canvas &canvas)
{
canvas.DrawCircle(X_position, Y_position, Ball_Radius);
canvas.Invalidate();
}
/*
double Ball:istance(Ball &ball);
{
}
*/
double Ball::GetX()
{
return X_position;
}
double Ball::GetY()
{
return Y_position;
}
double Ball::GetVX()
{
return X_velocity;
}
double Ball::GetVY()
{
return Y_velocity;
}
double Ball::GetRadius()
{
return Ball_Radius;
}
void Ball::SetX(double new_x)
{
X_position = new_x;
}
void Ball::SetY(double new_y)
{
Y_position = new_y;
}
void Ball::SetVX(double new_VX)
{
X_velocity = new_VX;
}
void Ball::SetVY(double new_VY)
{
Y_velocity = new_VY;
}
void Ball::SetRadius(double new_Radius)
{
Ball_Radius = new_Radius;
}
#############################################################################################
Ball..h
#############################################################################################
#ifndef CSCI135P5_BALL_H
#define CSCI135P5_BALL_H
#include "csci135canvas.h"
class Ball
{
public:
Ball(double x, double y, double vx, double vy, double radius);
Ball();
void Step();
void Draw(Canvas &canvas);
//double Distance(Ball &ball);
double GetX();
double GetY();
double GetVX();
double GetVY();
double GetRadius();
void SetX(double new_x);
void SetY(double new_y);
void SetVX(double new_VX);
void SetVY(double new_VY);
void SetRadius(double new_Radius);
private:
double X_position;
double Y_position;
double X_velocity;
double Y_velocity;
double Ball_Radius;
};
#endif
#############################################################################################
BallPit.h File
#############################################################################################
#ifndef CSCI135P5_BALLPIT_H
#define CSCI135P5_BALLPIT_H
#include "csci135canvas.h"
#include <vector>
using std::vector;
class BallPit
{
public:
BallPit(int ball_number);
void step();
void draw(Canvas &canvas);
private:
unsigned int balls;
double x_spot;
double y_spot;
double xv_spot;
double yv_spot;
double radius_spot;
vector<double> initial_x;
vector<double> initial_y;
vector<double> initial_xv;
vector<double> initial_yv;
vector<double> initial_radius;
};
#endif
#############################################################################################
BallPit.cpp File
#############################################################################################
#include "csci135p5ballpit.h"
#include "csci135p5ball.h"
#include "csci135canvas.h"
#include <vector>
using std::vector;
BallPit::BallPit(int ball_number)
{
balls = ball_number;
for(unsigned int i = 0; i < balls; i++)
{
Ball ball();
Ball myball;
x_spot = myball.GetX();
y_spot = myball.GetY();
xv_spot = myball.GetVX();
yv_spot = myball.GetVY();
radius_spot = myball.GetRadius();
//Ball ball(x_spot, y_spot, xv_spot, xv_spot, yv_spot,
radius_spot);
initial_x.push_back(x_spot);
initial_y.push_back(y_spot);
initial_xv.push_back(xv_spot);
initial_yv.push_back(yv_spot);
initial_radius.push_back(radius_spot);
}
}
void BallPit::step()
{
double xp;
double yp;
double xv;
double yv;
double radius;
for(unsigned int x = 0; x < balls; x++)
{
Ball myball;
myball.SetX(initial_x[x]);
myball.SetY(initial_y[x]);
myball.SetVX(initial_xv[x]);
myball.SetVY(initial_yv[x]);
myball.SetRadius(initial_radius[x]);
xp = initial_x[x];
yp = initial_y[x];
xv = initial_xv[x];
yv = initial_yv[x];
radius = initial_radius[x];
Ball ball(xp, yp, xv, yv, radius);
myball.Step();
xp = myball.GetX();
yp = myball.GetY();
xv = myball.GetVX();
yv = myball.GetVY();
radius = GetRadius();
initial_x[x] = xp;
initial_y[x] = yp;
initial_xv[x] = xv;
initial_yv[x] = yv;
initial_radius[x] = radius;
}
void BallPit::draw(Canvas &canvas)
{
for(unsigned int y = 0; y < balls; y++)
{
double xp = initial_x[y];
double yp = initial_y[y];
double xv = initial_xv[y];
double yv = initial_yv[y];
double radius = initial_radius[y];
Ball ball;
ball.SetX(xp);
ball.SetY(yp);
ball.SetVX(xv);
ball.SetVY(yv);
ball.SetRadius(radius);
ball.Draw(canvas);
}
}
#############################################################################################
Sorry About posting so much code. I don't know where the problem is
and don't want to leave something out. thank you again
Hey everybody I am pretty knew to c++ programming and I am having alot
of trouble with a program that is suppose to create an certain number
of bouncy balls entered by the user. Right now i only get one ball.
The problem is that it creates the same ball in the same position over
and over again. I don't know what to do. PLEASE HELP ME SOLVE THIS.
Thanks in advance.
compile using g++ -Wall -o csci135p5main csci135p5main.cpp
csci135p5ball.cpp csci135p5ballpit.cpp csci135canvas.cpp $(pkg-config
--cflags --libs cairo gtk+-2.0 gthread-2.0)
main program
#############################################################################################
#include <iostream>
#include "csci135canvas.h"
#include "csci135p5ball.h"
#include "csci135p5ballpit.h"
#include <unistd.h>
using namespace std;
void CircleFunction(Canvas &canvas)
{
int ball_count;
cout<<"Enter the number of balls to place in the Ball Pit: ";
cin>>ball_count;
canvas.Erase();
BallPit ballpit(ball_count);
usleep(40000);
while (true)
{
canvas.Erase();
ballpit.draw(canvas);
ballpit.step();
canvas.Invalidate();
usleep(40000);
}
}
int main ()
{
Canvas canvas(500,500);
canvas.Go("CSCI 135 Project 5",CircleFunction);
return 0;
}
#############################################################################################
Ball.cpp File
#############################################################################################
#include "csci135p5ball.h"
#include "csci135p5ballpit.h"
#include "csci135canvas.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
Ball::Ball()
{
srand ( time(NULL) );
X_position = rand() % 500;
Y_position = rand() % 500;
X_velocity = rand() % 5 + -5;
Y_velocity = rand() % 5 + -5;
Ball_Radius = rand() % 12 + 2;
}
Ball::Ball(double x, double y, double vx, double vy, double radius)
{
X_position = x;
Y_position = y;
X_velocity = vx;
Y_velocity = vy;
Ball_Radius = radius;
}
void Ball::Step()
{
if (X_position <= 0)
X_velocity = -(X_velocity);
else if (X_position >= 500)
X_velocity = -(X_velocity);
else if (Y_position <= 0)
Y_velocity = -(Y_velocity);
else if (Y_position >= 500)
Y_velocity = -(Y_velocity);
X_position = X_position + X_velocity;
Y_position = Y_position + Y_velocity;
}
void Ball:raw(Canvas &canvas)
{
canvas.DrawCircle(X_position, Y_position, Ball_Radius);
canvas.Invalidate();
}
/*
double Ball:istance(Ball &ball);
{
}
*/
double Ball::GetX()
{
return X_position;
}
double Ball::GetY()
{
return Y_position;
}
double Ball::GetVX()
{
return X_velocity;
}
double Ball::GetVY()
{
return Y_velocity;
}
double Ball::GetRadius()
{
return Ball_Radius;
}
void Ball::SetX(double new_x)
{
X_position = new_x;
}
void Ball::SetY(double new_y)
{
Y_position = new_y;
}
void Ball::SetVX(double new_VX)
{
X_velocity = new_VX;
}
void Ball::SetVY(double new_VY)
{
Y_velocity = new_VY;
}
void Ball::SetRadius(double new_Radius)
{
Ball_Radius = new_Radius;
}
#############################################################################################
Ball..h
#############################################################################################
#ifndef CSCI135P5_BALL_H
#define CSCI135P5_BALL_H
#include "csci135canvas.h"
class Ball
{
public:
Ball(double x, double y, double vx, double vy, double radius);
Ball();
void Step();
void Draw(Canvas &canvas);
//double Distance(Ball &ball);
double GetX();
double GetY();
double GetVX();
double GetVY();
double GetRadius();
void SetX(double new_x);
void SetY(double new_y);
void SetVX(double new_VX);
void SetVY(double new_VY);
void SetRadius(double new_Radius);
private:
double X_position;
double Y_position;
double X_velocity;
double Y_velocity;
double Ball_Radius;
};
#endif
#############################################################################################
BallPit.h File
#############################################################################################
#ifndef CSCI135P5_BALLPIT_H
#define CSCI135P5_BALLPIT_H
#include "csci135canvas.h"
#include <vector>
using std::vector;
class BallPit
{
public:
BallPit(int ball_number);
void step();
void draw(Canvas &canvas);
private:
unsigned int balls;
double x_spot;
double y_spot;
double xv_spot;
double yv_spot;
double radius_spot;
vector<double> initial_x;
vector<double> initial_y;
vector<double> initial_xv;
vector<double> initial_yv;
vector<double> initial_radius;
};
#endif
#############################################################################################
BallPit.cpp File
#############################################################################################
#include "csci135p5ballpit.h"
#include "csci135p5ball.h"
#include "csci135canvas.h"
#include <vector>
using std::vector;
BallPit::BallPit(int ball_number)
{
balls = ball_number;
for(unsigned int i = 0; i < balls; i++)
{
Ball ball();
Ball myball;
x_spot = myball.GetX();
y_spot = myball.GetY();
xv_spot = myball.GetVX();
yv_spot = myball.GetVY();
radius_spot = myball.GetRadius();
//Ball ball(x_spot, y_spot, xv_spot, xv_spot, yv_spot,
radius_spot);
initial_x.push_back(x_spot);
initial_y.push_back(y_spot);
initial_xv.push_back(xv_spot);
initial_yv.push_back(yv_spot);
initial_radius.push_back(radius_spot);
}
}
void BallPit::step()
{
double xp;
double yp;
double xv;
double yv;
double radius;
for(unsigned int x = 0; x < balls; x++)
{
Ball myball;
myball.SetX(initial_x[x]);
myball.SetY(initial_y[x]);
myball.SetVX(initial_xv[x]);
myball.SetVY(initial_yv[x]);
myball.SetRadius(initial_radius[x]);
xp = initial_x[x];
yp = initial_y[x];
xv = initial_xv[x];
yv = initial_yv[x];
radius = initial_radius[x];
Ball ball(xp, yp, xv, yv, radius);
myball.Step();
xp = myball.GetX();
yp = myball.GetY();
xv = myball.GetVX();
yv = myball.GetVY();
radius = GetRadius();
initial_x[x] = xp;
initial_y[x] = yp;
initial_xv[x] = xv;
initial_yv[x] = yv;
initial_radius[x] = radius;
}
void BallPit::draw(Canvas &canvas)
{
for(unsigned int y = 0; y < balls; y++)
{
double xp = initial_x[y];
double yp = initial_y[y];
double xv = initial_xv[y];
double yv = initial_yv[y];
double radius = initial_radius[y];
Ball ball;
ball.SetX(xp);
ball.SetY(yp);
ball.SetVX(xv);
ball.SetVY(yv);
ball.SetRadius(radius);
ball.Draw(canvas);
}
}
#############################################################################################
Sorry About posting so much code. I don't know where the problem is
and don't want to leave something out. thank you again