Skip to content

Commit

Permalink
Merge pull request #94 from abekkine/br_SpeedLimit
Browse files Browse the repository at this point in the history
Maximum speed limited by use of lorentz factor (Closes #73)
  • Loading branch information
abekkine authored Apr 18, 2018
2 parents 7c737bc + d93cb87 commit 0561eaf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
14 changes: 14 additions & 0 deletions game_definitions.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "game_definitions.h"

#include <math.h>

namespace GameDefinitions {

MenuItemEnum & operator++ (MenuItemEnum & m) {
Expand All @@ -26,5 +28,17 @@ MenuItemEnum & operator-- (MenuItemEnum & m) {
}
}

double LorentzFactor(double v) {
double lf;
double r = v / kSpeedOfLight;
if (r > 1.0) {
lf = kEpsilon;
}
else {
lf = sqrt(1.0 - r*r);
}
return lf;
}

} // namespace GameDefinitions

4 changes: 4 additions & 0 deletions game_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace GameDefinitions {

const double kEpsilon = 0.001;
const double kGravityConstant = 0.7;
const double kSpeedOfLight = 180.0;

enum OrbitDirEnum {
od_Clockwise,
Expand All @@ -27,6 +29,8 @@ enum MenuItemEnum {
MenuItemEnum & operator++ (MenuItemEnum & m);
MenuItemEnum & operator-- (MenuItemEnum & m);

double LorentzFactor(double v);

} // namespace GameDefinitions

#endif // GAME_DEFINITIONS_H_
16 changes: 9 additions & 7 deletions space_ship.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SpaceShip {
double density_;
double thrust_force_;
double moment_;

b2Vec2 position_;
b2Vec2 velocity_;
b2Vec2 gravity_;
Expand All @@ -46,7 +46,7 @@ class SpaceShip {

float color_[3];
public:
SpaceShip()
SpaceShip()
: engine_(0)
, radar_(0)
, kMaxHullStrength(10.0)
Expand Down Expand Up @@ -152,10 +152,6 @@ class SpaceShip {
// Begin -- Handlers for Engine system.
void hndThrustOut(double value) {
thrust_force_ = value;

BD_Scalar thrust;
thrust.value = thrust_force_;
DATABUS.Publish(db_PlayerThrust, &thrust);
}
void hndMomentOut(double value) {
moment_ = value;
Expand All @@ -172,7 +168,9 @@ class SpaceShip {
physics_body_->ApplyTorque(moment_, true);
// Get velocity for devices.
velocity_ = physics_body_->GetLinearVelocity();

double speed = velocity_.Length();
double lf = GameDefinitions::LorentzFactor(speed);
physics_body_->GetFixtureList()->SetDensity( density_ / lf );
// Get position.
position_ = physics_body_->GetPosition();
angle_ = physics_body_->GetAngle() * 180.0 / M_PI;
Expand All @@ -192,6 +190,10 @@ class SpaceShip {
s.value = angle_;
DATABUS.Publish(db_PlayerAngle, &s);

BD_Scalar thrust;
thrust.value = thrust_force_ * lf;
DATABUS.Publish(db_PlayerThrust, &thrust);

engine_->Update(delta_time);
radar_->Update(delta_time);
}
Expand Down

0 comments on commit 0561eaf

Please sign in to comment.