Spoiler
Test code can be fun. You can program in the abstract all day long, but actually using the code for something points out what you really need.
I needed a getIntersect. I'm not thrilled with it and it could be buggered, but it worked for the demo. The demo is common; bounce ball. Because, even showing all the methods doesn't really tell a student what they're good for.
This isn't the usual ricochet called bounce, we have gravity here. I think it's a pretty good test of any basic 2D physics framework.
#include <iostream>
#include "plane.h"
using namespace std;
using namespace Plane;
struct Ball : public BoundVector {
double bounceConserve;
Ball(double x, double y, const Vector &speed, double B)/> : BoundVector(speed, Point(x,y)), bounceConserve(B)/> { }
void bounce() {
Vector v(magnitude()*bounceConserve, -degrees());
x = v.x;
y = v.y;
}
};
int main() {
Point maxCorner(77, 21);
const Vector gravity(Point(0, 1));
const Vector ground(Point(0, maxCorner.y));
Ball ball(0, maxCorner.y, Point(2, -7), 0.8);
while(ball.origin.x<=maxCorner.x && ball.magnitude() > 1) {
cout << ball.origin << endl;
ball += gravity;
ball++;
if(ball.origin.y>maxCorner.y) {
ball.origin = ball.getIntersect(ground);
ball.bounce();
}
}
return 0;
}
Results:
(0,21) (2,15) (4,10) (6,6) (8,3) (10,1) (12,0) (14,0) (16,1) (18,3) (20,6) (22,10) (24,15) (26,21) (26,21) (27.6,16.4) (29.2,12.8) (30.8,10.2) (32.4,8.6) (34,8) (35.6,8.4) (37.2,9.8) (38.8,12.2) (40.4,15.6) (42,20) (42.2963,21) (43.5763,17.68) (44.8563,15.36) (46.1363,14.04) (47.4163,13.72) (48.6963,14.4) (49.9763,16.08) (51.2563,18.76) (52.0354,21) (53.0594,19.056) (54.0834,18.112) (55.1074,18.168) (56.1314,19.224) (57.016,21) (57.8352,20.3552)
I know, hard to follow. But if you were to plot those pixels, you'd get something like:
* *
* *
* *
* *
*
* *
*
* * *
* *
*
* *
* * * *
* *
*
* **
* * *
*
* * * * *
If the screen wraps that too much, it will probably look like ass, but hopefully you get the idea.

New Topic/Question
Reply




MultiQuote









|