Skip to content

Commit

Permalink
Activate the cockpit if it's being used
Browse files Browse the repository at this point in the history
  • Loading branch information
fluffyfreak committed Feb 11, 2018
1 parent 951c154 commit c90197b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
6 changes: 4 additions & 2 deletions src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ void Player::InitCockpit()
}
if (!cockpitModelName.empty())
m_cockpit.reset(new ShipCockpit(cockpitModelName));

OnCockpitActivated();
}

bool Player::DoCrushDamage(float kgDamage)
Expand Down Expand Up @@ -265,7 +267,7 @@ void Player::AbortHyperjump()
void Player::OnCockpitActivated()
{
if (m_cockpit)
m_cockpit->OnActivated();
m_cockpit->OnActivated(this);
}

void Player::StaticUpdate(const float timeStep)
Expand All @@ -275,7 +277,7 @@ void Player::StaticUpdate(const float timeStep)
// XXX even when not on screen. hacky, but really cockpit shouldn't be here
// anyway so this will do for now
if (m_cockpit)
m_cockpit->Update(timeStep);
m_cockpit->Update(this, timeStep);
}

int Player::GetManeuverTime() const {
Expand Down
55 changes: 26 additions & 29 deletions src/ShipCockpit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
#include "WorldView.h"

ShipCockpit::ShipCockpit(const std::string &modelName) :
m_shipDir(0.0),
m_shipYaw(0.0),
m_dir(0.0),
m_yaw(0.0),
m_rotInterp(0.f),
m_transInterp(0.f),
m_gForce(0.f),
m_offset(0.f),
m_shipVel(0.f),
m_translate(0.0),
m_transform(matrix4x4d::Identity())
m_shipDir(0.0),
m_shipYaw(0.0),
m_dir(0.0),
m_yaw(0.0),
m_rotInterp(0.f),
m_transInterp(0.f),
m_gForce(0.f),
m_offset(0.f),
m_shipVel(0.f),
m_translate(0.0),
m_transform(matrix4x4d::Identity())
{
assert(!modelName.empty());
SetModel(modelName.c_str());
Expand All @@ -38,14 +38,14 @@ ShipCockpit::~ShipCockpit()
void ShipCockpit::Render(Graphics::Renderer *renderer, const Camera *camera, const vector3d &viewCoords, const matrix4x4d &viewTransform)
{
PROFILE_SCOPED()
RenderModel(renderer, camera, viewCoords, viewTransform);
RenderModel(renderer, camera, viewCoords, viewTransform);
}

inline void ShipCockpit::resetInternalCameraController() {
m_icc = static_cast<InternalCameraController*>(Pi::game->GetWorldView()->GetCameraController());
}

void ShipCockpit::Update(float timeStep)
void ShipCockpit::Update(const Player *player, float timeStep)
{
m_transform = matrix4x4d::Identity();

Expand All @@ -60,14 +60,14 @@ void ShipCockpit::Update(float timeStep)
m_transform.RotateX(rotX);
m_transform.RotateY(rotY);

vector3d cur_dir = Pi::player->GetOrient().VectorZ().Normalized();
vector3d cur_dir = player->GetOrient().VectorZ().Normalized();
if (cur_dir.Dot(m_shipDir) < 1.0f) {
m_rotInterp = 0.0f;
m_shipDir = cur_dir;
}

//---------------------------------------- Acceleration
float cur_vel = CalculateSignedForwardVelocity(-cur_dir, Pi::player->GetVelocity()); // Forward is -Z
float cur_vel = CalculateSignedForwardVelocity(-cur_dir, player->GetVelocity()); // Forward is -Z
float gforce = Clamp(floorf(((fabs(cur_vel) - m_shipVel) / timeStep) / 9.8f), -COCKPIT_MAX_GFORCE, COCKPIT_MAX_GFORCE);
if (fabs(cur_vel) > 500000.0f || // Limit gforce measurement so we don't get astronomical fluctuations
fabs(gforce - m_gForce) > 100.0) { // Smooth out gforce one frame spikes, sometimes happens when hitting max speed due to the thrust limiters
Expand All @@ -93,8 +93,8 @@ void ShipCockpit::Update(float timeStep)
//------------------------------------------ Rotation
// For yaw/pitch
vector3d rot_axis = cur_dir.Cross(m_dir).Normalized();
vector3d yaw_axis = Pi::player->GetOrient().VectorY().Normalized();
vector3d pitch_axis = Pi::player->GetOrient().VectorX().Normalized();
vector3d yaw_axis = player->GetOrient().VectorY().Normalized();
vector3d pitch_axis = player->GetOrient().VectorX().Normalized();
float dot = cur_dir.Dot(m_dir);
float angle = acos(dot);
// For roll
Expand Down Expand Up @@ -144,8 +144,7 @@ void ShipCockpit::Update(float timeStep)
}
m_transform.RotateY(-yaw_angle);
}
}
else {
} else {
angle = 0.0f;
}
}
Expand All @@ -172,34 +171,32 @@ void ShipCockpit::Update(float timeStep)
}
m_transform.RotateZ(-roll_angle);
}
}
else {
} else {
angle_yaw = 0.0f;
}
}
}
else {
} else {
m_rotInterp = 0.0f;
}
}

