Skip to content

Commit

Permalink
use CCamera::SetRot() in the CameraControllers
Browse files Browse the repository at this point in the history
  • Loading branch information
jK committed Dec 5, 2014
1 parent 75a6e49 commit 4432b01
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 121 deletions.
6 changes: 3 additions & 3 deletions rts/Game/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ void CCamera::UpdateDirFromRot()
forward.z = math::sin(rot.x) * math::sin(rot.y - fastmath::HALFPI);
forward.y = math::cos(rot.x);

right.x = math::sin(rot.z + fastmath::HALFPI) * math::cos(rot.y);
right.z = math::sin(rot.z + fastmath::HALFPI) * math::sin(rot.y);
right.y = math::cos(rot.z + fastmath::HALFPI);
right.x = math::sin(fastmath::HALFPI - rot.z) * math::cos(rot.y);
right.z = math::sin(fastmath::HALFPI - rot.z) * math::sin(rot.y);
right.y = math::cos(fastmath::HALFPI - rot.z);

up = right.cross(forward);
}
Expand Down
24 changes: 10 additions & 14 deletions rts/Game/Camera/FPSController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,23 @@ CFPSController::CFPSController()
mouseScale = configHandler->GetFloat("FPSMouseScale");
enabled = configHandler->GetBool("FPSEnabled");
fov = configHandler->GetFloat("FPSFOV");
UpdateVectors();
Update();
}


void CFPSController::KeyMove(float3 move)
{
move *= move.z * 400;
pos += (camera->GetDir() * move.y + camera->GetRight() * move.x) * scrollSpeed;
UpdateVectors();
Update();
}


void CFPSController::MouseMove(float3 move)
{
camera->rot.y -= mouseScale * move.x;
camera->rot.x -= mouseScale * move.y * move.z;
camera->rot.x = Clamp(camera->rot.x, -PI*0.4999f, PI*0.4999f);
UpdateVectors();
camera->SetRotY(camera->GetRot().y + mouseScale * move.x);
camera->SetRotX(Clamp(camera->GetRot().x + mouseScale * move.y * move.z, 0.01f, PI * 0.99f));
Update();
}


Expand All @@ -56,11 +55,11 @@ void CFPSController::ScreenEdgeMove(float3 move)
void CFPSController::MouseWheelMove(float move)
{
pos += (camera->GetUp() * move);
UpdateVectors();
Update();
}


void CFPSController::UpdateVectors()
void CFPSController::Update()
{
if (!gu->fpsMode) {
const float margin = 0.01f;
Expand All @@ -79,10 +78,7 @@ void CFPSController::UpdateVectors()
oldHeight = pos.y - gndHeight;
}

dir.x = (float)(math::cos(camera->rot.x) * math::sin(camera->rot.y));
dir.z = (float)(math::cos(camera->rot.x) * math::cos(camera->rot.y));
dir.y = (float)(math::sin(camera->rot.x));
dir.ANormalize();
dir = camera->GetDir();
}


Expand All @@ -93,14 +89,14 @@ void CFPSController::SetPos(const float3& newPos)
if (!gu->fpsMode) {
pos.y = CGround::GetHeightAboveWater(pos.x, pos.z, false) + oldHeight;
}
UpdateVectors();
Update();
}


void CFPSController::SetDir(const float3& newDir)
{
dir = newDir;
UpdateVectors();
Update();
}


Expand Down
3 changes: 1 addition & 2 deletions rts/Game/Camera/FPSController.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class CFPSController : public CCameraController
void GetState(StateMap& sm) const;
bool SetState(const StateMap& sm);

private:
void UpdateVectors();
void Update();

private:
float mouseScale;
Expand Down
57 changes: 26 additions & 31 deletions rts/Game/Camera/FreeController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ CFreeController::CFreeController()

if (camera) {
const float hDist = math::sqrt((dir.x * dir.x) + (dir.z * dir.z));
camera->rot.y = math::atan2(dir.x, dir.z); // yaw
camera->rot.x = math::atan2(dir.y, hDist); // pitch
camera->SetRotY(math::atan2(dir.x, dir.z)); // yaw
camera->SetRotX(math::atan2(dir.y, hDist)); // pitch
}
pos -= (dir * 1000.0f);

