Tom Kelliher, CS 320
Mar. 13, 2009
Carefully read collision.c
and Sections 4.1 and B.1-B.5. Note that
collision.c
is fairly ``literate:'' good identifier names,
high-level overviews, details where necessary, use of whitespace, splitting
lines to maximize readability. Use it as a model.
Experimentation with polygon.c
.
polygon.c
.
collision.c
.
Continued study of collision.c
.
20 minutes to tie up loose ends. Turn in to kelliher[at]goucher.edu
by
beginning of next class.
Operating systems is the application of data structures and algorithms. Computer graphics is the application of data structures, algorithms, trigonometry, linear algebra, Calculus, and differential equations.
Not the same as a normal vector.
Note that this is a scalar. What if I need a vector?
This is all 2-D. It should be extended to 3-D.
typedef struct Vector2 { GLdouble x, y; } Vector2;
double distanceSquared(double x, double y) { return x * x + y * y; } double vectorLength(Vector2 v) { return sqrt(distanceSquared(v.x, v.y)); }Square root and division are expensive. Avoid where possible.
Vector2 scalarProduct(double s, Vector2 v) { v.x *= s; v.y *= s; return v; }
double dotProduct(Vector2 a, Vector2 b) { return a.x * b.x + a.y * b.y; }
Vector2 normalize(Vector2 v) { double length = vectorLength(v); v.x /= length; v.y /= length; return v; }
Vector2 vectorSum(Vector2 a, Vector2 b) { a.x += b.x; a.y += b.y; return a; }
main()
: Note use of srand()
.
init()
: Calls initBalls()
, little else.
initBalls
:
typedef struct Color { GLdouble r, g, b; } Color; typedef struct Ball { Vector2 position; /* Ok, so it's not really a vector. Sue me. */ Vector2 velocity; GLdouble radius; GLdouble mass; Color color; GLuint handle; } Ball;These should be proper classes.
placeBalls()
:
Velocity computation. Target: origin. Scaling velocity.
Translating position to circle of radius 40.
Making the second ball stationary, at an arbitrary position.