Skip to content

Commit

Permalink
Basic thruster effects implemented (Closes #59)
Browse files Browse the repository at this point in the history
- EffectsManager class created.
- Thruster effects (main & bow) implemented.
- Thrust & Moments reset if hull depleted.
- Effects life cycle updated in universe thread.
  • Loading branch information
abekkine committed Apr 19, 2018
1 parent 0561eaf commit 7008e85
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 5 deletions.
107 changes: 107 additions & 0 deletions effects_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#ifndef EFFECTS_MANAGER_H_
#define EFFECTS_MANAGER_H_

#include <unistd.h>
#include <math.h>
#include <GLFW/glfw3.h>
#include <Box2D.h>

class EffectsManager {
public:
EffectsManager() {}
~EffectsManager() {}
void Init() {}
void Update(double time_step) {
UpdateThrust(time_step);
}
void Render() {
RenderThrust();
}
// BEGIN Thruster
struct ThrustParticle {
ThrustParticle()
: x(0.0), y(0.0), vx(0.0), vy(0.0), life(0.0) {}
~ThrustParticle() {}
void SetPosition(b2Vec2 position) {
x = position.x;
y = position.y;
life = 0.25;
}
void SetThrust(b2Vec2 thrust, b2Vec2 velocity) {
double ta = atan2(thrust.y, thrust.x) - M_PI;
ta = fmod(ta, 2.0 * M_PI);
double td = (drand48() * M_PI / 6.0) - M_PI / 12.0;
vx = velocity.x + 1.00 * thrust.Length() * cos(ta + td);
vy = velocity.y + 1.00 * thrust.Length() * sin(ta + td);
}
void Update(double time_step) {
if (life > 0.0) {
life -= time_step;
x += time_step * vx;
y += time_step * vy;
}
}
double x, y;
double vx, vy;
double life;
};
ThrustParticle thrust_particles_[100];
int tp_index_ = 0;
void MainThruster(b2Vec2 thrust, b2Vec2 position, b2Vec2 velocity) {
if (thrust.Length() > 0.0) {
thrust_particles_[tp_index_].SetPosition(position);
thrust_particles_[tp_index_].SetThrust(thrust, velocity);
tp_index_++;
tp_index_ = tp_index_ % 100;
}
}
int bow_index;
void BowThruster(double moment, double angle, b2Vec2 position, b2Vec2 velocity) {
bow_index++;
bow_index &= 0x3;
if (bow_index != 0) return;

double abs_moment = fabs(moment);
if (abs_moment > 0.0) {
b2Vec2 t;
b2Vec2 p;
double a;
if (moment > 0.0) {
a = (angle - 180.0) * M_PI / 180.0;
} else if (moment < 0.0) {
a = (angle) * M_PI / 180.0;
}
p.x = position.x + 1.0 * cos((angle + 90.0) * M_PI / 180.0);
p.y = position.y + 1.0 * sin((angle + 90.0) * M_PI / 180.0);
t.x = 20.0 * abs_moment * cos(a);
t.y = 20.0 * abs_moment * sin(a);
thrust_particles_[tp_index_].SetPosition(p);
thrust_particles_[tp_index_].SetThrust(t, velocity);
tp_index_++;
tp_index_ = tp_index_ % 100;
}
}
void UpdateThrust(double time_step) {

for (int i=0; i<100; ++i) {
thrust_particles_[i].Update(time_step);
}
}
void RenderThrust() {

glColor3f(1.0, 1.0, 1.0);
glPointSize(3.0);
glBegin(GL_POINTS);
for (int i=0; i<100; ++i) {
if (thrust_particles_[i].life > 0.0) {
glColor4f(1.0, 1.0, 1.0, 4.0 * thrust_particles_[i].life);
glVertex2d(thrust_particles_[i].x, thrust_particles_[i].y);
}
}
glEnd();
}
// END Thruster
};

#endif // EFFECTS_MANAGER_H_

8 changes: 8 additions & 0 deletions game_play.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "planet.h"
#include "space_ship.h"
#include "effects_manager.h"

#include "object_manager.h"

Expand All @@ -19,6 +20,7 @@ class GamePlay {
double debug_scale_;
int num_planets_;
Planet* planet_;
EffectsManager* effects_;
Background background_;

public:
Expand All @@ -33,6 +35,7 @@ class GamePlay {
ship_ = static_cast<SpaceShip*>(OBJMGR.Get("ship"));
num_planets_ = *(static_cast<int*>(OBJMGR.Get("nplanets")));
planet_ = static_cast<Planet *>(OBJMGR.Get("planets"));
effects_ = static_cast<EffectsManager *>(OBJMGR.Get("effects"));
}

SpaceShip* ship_;
Expand Down Expand Up @@ -62,6 +65,7 @@ class GamePlay {

RenderBackground();
RenderUniverse();
RenderEffects();

glPopMatrix();

Expand Down Expand Up @@ -107,6 +111,10 @@ class GamePlay {
glPopMatrix();
}
}
void RenderEffects() {

effects_->Render();
}

void RenderBackground() {
background_.Render(ship_x_, ship_y_, ship_angle_);
Expand Down
30 changes: 25 additions & 5 deletions space_ship.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include <functional>

#include "effects_manager.h"
#include "object_manager.h"

#include "data_bus.h"

#include "systems/engine_system_interface.h"
Expand All @@ -24,6 +27,7 @@ class SpaceShip {
RadarSystemInterface * radar_;
GenericHudDevice hud_;
HOTASDevice hotas_;
EffectsManager * effects_;

// TODO
const float kMaxHullStrength;
Expand All @@ -49,6 +53,7 @@ class SpaceShip {
SpaceShip()
: engine_(0)
, radar_(0)
, effects_(0)
, kMaxHullStrength(10.0)
, kImpulseThreshold(1.0)
, hull_strength_(kMaxHullStrength)
Expand Down Expand Up @@ -148,6 +153,8 @@ class SpaceShip {

hud_.Init();
hotas_.Init();

effects_ = static_cast<EffectsManager*>(OBJMGR.Get("effects"));
}
// Begin -- Handlers for Engine system.
void hndThrustOut(double value) {
Expand All @@ -161,11 +168,24 @@ class SpaceShip {
// Gravity
physics_body_->ApplyForceToCenter(gravity_, true);
// Thrust
thrust_.x = thrust_force_ * cos(0.5 * M_PI + physics_body_->GetAngle());
thrust_.y = thrust_force_ * sin(0.5 * M_PI + physics_body_->GetAngle());
physics_body_->ApplyForceToCenter(thrust_, true);
// Moment
physics_body_->ApplyTorque(moment_, true);
if (hull_strength_ > 0.0) {
thrust_.x = thrust_force_ * cos(0.5 * M_PI + physics_body_->GetAngle());
thrust_.y = thrust_force_ * sin(0.5 * M_PI + physics_body_->GetAngle());
physics_body_->ApplyForceToCenter(thrust_, true);

effects_->MainThruster(thrust_, position_, velocity_);

// Moment
physics_body_->ApplyTorque(moment_, true);

effects_->BowThruster(moment_, angle_, position_, velocity_);
}
else {
thrust_force_ = 0.0;
moment_ = 0.0;
thrust_.x = 0.0;
thrust_.y = 0.0;
}
// Get velocity for devices.
velocity_ = physics_body_->GetLinearVelocity();
double speed = velocity_.Length();
Expand Down
13 changes: 13 additions & 0 deletions universe.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "space_ship.h"
#include "planet.h"
#include "effects_manager.h"

#include "object_manager.h"

Expand Down Expand Up @@ -74,6 +75,7 @@ class Universe {
, world_(0)
, space_ship_(0)
, planets_(0)
, effects_(0)
, state_(GameDefinitions::gameState_InMenu)
{}
~Universe() {
Expand All @@ -89,6 +91,10 @@ class Universe {
}

void Init() {
effects_ = new EffectsManager();
effects_->Init();
OBJMGR.Set("effects", effects_);

// Instantiate player ship.
space_ship_ = new SpaceShip();
space_ship_->SetPosition(0.0, 100.0);
Expand Down Expand Up @@ -167,6 +173,8 @@ class Universe {

UpdatePlanet();

UpdateEffects(delta_time);

Step(delta_time);

t_end_ = t_begin_;
Expand All @@ -187,6 +195,10 @@ class Universe {

space_ship_->Update(delta_time);
}
void UpdateEffects(double delta_time) {

effects_->Update(delta_time);
}
void UpdatePlanet() {

for (int i=0; i<kNumPlanets; ++i) {
Expand All @@ -212,6 +224,7 @@ class Universe {
std::thread thread_;
SpaceShip * space_ship_;
Planet * planets_;
EffectsManager * effects_;
GameDefinitions::GameStateEnum state_;
};

Expand Down

0 comments on commit 7008e85

Please sign in to comment.