Skip to content

Commit

Permalink
Added include guards and generally cleaned up includes. Added viscosi…
Browse files Browse the repository at this point in the history
…ty to air.
  • Loading branch information
bgreen committed Jul 18, 2009
1 parent aba729e commit d6ee9b4
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 24 deletions.
1 change: 1 addition & 0 deletions BM.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <SDL/SDL_ttf.h>

#include "character.h"
#include "object.h"
#include "fps_counter.h"
#include "static.h"

Expand Down
14 changes: 11 additions & 3 deletions character.cc
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@

#include <SDL/SDL.h>
#include <queue>

#include "character.h"
#include "physics.h"

Character::Character() {
layer = 0;

// in kg
mass = 80;
vel = PVector();
weight = PVector(0, mass*(-9.81*16.67));
weight = PVector(0, mass*(-9.81 * PIXELS_PER_METER));
force = PVector();
force += weight;
}

void Character::handle_move(double dt) {
// apply acceleration
vel.x += dt * force.x/mass;
vel.y += dt * force.y/mass;

// apply a set air viscosity
vel.x *= (1 - AIR_VISCOSITY * dt);
vel.y *= (1 - AIR_VISCOSITY * dt);

// 30 pixels = 1.8 meters
// 16.67 pixels/meter

Expand All @@ -35,8 +43,8 @@ Player::Player() {

void Player::handle_input() {
//SDL_Event event;
PVector one_x (5000.0,0);
PVector one_y (0,30000.0);
PVector one_x (mass*10*PIXELS_PER_METER,0);
PVector one_y (0,mass*2*9.81*PIXELS_PER_METER);
while(!input.empty()) {
// event = input.front();
// if(event.type == SDL_KEYDOWN) {
Expand Down
9 changes: 6 additions & 3 deletions character.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#ifndef _INC_CHARACTER
#define _INC_CHARACTER

#include <stdlib.h>
#include <queue>
#include <SDL/SDL.h>

#ifndef INC_OBJECT
#define INC_OBJECT
#include "object.h"
#endif
#include "physics.h"

class Character: public Object {
public:
Expand Down Expand Up @@ -33,3 +34,5 @@ class Player: public Character {
void collide(Object* other, double dt);
void handle_input();
};

#endif
3 changes: 3 additions & 0 deletions fps_counter.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"

#include "fps_counter.h"

FPS_counter::FPS_counter() {
Expand Down
5 changes: 5 additions & 0 deletions fps_counter.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef _INC_FPS_COUNTER
#define _INC_FPS_COUNTER

#include <stdlib.h>

#include <SDL/SDL.h>
Expand All @@ -20,3 +23,5 @@ class FPS_counter {

void delay_until(int);
};

#endif
6 changes: 3 additions & 3 deletions object.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef INC_OBJECT
#define INC_OBJECT

#include "SDL/SDL.h"

#include "object.h"
#endif

void Object::handle_show(SDL_Surface* scene[]) {
SDL_Rect tmp;
Expand Down
7 changes: 7 additions & 0 deletions object.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#ifndef _INC_OBJECT
#define _INC_OBJECT


#include <SDL/SDL.h>
#include <vector>

#include "physics.h"

struct coord {
Expand Down Expand Up @@ -33,3 +38,5 @@ struct collision_pair {
Object* a;
Object* b;
};

#endif
8 changes: 6 additions & 2 deletions physics.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@

#include <algorithm>

#include <SDL/SDL.h>

#include "physics.h"
#include "math.h"
#include <algorithm>
#include "SDL/SDL.h"


/************/
/* PVector */
Expand Down
8 changes: 8 additions & 0 deletions physics.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#ifndef _INC_PHYSICS
#define _INC_PHYSICS

#include "stdint.h"

#define AIR_VISCOSITY 0.2
#define PIXELS_PER_METER 16.67

class PVector {
public:

Expand Down Expand Up @@ -33,3 +39,5 @@ class Timer {
unsigned int dt_ms();
double dt_s();
};

#endif
16 changes: 6 additions & 10 deletions static.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

#include <stdio.h>

#include "static.h"
#include "character.h"
#include "math.h"
#include <stdio.h>

Wall::Wall(SDL_Rect dim) {
position.x = dim.x;
Expand Down Expand Up @@ -124,33 +126,27 @@ void Wall::collide(Object* other, double dt) {
y = m * x + b;
break;
}
printf("%d:[%f,%f]; (%f,%f)\n",side, lower_bound, upper_bound, x,y);
if((lower_bound <= y) && (y <= upper_bound)) {
float dist = sqrt(pow(x - path.x, 2) + pow(y - path.y, 2));
if(dist > max_dist) {
max_dist = dist;
float dist_squared = pow(x - path.x, 2) + pow(y - path.y, 2);
if(dist_squared > max_dist) {
max_dist = dist_squared;
hit_vector = path;
hit_where = side;
}
}
}

// compute how far to move the object back
float mag = max_dist / sqrt(pow(hit_vector.dx, 2) + pow(hit_vector.dy, 2));
if(hit_where == top) {
printf("hit top\n"); fflush(stdout);
o->position.y = position.y + o->hitboxes.front().h;
o->vel.y = 0;
} else if(hit_where == bottom) {
printf("hit bottom\n"); fflush(stdout);
o->position.y = position.y - hitboxes.front().h;
o->vel.y = 0;
} else if(hit_where == left) {
printf("hit left\n"); fflush(stdout);
o->position.x = position.x - o->hitboxes.front().w;
o->vel.x = 0;
} else if(hit_where == right) {
printf("hit right\n"); fflush(stdout);
o->position.x = position.x + hitboxes.front().w;
o->vel.x = 0;
}
Expand Down
8 changes: 5 additions & 3 deletions static.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#ifndef INC_OBJECT
#define INC_OBJECT
#ifndef _INC_STATIC
#define _INC_STATIC

#include "object.h"
#endif

class Wall: public Object {
public:
Wall(SDL_Rect dim);
void collide(Object* other, double dt);
};

#endif

0 comments on commit d6ee9b4

Please sign in to comment.