void ShipCockpit::RenderCockpit(Graphics::Renderer* renderer, const Camera* camera, Frame* frame)
{
PROFILE_SCOPED()
renderer->ClearDepthBuffer();
renderer->ClearDepthBuffer();
SetFrame(frame);
Render(renderer, camera, m_translate, m_transform);
SetFrame(nullptr);
}

void ShipCockpit::OnActivated()
void ShipCockpit::OnActivated(const Player *player)
{
assert(Pi::player);
m_dir = Pi::player->GetOrient().VectorZ().Normalized();
m_yaw = Pi::player->GetOrient().VectorY().Normalized();
assert(player);
m_dir = player->GetOrient().VectorZ().Normalized();
m_yaw = player->GetOrient().VectorY().Normalized();
m_shipDir = m_dir;
m_shipYaw = m_yaw;
m_shipVel = CalculateSignedForwardVelocity(-m_shipDir, Pi::player->GetVelocity());
m_shipVel = CalculateSignedForwardVelocity(-m_shipDir, player->GetVelocity());
}

float ShipCockpit::CalculateSignedForwardVelocity(const vector3d &normalized_forward, const vector3d &velocity) {
Expand Down
10 changes: 6 additions & 4 deletions src/ShipCockpit.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ static const float COCKPIT_ACCEL_INTERP_MULTIPLIER = 0.5f;
static const float COCKPIT_MAX_GFORCE = 10000.0f;
static const float COCKPIT_ACCEL_OFFSET = 0.075f;

class Player;

class ShipCockpit : public ModelBody
{
public:
Expand All @@ -24,10 +26,10 @@ class ShipCockpit : public ModelBody

virtual void Render(Graphics::Renderer *r, const Camera *camera, const vector3d &viewCoords, const matrix4x4d &viewTransform) override;

void Update(float timeStep);
void Update(const Player *player, float timeStep);
void RenderCockpit(Graphics::Renderer* renderer, const Camera* camera, Frame* frame);
void OnActivated();
void resetInternalCameraController(void);
void OnActivated(const Player *player);
void resetInternalCameraController(void);

protected:
float CalculateSignedForwardVelocity(const vector3d &forward, const vector3d &velocity);
Expand All @@ -47,6 +49,6 @@ class ShipCockpit : public ModelBody
float m_shipVel; // current ship velocity
vector3d m_translate; // cockpit translation
matrix4x4d m_transform; // cockpit transformation
InternalCameraController* m_icc;
InternalCameraController* m_icc;
};
#endif

0 comments on commit c90197b

Please sign in to comment.