Skip to content

Commit

Permalink
Merge branch 'collision' - I should have done this ages ago.
Browse files Browse the repository at this point in the history
Conflicts:
	src/bindings.cpp
	src/includes.h
	src/main.cpp
	src/ph-utils
	src/player.h
  • Loading branch information
ChickenProp committed Dec 28, 2010
2 parents d91c1be + ba00a6a commit e327fc5
Show file tree
Hide file tree
Showing 25 changed files with 596 additions and 106 deletions.
Binary file added media/crosshair.tga
Binary file not shown.
5 changes: 3 additions & 2 deletions src/SConstruct
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
env = Environment()
env = Environment(CPPPATH=['/usr/include/bullet'])

debug = ARGUMENTS.get('debug', 0)
if int(debug):
env.Append(CCFLAGS = '-g')
env.Append(CPPDEFINES = 'DEBUG')

env.Program("#gel-on-earth", Glob('*.cpp'),
LIBS=['GL', 'GLU', 'sfml-system', 'sfml-window', 'sfml-graphics'])
LIBS=['GL', 'GLU', 'sfml-system', 'sfml-window', 'sfml-graphics',
'BulletDynamics'])
15 changes: 15 additions & 0 deletions src/bindings.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "bindings.h"

#include "globals.h"
#include "world.h"
#include "menu.h"

void setupKeyBindings() {
sf::Event e;

Expand All @@ -21,6 +25,10 @@ void setupKeyBindings() {
e.Type = sf::Event::MouseMoved;
G::gameScreen->keymap.bindEvent(e, &lookAround);

e.Type = sf::Event::MouseButtonPressed;
e.MouseButton.Button = sf::Mouse::Left;
G::gameScreen->keymap.bindEvent(e, &playershoot);

G::gameScreen->keymap.bindKey(sf::Key::Left, &strafeLeft);
G::gameScreen->keymap.bindKey(sf::Key::Right, &strafeRight);
G::gameScreen->keymap.bindKey(sf::Key::Down, &strafeBack);
Expand All @@ -41,6 +49,13 @@ void lookAround(sf::Event ev, bool real) {
if (v.x == 0 && v.y == 0)
return;
G::gameScreen->player.changeOrientationWithMouse(v.x, v.y);
<<<<<<< HEAD
=======
}

void playershoot(sf::Event ev, bool) {
G::gameScreen->player.shoot();
>>>>>>> collision
}

void strafeFwd(sf::Event, bool) {
Expand Down
3 changes: 2 additions & 1 deletion src/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#define _BINDINGS_H

#include "includes.h"
#include "globals.h"

void setupKeyBindings();

void quitGame(sf::Event ev, bool real);

void lookAround(sf::Event ev, bool real);

void playershoot(sf::Event ev, bool);

void strafeFwd(sf::Event, bool);
void strafeBack(sf::Event, bool);
void strafeLeft(sf::Event, bool);
Expand Down
22 changes: 22 additions & 0 deletions src/bullet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "bullet.h"

Bullet::Bullet(ph::vec3f pos, ph::vec3f vel) {
shape = new btSphereShape(0.2);

btScalar mass = 1;
btVector3 inertia = vel;
btMotionState *state = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), pos));

initialize(mass, inertia, state);

body->setGravity(btVector3(0,0,0));
body->setCcdMotionThreshold(0.2);
body->setCcdSweptSphereRadius(0.2);
}







13 changes: 13 additions & 0 deletions src/bullet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef GOE_BULLET_H
#define GOE_BULLET_H

#include "entity.h"

class Bullet : public Entity {
public:
typedef Entity super;

Bullet(ph::vec3f pos, ph::vec3f vel);
};

#endif
32 changes: 32 additions & 0 deletions src/debug-draw.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "debug-draw.h"
#include "vertex.h"

void DebugDraw::drawLine(const btVector3 &from, const btVector3 &to,
const btVector3 &color)
{
Vertex line[] = {
Vertex(from, color.x(), color.y(), color.z()),
Vertex(to, color.x(), color.y(), color.z())
};

line->draw(GL_LINES, 2);
}

void DebugDraw::setDebugMode(int m) {
mode = m;
}
int DebugDraw::getDebugMode() const {
return mode;
}

void DebugDraw::drawContactPoint(const btVector3&, const btVector3&, btScalar,
int, const btVector3&)
{
printf("drawContactPoint()");
}
void DebugDraw::reportErrorWarning(const char*) {
printf("reportErrorWarning()");
}
void DebugDraw::draw3dText(const btVector3&, const char*) {
printf("draw3dText()");
}
23 changes: 23 additions & 0 deletions src/debug-draw.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef GOE_DEBUG_DRAW_H
#define GOE_DEBUG_DRAW_H

#include "includes.h"

class DebugDraw : public btIDebugDraw {
public:
int mode;

void drawLine(const btVector3 &from, const btVector3 &to,
const btVector3 &color);

void setDebugMode(int);
int getDebugMode() const;

/* These are pure virtual, but I haven't really implemented them. */
void drawContactPoint(const btVector3&, const btVector3&, btScalar,
int, const btVector3&);
void reportErrorWarning(const char*);
void draw3dText(const btVector3&, const char*);
};

#endif
44 changes: 44 additions & 0 deletions src/entity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "entity.h"
#include "globals.h"

Entity::Entity ()
: shape(NULL), body(NULL)
{}

