Skip to content

Commit 3e0a5be

Browse files
authored
refactor(view): Simplify View angle normalization (TheSuperHackers#2271)
1 parent fa9345a commit 3e0a5be

3 files changed

Lines changed: 27 additions & 34 deletions

File tree

Core/GameEngine/Include/GameClient/View.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,16 @@ class View : public Snapshot
261261
Int m_width, m_height; ///< Dimensions of the view
262262
Int m_originX, m_originY; ///< Location of top/left view corner
263263

264-
Real m_angle; ///< Angle at which view has been rotated about the Z axis
265-
Real m_pitch; ///< Rotation of view direction around horizontal (X) axis
264+
Real m_angle; ///< Angle at which view has been rotated about the Z axis. Expected normalized
265+
Real m_pitch; ///< Rotation of view direction around horizontal (X) axis. Expected normalized
266266

267267
Real m_maxHeightAboveGround; ///< Highest camera above ground value
268268
Real m_minHeightAboveGround; ///< Lowest camera above ground value
269269
Real m_zoom; ///< Current zoom value
270270
Real m_heightAboveGround; ///< User's desired camera height above ground
271271
Bool m_zoomLimited; ///< Camera restricted in zoom height
272-
Real m_defaultAngle;
273-
Real m_defaultPitch;
272+
Real m_defaultAngle; ///< Expected normalized
273+
Real m_defaultPitch; ///< Expected normalized
274274
Real m_currentHeightAboveGround; ///< Actual camera height above ground, or rather height above ground at default pitch
275275
Real m_terrainHeightAtPivot; ///< Actual terrain height at camera pivot
276276

Core/GameEngine/Source/GameClient/View.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void View::scrollBy( Coord2D *delta )
159159
*/
160160
void View::setAngle( Real radians )
161161
{
162-
m_angle = radians;
162+
m_angle = WWMath::Normalize_Angle(radians);
163163
}
164164

165165
/**
@@ -174,7 +174,7 @@ void View::setPitch( Real radians )
174174
/**
175175
* Set the view angle back to default
176176
*/
177-
void View::setAngleToDefault()
177+
void View::setAngleToDefault( void )
178178
{
179179
m_angle = m_defaultAngle;
180180
}

Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@
9898

9999
#include "W3DDevice/GameClient/CameraShakeSystem.h"
100100

101-
constexpr const Real NearZ = MAP_XY_FACTOR; ///< Set the near to MAP_XY_FACTOR. Improves z buffer resolution.
102-
103101
// 30 fps
104102
Real TheW3DFrameLengthInMsec = MSEC_PER_LOGICFRAME_REAL; // default is 33msec/frame == 30fps. but we may change it depending on sys config.
105103
static const Int MAX_REQUEST_CACHE_SIZE = 40; // Any size larger than 10, or examine code below for changes. jkmcd.
106104
static const Real DRAWABLE_OVERSCAN = 75.0f; ///< 3D world coords of how much to overscan in the 3D screen region
107105

106+
constexpr const Real NearZ = MAP_XY_FACTOR; ///< Set the near to MAP_XY_FACTOR. Improves z buffer resolution.
107+
108108
//=================================================================================================
109109
inline Real minf(Real a, Real b) { if (a < b) return a; else return b; }
110110
inline Real maxf(Real a, Real b) { if (a > b) return a; else return b; }
@@ -1234,22 +1234,21 @@ void W3DView::update(void)
12341234
if (cameraLockObj->isUsingAirborneLocomotor() && cameraLockObj->isAboveTerrainOrWater())
12351235
{
12361236
Matrix3D camXForm;
1237-
Real oldZRot = m_angle;
12381237
Real idealZRot = cameraLockObj->getOrientation() - M_PI_2;
1239-
normAngle(oldZRot);
1240-
normAngle(idealZRot);
1241-
Real diff = idealZRot - oldZRot;
1242-
normAngle(diff);
12431238

12441239
if (m_snapImmediate)
12451240
{
1246-
m_angle = idealZRot;
1241+
View::setAngle(idealZRot);
12471242
}
12481243
else
12491244
{
1250-
m_angle += diff * 0.1f;
1245+
normAngle(idealZRot);
1246+
Real oldZRot = m_angle;
1247+
normAngle(oldZRot);
1248+
Real diffRot = idealZRot - oldZRot;
1249+
normAngle(diffRot);
1250+
View::setAngle(m_angle + diffRot * 0.1f);
12511251
}
1252-
normAngle(m_angle);
12531252
}
12541253
}
12551254
if (m_snapImmediate)
@@ -1816,12 +1815,9 @@ void W3DView::forceRedraw()
18161815
//-------------------------------------------------------------------------------------------------
18171816
/** Rotate the view around the up axis to the given angle. */
18181817
//-------------------------------------------------------------------------------------------------
1819-
void W3DView::setAngle( Real angle )
1818+
void W3DView::setAngle( Real radians )
18201819
{
1821-
// Normalize to +-PI.
1822-
normAngle(angle);
1823-
// call our base class, we are adding functionality
1824-
View::setAngle( angle );
1820+
View::setAngle( radians );
18251821

18261822
m_doingMoveCameraOnWaypointPath = false;
18271823
m_CameraArrivedAtWaypointOnPathFlag = false;
@@ -1837,10 +1833,9 @@ void W3DView::setAngle( Real angle )
18371833
//-------------------------------------------------------------------------------------------------
18381834
/** Rotate the view around the horizontal (X) axis to the given angle. */
18391835
//-------------------------------------------------------------------------------------------------
1840-
void W3DView::setPitch( Real angle )
1836+
void W3DView::setPitch( Real radians )
18411837
{
1842-
// call our base class, we are extending functionality
1843-
View::setPitch( angle );
1838+
View::setPitch( radians );
18441839

18451840
m_doingMoveCameraOnWaypointPath = false;
18461841
m_doingRotateCamera = false;
@@ -2730,7 +2725,7 @@ void W3DView::resetCamera(const Coord3D *location, Int milliseconds, Real easeIn
27302725
moveCameraTo(location, milliseconds, 0, false, easeIn, easeOut);
27312726
m_mcwpInfo.cameraAngle[2] = 0.0f; // default angle.
27322727
// m_mcwpInfo.cameraAngle[2] = m_defaultAngle;
2733-
m_angle = m_mcwpInfo.cameraAngle[0];
2728+
View::setAngle(m_mcwpInfo.cameraAngle[0]);
27342729

27352730
// terrain height + desired height offset == cameraOffset * actual zoom
27362731
// find best approximation of max terrain height we can see
@@ -2944,22 +2939,21 @@ void W3DView::rotateCameraOneFrame(void)
29442939
Real angleDiff = angle - m_angle;
29452940
normAngle(angleDiff);
29462941
angleDiff *= factor;
2947-
m_angle += angleDiff;
2948-
normAngle(m_angle);
2942+
View::setAngle(m_angle + angleDiff);
29492943
m_timeMultiplier = m_rcInfo.startTimeMultiplier + REAL_TO_INT_FLOOR(0.5 + (m_rcInfo.endTimeMultiplier-m_rcInfo.startTimeMultiplier)*factor);
29502944
}
29512945
else
29522946
{
2953-
m_angle = angle;
2947+
View::setAngle(angle);
29542948
}
29552949
}
29562950
}
29572951
}
29582952
else if (m_rcInfo.curFrame <= m_rcInfo.numFrames)
29592953
{
29602954
Real factor = m_rcInfo.ease(((Real)m_rcInfo.curFrame)/m_rcInfo.numFrames);
2961-
m_angle = WWMath::Lerp(m_rcInfo.angle.startAngle, m_rcInfo.angle.endAngle, factor);
2962-
normAngle(m_angle);
2955+
Real angle = WWMath::Lerp(m_rcInfo.angle.startAngle, m_rcInfo.angle.endAngle, factor);
2956+
View::setAngle(angle);
29632957
m_timeMultiplier = m_rcInfo.startTimeMultiplier + REAL_TO_INT_FLOOR(0.5 + (m_rcInfo.endTimeMultiplier-m_rcInfo.startTimeMultiplier)*factor);
29642958
}
29652959