Expand Down Expand Up @@ -85,15 +85,15 @@ void CFreeController::SetTrackingInfo(const float3& target, float radius)
const float rads = math::atan2(diff.x, diff.z);
const float len2D = diff.Length2D();

camera->rot.y = rads;
camera->SetRotY(rads);

if (math::fabs(len2D) <= 0.001f) {
camera->rot.x = 0.0f;
camera->SetRotX(0.0f);
} else {
camera->rot.x = math::atan2((trackPos.y - pos.y), len2D);
camera->SetRotX(math::atan2((trackPos.y - pos.y), len2D));
}

camera->UpdateForward(GetDir());
camera->SetDir(GetDir());
}


Expand Down Expand Up @@ -127,14 +127,11 @@ void CFreeController::Update()
if (gndLock && (autoTilt > 0.0f)) {
const float gndHeight = CGround::GetHeightReal(pos.x, pos.z, false);
if (pos.y < (gndHeight + gndOffset + 1.0f)) {
float3 hDir;
hDir.y = 0.0f;
hDir.x = (float)math::sin(camera->rot.y);
hDir.z = (float)math::cos(camera->rot.y);
const float3 hDir(math::sin(camera->GetRot().y), 0.f, math::cos(camera->GetRot().y));
const float3 gndNormal = CGround::GetSmoothNormal(pos.x, pos.z, false);
const float dot = gndNormal.dot(hDir);
const float gndRotX = (float)math::acos(dot) - (PI * 0.5f);
const float rotXdiff = (gndRotX - camera->rot.x);
const float rotXdiff = (gndRotX - camera->GetRot().x);
autoTiltVel = (autoTilt * rotXdiff);
}
}
Expand Down Expand Up @@ -178,9 +175,9 @@ void CFreeController::Update()