Entity::~Entity() {
if (shape)
delete shape;

if (body) {
G::physics->removeRigidBody(body);

if (body->getMotionState())
delete body->getMotionState();

delete body;
}
}

void Entity::initialize (btScalar mass, btVector3 vel, btMotionState *state)
{
if (shape) {
btVector3 inertia;
shape->calculateLocalInertia(mass, inertia);
btRigidBody::btRigidBodyConstructionInfo
construct(mass, state, shape, inertia);
body = new btRigidBody(construct);

body->setLinearVelocity(vel);
body->setActivationState(DISABLE_DEACTIVATION);

G::physics->addRigidBody(body);
}
}

void Entity::update () {}
void Entity::draw () {}





22 changes: 22 additions & 0 deletions src/entity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef GOE_ENTITY_H
#define GOE_ENTITY_H

#include "includes.h"

class Entity {
public:
Entity();
~Entity();

virtual void initialize(btScalar mass, btVector3 vel,
btMotionState *state);

virtual void update();
virtual void draw();

btCollisionShape* shape;
btRigidBody *body;
};

#endif

19 changes: 17 additions & 2 deletions src/globals.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
#include "globals.h"

sf::RenderWindow G::window;
sf::Clock G::clock;
int G::windowWidth = 640;
int G::windowHeight = 480;
sf::Vector2f G::windowCentre (320.0f, 240.0f);
float G::clipNear = 1.0;
float G::clipNear = 0.8;
float G::clipFar = 10.0;
float G::framerate = 60;

sf::Clock G::clock;

World *G::gameScreen = NULL;
Menu *G::menuScreen = NULL;
Screen *G::curScreen = NULL;

bool G::debugMode = 0;

btDiscreteDynamicsWorld *G::physics = NULL;

btCollisionShape* G::shapes::ground
= new btStaticPlaneShape(btVector3(0, 0, 1), 0);

sf::Image G::images::wall;

void G::loadImages() {
images::wall.LoadFromFile("media/wall.tga");
}

sf::Vector2i G::getMouseMoveDelta(sf::Event e) {
return sf::Vector2i((int) windowCentre.x - e.MouseMove.X,
(int) windowCentre.y - e.MouseMove.Y);
Expand Down
21 changes: 18 additions & 3 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
#define _GLOBALS_H

#include "includes.h"
#include "screen.h"
#include "world.h"
#include "menu.h"

class Screen;
class World;
class Menu;

class G {
public:
Expand All @@ -14,6 +15,7 @@ class G {
static sf::Vector2f windowCentre;
static float clipNear;
static float clipFar;
static float framerate;

/* We use this because SFML doesn't provide delta coordinates in the
MouseMove event. It relies on us resetting the cursor back to the
Expand All @@ -26,6 +28,19 @@ class G {
static World *gameScreen;
static Menu *menuScreen;
static Screen *curScreen;

static bool debugMode;

static btDiscreteDynamicsWorld *physics;

struct shapes {
static btCollisionShape *ground;
};

struct images {
static sf::Image wall;
};
static void loadImages();
};

#endif
7 changes: 7 additions & 0 deletions src/includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@
#include <GL/gl.h>
#include <GL/glu.h>

#include <bullet/BulletDynamics/btBulletDynamicsCommon.h>
#include <bullet/BulletCollision/CollisionShapes/btBox2dShape.h>

#include <cstdio>
#include <iostream>
#include <map>

#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH

#include "ph-utils/vector.h"

// I took this from SFML/Graphics/GraphicsContext.hpp, but it doesn't seem
Expand Down
45 changes: 42 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,50 @@
#include "bindings.h"
#include "fps.h"
#include "world.h"
#include "menu.h"
#include "debug-draw.h"

int main(int argc, char **argv) {
G::loadImages();

// Build the broadphase
btBroadphaseInterface* broadphase = new btDbvtBroadphase();

// Set up the collision configuration and dispatcher
btDefaultCollisionConfiguration* collisionConfiguration
= new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher
= new btCollisionDispatcher(collisionConfiguration);

// The actual physics solver
btSequentialImpulseConstraintSolver* solver
= new btSequentialImpulseConstraintSolver;

// The world.
G::physics = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver,
collisionConfiguration);
G::physics->setGravity(btVector3(0, 0, -10));

DebugDraw *dbg = new DebugDraw();
G::physics->setDebugDrawer(dbg);
dbg->setDebugMode(btIDebugDraw::DBG_DrawAabb
| btIDebugDraw::DBG_DrawWireframe);

if (argc >= 2)
G::debugMode = 1;

int main()
{
G::window.Create(sf::VideoMode(G::windowWidth, G::windowHeight, 32),
"Gel on Earth");
//G::window.PreserveOpenGLStates(true);
G::window.ShowMouseCursor(false);
int cX = (int) G::windowCentre.x;
int cY = (int) G::windowCentre.y;
G::window.SetCursorPosition(cX, cY);
G::window.SetFramerateLimit(G::framerate);

G::gameScreen = new World();
G::menuScreen = new Menu();
G::curScreen = G::menuScreen;
G::curScreen = G::gameScreen;
setupKeyBindings();

sf::String fpsStr("", sf::Font::GetDefaultFont(), 15);
Expand Down Expand Up @@ -96,5 +126,14 @@ int main()
G::window.Display();
}

delete G::gameScreen;
delete G::menuScreen;

delete broadphase;
delete collisionConfiguration;
delete dispatcher;
delete solver;
delete G::physics;

return 0;
}
Loading

0 comments on commit e327fc5

Please sign in to comment.