Skip to content

Commit

Permalink
fleshed out the object model a bit.
Browse files Browse the repository at this point in the history
made a rocket that blasts off!
  • Loading branch information
bvanderveen committed Jan 24, 2012
1 parent f139123 commit 8488128
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 42 deletions.
141 changes: 100 additions & 41 deletions sim/sim.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,126 @@
#include <stdlib.h>
#include <OpenGL/gl.h>

struct SimContext
{
double t;
typedef double SimUnit;
#define SimUnitZero 0

struct Sim3Vector {
SimUnit x, y, z;
};
typedef struct SimContext *SimContext;
typedef struct Sim3Vector Sim3Vector;

SimContextRef SimContextCreate() {
SimContext result = (SimContext)malloc(sizeof(SimContext));
result->t = 0.0;
return (SimContext)result;
#define Sim3VectorZero ((Sim3Vector){ .x = SimUnitZero, .y = SimUnitZero, .z = SimUnitZero })
#define Sim3VectorMake(X, Y, Z) ((Sim3Vector){ .x = (X), .y = (Y), .z = (Z) })
#define Sim3VectorAdd(A, B) ((Sim3Vector){ .x = (A).x + (B).x, .y = (A).y + (B).y, .z = (A).z + (B).z })
#define Sim3VectorScale(K, V) ((Sim3Vector){ .x = (K) * (V).x, .y = (K) * (V).y, .z = (K) * (V).z })

struct SimThruster {
Sim3Vector thrust;
};
typedef struct SimThruster *SimThrusterRef;

SimThrusterRef SimThrusterCreate() {
SimThrusterRef result = (SimThrusterRef)malloc(sizeof(struct SimThruster));
result->thrust = Sim3VectorZero;
return result;
}

void SimContextDestroy(SimContextRef c) {
free(c);
void SimThrusterDestroy(SimThrusterRef ref) {
free(ref);
}

void SimContextUpdate(SimContextRef c, double t) {
SimContext ctx = c;
ctx->t += t * .0001;
struct SimVehicle {
Sim3Vector position;
Sim3Vector velocity;

Sim3Vector angle;
Sim3Vector angularVelocity;

SimUnit mass;

SimThrusterRef thruster;
};
typedef struct SimVehicle *SimVehicleRef;

SimVehicleRef SimVehicleCreate() {
SimVehicleRef result = (SimVehicleRef)malloc(sizeof(struct SimVehicle));

result->position = Sim3VectorZero;
result->velocity = Sim3VectorZero;

result->angle = Sim3VectorZero;
result->angularVelocity = Sim3VectorZero;

result->mass = SimUnitZero;
result->thruster = SimThrusterCreate();
return result;
}

void SimContextDraw(SimContextRef c, double t) {
SimContext ctx = c;

glRotated(ctx->t, 0,0,1);
void SimVehicleDestroy(SimVehicleRef ref) {
SimThrusterDestroy(ref->thruster);
free(ref);
}

glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
void SimVehicleUpdate(SimVehicleRef ref, SimUnit t) {
ref->position = Sim3VectorAdd(ref->position, Sim3VectorScale(1/t, ref->velocity));
ref->velocity = Sim3VectorAdd(ref->velocity, Sim3VectorScale(1/t, ref->thruster->thrust));
}

void SimVehicleDraw(SimVehicleRef ref, SimUnit t) {
glPushMatrix();

glTranslated(ref->position.x, ref->position.y, ref->position.z);

glColor3f(1.0f, 0.85f, 0.35f);
glBegin(GL_TRIANGLES);
{
glVertex3f( 0.0, 0.6, 0.0);
glVertex3f( -0.2, -0.3, 0.0);
glVertex3f( 0.2, -0.3 ,0.0);
glVertex3f( 0.0, 0.5, 0.0);
glVertex3f( -0.2, -0.5, 0.0);
glVertex3f( 0.2, -0.5 ,0.0);
}
glEnd();
glFlush();

glPopMatrix();
}

struct SimContext {
SimVehicleRef vehicle;
};
typedef struct SimContext *SimContext;

// const int TICKS_PER_SECOND = 25;
// const int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
// const int MAX_FRAMESKIP = 5;
SimContextRef SimContextCreate() {
SimContext result = (SimContext)malloc(sizeof(struct SimContext));
result->vehicle = SimVehicleCreate();
result->vehicle->thruster->thrust = Sim3VectorMake(0,0.00001,0);
return result;
}

// DWORD next_game_tick = GetTickCount();
// int loops;
// float interpolation;
void SimContextDestroy(SimContextRef ref) {
SimContext self = ref;
SimVehicleDestroy(self->vehicle);
free(ref);
}

// bool game_is_running = true;
// while( game_is_running ) {
void SimContextUpdate(SimContextRef c, double t) {
SimContext ctx = c;

// loops = 0;
// while( GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) {
// update_game();
SimVehicleUpdate(ctx->vehicle, t);
}

// next_game_tick += SKIP_TICKS;
// loops++;
// }
void SimContextDraw(SimContextRef c, double t) {
SimContext ctx = c;

glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();

// interpolation = float( GetTickCount() + SKIP_TICKS - next_game_tick )
// / float( SKIP_TICKS );
// display_game( interpolation );
// }
glTranslatef(0,-1,0);
glScalef(.1,.1,.1);

SimVehicleDraw(ctx->vehicle, t);

glPopMatrix();

glFlush();
}
1 change: 0 additions & 1 deletion sim/sim.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


typedef void *SimContextRef;

SimContextRef SimContextCreate();
Expand Down

0 comments on commit 8488128

Please sign in to comment.