@@ -2969,7 +2963,7 @@ void W3DView::rotateCameraOneFrame(void)
29692963
m_freezeTimeForCameraMovement = false;
29702964
if (! m_rcInfo.trackObject)
29712965
{
2972-
m_angle = m_rcInfo.angle.endAngle;
2966+
View::setAngle(m_rcInfo.angle.endAngle);
29732967
}
29742968
}
29752969
}
@@ -3041,7 +3035,7 @@ void W3DView::moveAlongWaypointPath(Real milliseconds)
30413035
m_CameraArrivedAtWaypointOnPathFlag = false;
30423036

30433037
m_freezeTimeForCameraMovement = false;
3044-
m_angle = m_mcwpInfo.cameraAngle[m_mcwpInfo.numWaypoints];
3038+
View::setAngle(m_mcwpInfo.cameraAngle[m_mcwpInfo.numWaypoints]);
30453039

30463040
m_groundLevel = m_mcwpInfo.groundHeight[m_mcwpInfo.numWaypoints];
30473041
/////////////////////m_cameraOffset.z = m_groundLevel+TheGlobalData->m_cameraHeight;
@@ -3112,8 +3106,7 @@ void W3DView::moveAlongWaypointPath(Real milliseconds)
31123106
if (fabs(deltaAngle) > PI/10) {
31133107
DEBUG_LOG(("Huh."));
31143108
}
3115-
m_angle += avgFactor*(deltaAngle);
3116-
normAngle(m_angle);
3109+
View::setAngle(m_angle + (avgFactor*deltaAngle));
31173110

31183111
Real timeMultiplier = m_mcwpInfo.timeMultiplier[m_mcwpInfo.curSegment]*factor1 +
31193112
m_mcwpInfo.timeMultiplier[m_mcwpInfo.curSegment+1]*factor2;

0 commit comments

Comments
 (0)