Hope this helps. It compiles with bloodshed dev c++. Note, modified the randPosition function so it varies with the constants, rather than just returns a position in the rectange between (0,0) and (1,1)
Code:
// code for pongesque 2D motion
#include <cmath>
#include <cstdlib>
//#include "your2dgraphicslib.h"
struct vec {
vec(float _x, float _y) : x(_x), y(_y) { };
vec() : x(0), y(0) { };
void operator = (const vec& rhs) { x = rhs.x; y = rhs.y; };
float x;
float y;
};
vec operator + (const vec& left, const vec& right) {
return vec(left.x + right.x, left.y + right.y);
}
vec operator - (const vec& left, const vec& right) {
return vec(left.x - right.x, left.y - right.y);
}
vec operator * (const vec& left, float scale) {
return vec(left.x*scale, left.y*scale);
}
inline vec operator* (float scale, const vec& right) {
return right * scale;
}
enum wall { LEFT_WALL, RIGHT_WALL, TOP_WALL, BOTTOM_WALL};
// moves between rectangle with bottom left coords (0,0) and top right coords (1,1)
const float RIGHT_BORDER = 1.0f;
const float TOP_BORDER = 1.0f;
const float LEFT_BORDER = 0.0f;
const float BOTTOM_BORDER = 0.0f;
class point {
public:
point(const vec& pos, const vec& vel);
point() { };
void move(float timeChange);
vec getPosition() { return position; };
void draw();
private:
vec position;
vec velocity;
void collide(wall Wall);
};
point::point(const vec& pos, const vec& vel) {
velocity = vel;
position = pos;
move(0.0f); // make sure point starts off in the valid area
}
void point::collide(wall Wall) {
switch (Wall) {
case LEFT_WALL:
position.x = LEFT_BORDER; // make sure it doesn't move through the wall
velocity.x = -velocity.x; // reverse movement in x direction
break;
case RIGHT_WALL:
position.x = RIGHT_BORDER;
velocity.x = -velocity.x;
break;
case TOP_WALL:
position.y = TOP_BORDER;
velocity.y = -velocity.y;
break;
case BOTTOM_WALL:
position.y = BOTTOM_BORDER;
velocity.y = -velocity.y;
break;
}
}
void point::move(float timeChange) {
position = position + timeChange * velocity;
// check for collosions
if (position.x > RIGHT_BORDER) {
collide(RIGHT_WALL);
} else if (position.x < LEFT_BORDER) {
collide(LEFT_WALL);
}
if (position.y > TOP_BORDER) {
collide(TOP_WALL);
} else if (position.y < BOTTOM_BORDER) {
collide(BOTTOM_WALL);
}
}
// cheesy method because I am not gonna use it to actually draw to screen
#include <iostream>
void point::draw() {
/* your drawing function comes in here, keep in mind x and y
coordinations of point are in position.x and position.y */
std::cout << position.x << "\t" << position.y << endl;
}
// returns vector with magnitude of one and random direction
vec randDirection() {
double ang = (double) (rand() % 360) * 3.14159265 / 180.0;
return vec(cos(ang) , sin(ang));
}
vec randPosition() {
return vec(
(float) rand()/RAND_MAX *(RIGHT_BORDER - LEFT_BORDER) + LEFT_BORDER,
(float) rand()/RAND_MAX * (TOP_BORDER - BOTTOM_BORDER) + BOTTOM_BORDER);
}
const int NUM_POINTS = 50;
const float SPEED_SCALE = .0002f;
int main() {
srand(0);
point pointList[NUM_POINTS];
for (int i = 0; i < NUM_POINTS; i++) {
// fill point list with random points
pointList[i] = point(randPosition() , randDirection() * SPEED_SCALE);
}
while (true) {
for (int i = 0; i < NUM_POINTS; i++) {
pointList[i].move(1);
pointList[i].draw();
}
}
return 0;
}