// set the new position/rotation
if (!tracking) {
pos += (vel * ft);
camera->rot += (avel * ft);
camera->rot.x += (autoTiltVel * ft); // note that this is not smoothed
pos += (vel * ft);
camera->SetRot( camera->GetRot() + (avel * ft));
camera->SetRotX(camera->GetRot().x + (autoTiltVel * ft)); // note that this is not smoothed
} else {
// speed along the tracking direction varies with distance
const float3 diff = (pos - trackPos);
Expand Down Expand Up @@ -251,14 +248,15 @@ void CFreeController::Update()
// angular clamps
const float xRotLimit = PI * 0.499f;

if (camera->rot.x >= xRotLimit) {
if (camera->GetRot().x >= xRotLimit) {
// maximum upward pitch
camera->rot.x = xRotLimit; avel.x = 0.0f;
} else if (camera->rot.x <= -xRotLimit) {
camera->SetRotX(xRotLimit);
avel.x = 0.0f;
} else if (camera->GetRot().x <= -xRotLimit) {
// maximum downward pitch
camera->rot.x = -xRotLimit; avel.x = 0.0f;
camera->SetRotX(-xRotLimit);
avel.x = 0.0f;
}
camera->rot.y = math::fmod(camera->rot.y, PI * 2.0f);

// setup for the next loop
prevVel = vel;
Expand All @@ -271,12 +269,7 @@ void CFreeController::Update()

float3 CFreeController::GetDir() const
{
float3 dir;
dir.x = (float)(math::sin(camera->rot.y) * math::cos(camera->rot.x));
dir.z = (float)(math::cos(camera->rot.y) * math::cos(camera->rot.x));
dir.y = (float)(math::sin(camera->rot.x));
dir.ANormalize();
return dir;
return camera->GetDir();
}


Expand Down Expand Up @@ -422,9 +415,9 @@ void CFreeController::GetState(StateMap& sm) const
sm["invertAlt"] = invertAlt ? +1.0f : -1.0f;
sm["gndLock"] = gndLock ? +1.0f : -1.0f;

sm["rx"] = camera->rot.x;
sm["ry"] = camera->rot.y;
sm["rz"] = camera->rot.z;
sm["rx"] = camera->GetRot().x;
sm["ry"] = camera->GetRot().y;
sm["rz"] = camera->GetRot().z;

sm["vx"] = prevVel.x;
sm["vy"] = prevVel.y;
Expand Down Expand Up @@ -454,9 +447,11 @@ bool CFreeController::SetState(const StateMap& sm)
SetStateBool (sm, "invertAlt", invertAlt);
SetStateBool (sm, "gndLock", gndLock);

SetStateFloat(sm, "rx", camera->rot.x);
SetStateFloat(sm, "ry", camera->rot.y);
SetStateFloat(sm, "rz", camera->rot.z);
float3 rot;
SetStateFloat(sm, "rx", rot.x);
SetStateFloat(sm, "ry", rot.y);
SetStateFloat(sm, "rz", rot.z);
camera->SetRot(rot);

SetStateFloat(sm, "vx", prevVel.x);
SetStateFloat(sm, "vy", prevVel.y);
Expand Down
5 changes: 2 additions & 3 deletions rts/Game/Camera/OrbitController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ void COrbitController::Orbit()
{
camera->SetPos(cen + GetOrbitPos());
camera->SetPos((camera->GetPos() + XZVector) + (UpVector * std::max(camera->GetPos().y, CGround::GetHeightReal(camera->GetPos().x, camera->GetPos().z, false))));
camera->forward = (cen - camera->GetPos()).ANormalize();
camera->up = UpVector;
camera->SetDir((cen - camera->GetPos()).ANormalize());
}

void COrbitController::Pan(int rdx, int rdy)
Expand All @@ -184,7 +183,7 @@ void COrbitController::Pan(int rdx, int rdy)

if (cen.y < cenGH) {
cen.y = cenGH;
camera->forward = (cen - camera->GetPos()).ANormalize();
camera->SetDir((cen - camera->GetPos()).ANormalize());

Init(camera->GetPos(), cen);
}
Expand Down
19 changes: 7 additions & 12 deletions rts/Game/Camera/OverheadController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ COverheadController::COverheadController()
}

maxHeight = 9.5f * std::max(gs->mapx, gs->mapy);
UpdateVectors();
Update();
}

void COverheadController::KeyMove(float3 move)
Expand All @@ -59,7 +59,7 @@ void COverheadController::KeyMove(float3 move)

pos.x += move.x * pixelSize * 2.0f * scrollSpeed;
pos.z -= move.y * pixelSize * 2.0f * scrollSpeed;
UpdateVectors();
Update();
}

void COverheadController::MouseMove(float3 move)
Expand All @@ -72,7 +72,7 @@ void COverheadController::MouseMove(float3 move)

pos.x += move.x * pixelSize * (1 + KeyInput::GetKeyModState(KMOD_SHIFT) * 3) * scrollSpeed;
pos.z += move.y * pixelSize * (1 + KeyInput::GetKeyModState(KMOD_SHIFT) * 3) * scrollSpeed;
UpdateVectors();
Update();
}

void COverheadController::ScreenEdgeMove(float3 move)
Expand Down Expand Up @@ -150,10 +150,10 @@ void COverheadController::MouseWheelMove(float move)
}
}

UpdateVectors();
Update();
}

void COverheadController::UpdateVectors()
void COverheadController::Update()
{
pos.x = Clamp(pos.x, 0.01f, gs->mapx * SQUARE_SIZE - 0.01f);
pos.z = Clamp(pos.z, 0.01f, gs->mapy * SQUARE_SIZE - 0.01f);
Expand All @@ -163,10 +163,6 @@ void COverheadController::UpdateVectors()
float alpha = std::roundf(angle / angleStep) * angleStep;
alpha = Clamp(alpha, 0.01f, fastmath::HALFPI);
dir = float3(0.0f, -fastmath::cos(alpha), flipped ? fastmath::sin(alpha) : -fastmath::sin(alpha));
}

