Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Restructure ship state, fix braking, slightly more useful rendering

  • Loading branch information...
commit a03578c70ec126675e92a070cad2013d8dee5918 1 parent 326c854
@brendonh authored
View
6 client/include/microcosm.hpp
@@ -1,7 +1,9 @@
#ifndef __MICROCOSM_MICROCOSM
#define __MICROCOSM_MICROCOSM
-#define DEG2RAD 3.1415927 / 180.f
-#define RAD2DEG 180.f / 3.1415927
+#define PI 3.1415927
+#define TWOPI (2*PI)
+#define DEG2RAD (PI / 180.f)
+#define RAD2DEG (180.f / PI)
#endif
View
32 client/include/pushbox.hpp
@@ -6,9 +6,17 @@
#include "microcosm.hpp"
namespace Microcosm {
+
+ enum ship_states {
+ SHIP_THRUST = 0,
+ SHIP_BRAKE = 1,
+ SHIP_TURN_LEFT = 2,
+ SHIP_TURN_RIGHT = 3
+ };
class PushBox {
public:
+
b2World* world;
b2Body* body;
@@ -21,17 +29,37 @@ namespace Microcosm {
float maxSpeed;
float maxTurn;
+ char state;
+
+ bool engine;
+
PushBox(b2World* world, float x, float y);
+ void setState(int s, bool v);
+
+ bool getState(int s);
+ void setState(int s);
+ void clearState(int s);
+
void thrust();
- void turn(int sign);
+ void turn(int dir);
+ void brake();
+
void stopTurn();
- void brake(int on);
void update();
void render();
};
+ inline void PushBox::setState(int s, bool v) {
+ if (v) setState(s);
+ else clearState(s);
+ }
+
+ inline bool PushBox::getState(int s) { return state & (1 << s); }
+ inline void PushBox::setState(int s) { state |= (1 << s); }
+ inline void PushBox::clearState(int s) { state &= ~(1 << s); }
+
}
#endif
View
39 client/src/game.cpp
@@ -27,17 +27,12 @@ void Game::mainloop() {
handleEvents();
- int iterations = 0;
-
while(unrenderedTime > timeStep) {
- iterations++;
+ pb->update();
world->Step(timeStep, 10, 10);
unrenderedTime -= timeStep;
}
- pb->update();
- stat->update();
-
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
window->SaveGLStates();
@@ -59,7 +54,7 @@ void Game::mainloop() {
void Game::initSFML() {
if (fullscreen) {
sf::VideoMode vm = sf::VideoMode::GetDesktopMode();
- window = new sf::RenderWindow(vm, "Box2D Sandbox", sf::Style::Fullscreen);
+ window = new sf::RenderWindow(vm, "Box2D Sandbox", sf::Style::None);
} else {
window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "Box2D Sandbox");
}
@@ -104,29 +99,21 @@ void Game::handleEvents() {
while (window->GetEvent(Event)) {
if (Event.Type == sf::Event::Closed)
window->Close();
- if (Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Escape)
- window->Close();
-
- if (Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Down)
- pb->brake(true);
- if (Event.Type == sf::Event::KeyReleased && Event.Key.Code == sf::Key::Down)
- pb->brake(false);
+ if (Event.Type == sf::Event::KeyPressed || Event.Type == sf::Event::KeyReleased) {
+ switch(Event.Key.Code) {
+ case sf::Key::Escape:
+ window->Close();
+ break;
+ }
+ }
}
const sf::Input& Input = window->GetInput();
- if (Input.IsKeyDown(sf::Key::Up)) pb->thrust();
- bool turning = false;
- if (Input.IsKeyDown(sf::Key::Right)) {
- turning = !turning;
- pb->turn(-1);
- }
+ pb->setState(SHIP_THRUST, Input.IsKeyDown(sf::Key::Up));
+ pb->setState(SHIP_BRAKE, Input.IsKeyDown(sf::Key::Down));
+ pb->setState(SHIP_TURN_LEFT, Input.IsKeyDown(sf::Key::Left));
+ pb->setState(SHIP_TURN_RIGHT, Input.IsKeyDown(sf::Key::Right));
- if (Input.IsKeyDown(sf::Key::Left)) {
- turning = !turning;
- pb->turn(1);
- }
-
- if (!turning) pb->stopTurn();
}
View
78 client/src/pushbox.cpp
@@ -1,3 +1,5 @@
+#include <stdio.h>
+
#include <GL/gl.h>
#include "pushbox.hpp"
@@ -8,10 +10,16 @@ inline b2Vec2 rad2vec(float r) {
return b2Vec2(cos(r), sin(r));
}
+inline float vec2rad(b2Vec2 v) {
+ return atan2(v.y, v.x);
+}
+
PushBox::PushBox(b2World* world, float x, float y)
: world(world),
mainThrust(200.0f),
- turnThrust(40000.0f) {
+ turnThrust(100000.0f) {
+
+ maxSpeed = 80;
position.x = x;
position.y = y;
@@ -21,7 +29,9 @@ PushBox::PushBox(b2World* world, float x, float y)
bodyDef->type = b2_dynamicBody;
bodyDef->position.Set(x, y);
bodyDef->angle = angle;
+
bodyDef->linearDamping = 0;
+ bodyDef->angularDamping = 0;
body = world->CreateBody(bodyDef);
b2PolygonShape *dynamicBox = new b2PolygonShape();
@@ -34,17 +44,24 @@ PushBox::PushBox(b2World* world, float x, float y)
body->CreateFixture(fixtureDef);
mainThrust /= body->GetMass();
- maxSpeed = 50;
- brake(false);
}
void PushBox::update() {
- position = body->GetPosition();
- angle = body->GetAngle();
+
+ engine = false;
+
+ if (getState(SHIP_THRUST)) thrust();
+ else if (getState(SHIP_BRAKE)) brake();
+
+ int dir = getState(SHIP_TURN_LEFT) - getState(SHIP_TURN_RIGHT);
+ if (dir) turn(dir);
+ else stopTurn();
+
}
void PushBox::thrust() {
+ engine = true;
b2Vec2 force = rad2vec(angle);
force *= mainThrust;
b2Vec2 vel = body->GetLinearVelocity();
@@ -69,38 +86,61 @@ void PushBox::thrust() {
body->SetLinearVelocity(vel);
}
-void PushBox::turn(int sign) {
+void PushBox::turn(int dir) {
float vel = body->GetAngularVelocity();
- float force = 100000.0f * (1 - (fabs(vel) / 6.0f));
- body->ApplyTorque(sign * force);
+ float force = turnThrust * (1 - (fabs(vel) / PI));
+ body->ApplyTorque(dir * force);
}
void PushBox::stopTurn() {
float vel = body->GetAngularVelocity();
- if (fabs(vel) < 0.1) {
+ if (fabs(vel) < 0.05) {
body->SetAngularVelocity(0);
return;
}
- float prop = (fabs(vel) / 6.0f);
- float force = 500000.0f * prop;
- if (force < 1000.f) force = 1000.f;
- if (vel > 0) force = -force;
- body->ApplyTorque(force);
+
+ body->SetAngularVelocity(vel * 0.9);
+ return;
}
-void PushBox::brake(int on) {
- if (on) body->SetLinearDamping(0.3f);
- else body->SetLinearDamping(0);
+void PushBox::brake() {
+ b2Vec2 vel = body->GetLinearVelocity();
+ if (!vel.Length()) return;
+
+ float angle = body->GetAngle();
+ float wantAngle = vec2rad(-vel);
+ float diff = wantAngle - angle;
+
+ if (fabs(diff) < 0.01) {
+ body->SetAngularVelocity(0);
+ body->SetTransform(body->GetPosition(), wantAngle);
+ thrust();
+
+ b2Vec2 newVel = body->GetLinearVelocity();
+ float component = b2Dot(newVel, vel);
+ if (component < 0) body->SetLinearVelocity(b2Vec2(0, 0));
+ return;
+ }
+
+ float mod = sqrt(fabs(diff)) * (500 / body->GetMass());
+ if (diff < 0) mod = -mod;
+
+ body->SetAngularVelocity(mod);
}
void PushBox::render() {
+ position = body->GetPosition();
+ angle = body->GetAngle();
+
glLoadIdentity();
glTranslatef(position.x, position.y, 0);
glRotatef(angle * RAD2DEG, 0, 0, 1);
glBegin(GL_QUADS);
+ if (engine) glColor3f(1.0, 0.0, 0.0);
glVertex3f(-10.f, -10.f, 0);
glVertex3f(-10.f, 10.f, 0);
- glVertex3f( 10.f, 10.f, 0);
- glVertex3f( 10.f, -10.f, 0);
+ glColor3f(1.0, 1.0, 1.0);
+ glVertex3f( 10.f, 7.f, 0);
+ glVertex3f( 10.f, -7.f, 0);
glEnd();
}
Please sign in to comment.
Something went wrong with that request. Please try again.