void COverheadController::Update()
{
pixelSize = (camera->GetTanHalfFov() * 2.0f) / globalRendering->viewSizeY * height * 2.0f;
}

Expand All @@ -179,7 +175,7 @@ float3 COverheadController::GetPos() const
void COverheadController::SetPos(const float3& newPos)
{
pos = newPos;
UpdateVectors();
Update();
}

float3 COverheadController::SwitchFrom() const
Expand All @@ -193,7 +189,7 @@ void COverheadController::SwitchTo(bool showText)
LOG("Switching to Overhead (TA) style camera");
}
angle = DEFAULT_ANGLE;
UpdateVectors();
Update();
}

void COverheadController::GetState(StateMap& sm) const
Expand All @@ -212,7 +208,6 @@ bool COverheadController::SetState(const StateMap& sm)
SetStateFloat(sm, "height", height);
SetStateFloat(sm, "angle", angle);
SetStateBool (sm, "flipped", flipped);
UpdateVectors();

return true;
}
3 changes: 0 additions & 3 deletions rts/Game/Camera/OverheadController.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ class COverheadController : public CCameraController

bool flipped;

private:
void UpdateVectors();

private:
float middleClickScrollSpeed;
float height;
Expand Down
24 changes: 10 additions & 14 deletions rts/Game/Camera/RotOverheadController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CRotOverheadController::CRotOverheadController()
scrollSpeed = configHandler->GetInt("RotOverheadScrollSpeed") * 0.1f;
enabled = configHandler->GetBool("RotOverheadEnabled");
fov = configHandler->GetFloat("RotOverheadFOV");
UpdateVectors();
Update();
}


Expand All @@ -37,16 +37,17 @@ void CRotOverheadController::KeyMove(float3 move)
flatForward.ANormalize();

pos += (flatForward * move.y + camera->GetRight() * move.x) * scrollSpeed;
UpdateVectors();
Update();
}


void CRotOverheadController::MouseMove(float3 move)
{
camera->rot.y -= mouseScale * move.x;
camera->rot.x -= mouseScale * move.y * move.z;
camera->rot.x = Clamp(camera->rot.x, -PI*0.4999f, PI*0.4999f);
UpdateVectors();
camera->SetRotY(camera->GetRot().y + mouseScale * move.x);
camera->SetRotX(camera->GetRot().x + mouseScale * move.y * move.z);
camera->SetRotX(Clamp(camera->GetRot().x, PI*0.4999f, PI));
dir = camera->GetDir();
Update();
}


Expand All @@ -64,16 +65,11 @@ void CRotOverheadController::MouseWheelMove(float move)
height *= (1.0f + (move * mouseScale));
pos.y = height + gheight;

UpdateVectors();
Update();
}

void CRotOverheadController::UpdateVectors()
void CRotOverheadController::Update()
{
dir.x=(float)(math::sin(camera->rot.y) * math::cos(camera->rot.x));
dir.y=(float)(math::sin(camera->rot.x));
dir.z=(float)(math::cos(camera->rot.y) * math::cos(camera->rot.x));
dir.ANormalize();

pos.x = Clamp(pos.x, 0.01f, gs->mapx * SQUARE_SIZE - 0.01f);
pos.z = Clamp(pos.z, 0.01f, gs->mapy * SQUARE_SIZE - 0.01f);

Expand All @@ -86,7 +82,7 @@ void CRotOverheadController::SetPos(const float3& newPos)
{
CCameraController::SetPos(newPos);
pos.y = CGround::GetHeightAboveWater(pos.x, pos.z, false) + oldHeight;
UpdateVectors();
Update();
}


Expand Down
3 changes: 1 addition & 2 deletions rts/Game/Camera/RotOverheadController.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class CRotOverheadController : public CCameraController
void GetState(StateMap& sm) const;
bool SetState(const StateMap& sm);

private:
void UpdateVectors();
void Update();

private:
float mouseScale;
Expand Down
Loading

0 comments on commit 4432b01

Please sign in to comment.