Skip to content

Commit f3afe58

Browse files
committed
Merge branch 'refactore_physics'
2 parents dacb185 + d43aeff commit f3afe58

32 files changed

Lines changed: 930 additions & 699 deletions

src/Box2D_Helpers.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#pragma once
2+
3+
namespace box2d
4+
{
5+
// simple wrapper for seamlessly cast between math libraries
6+
struct vec2: public b2Vec2
7+
{
8+
public:
9+
vec2() = default;
10+
vec2(float xIn, float yIn): b2Vec2(xIn, yIn)
11+
{
12+
}
13+
template<typename TVec2>
14+
vec2(const TVec2& in_vec2): b2Vec2(in_vec2.x, in_vec2.y)
15+
{
16+
}
17+
template<typename TVec2>
18+
inline vec2& operator = (const TVec2& in_vec2)
19+
{
20+
x = in_vec2.x;
21+
y = in_vec2.y;
22+
return *this;
23+
}
24+
inline operator glm::vec2 () const
25+
{
26+
return {x, y};
27+
}
28+
inline vec2 operator * (float scalar) const
29+
{
30+
return {x * scalar, y * scalar};
31+
}
32+
};
33+
34+
35+
static const vec2 NullVector { 0.0f, 0.0f };
36+
static const vec2 ForwardVector (1.0f, 0.0f);
37+
static const vec2 LateralVector (0.0f, 1.0f);
38+
39+
} // namespace box2d

src/Carnage3D.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
</ItemDefinitionGroup>
155155
<ItemGroup>
156156
<ClInclude Include="AiCharacterController.h" />
157+
<ClInclude Include="Box2D_Helpers.h" />
157158
<ClInclude Include="common_utils.h" />
158159
<ClInclude Include="Convert.h" />
159160
<ClInclude Include="game_version.h" />

src/Carnage3D.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@
329329
<ClInclude Include="math_utils.h">
330330
<Filter>Lib</Filter>
331331
</ClInclude>
332+
<ClInclude Include="Box2D_Helpers.h">
333+
<Filter>Game\Physics</Filter>
334+
</ClInclude>
332335
</ItemGroup>
333336
<ItemGroup>
334337
<ClCompile Include="stdafx.cpp">

src/CarnageGame.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ bool CarnageGame::Initialize()
2323

2424
SetInputActionsFromConfig();
2525

26-
gGameParams.LoadDefaults();
26+
gGameParams.SetToDefaults();
2727

2828
if (gSystem.mStartupParams.mDebugMapName.empty())
2929
{
@@ -330,22 +330,16 @@ bool CarnageGame::StartScenario(const std::string& mapName)
330330
{
331331
for (int xBlock = 10; xBlock < 20 && currFindPosIter < mNumPlayers; ++xBlock)
332332
{
333-
pos[currFindPosIter] = glm::ivec3(xBlock, MAP_LAYERS_COUNT - 1, yBlock);
334-
335-
float currHeight = gGameMap.GetHeightAtPosition(pos[currFindPosIter]);
336-
int zBlock = static_cast<int>(currHeight);
337-
if (zBlock > MAP_LAYERS_COUNT - 1)
338-
continue;
339-
340-
BlockStyle* currBlock = gGameMap.GetBlock(xBlock, yBlock, zBlock);
341-
if (currBlock->mGroundType == eGroundType_Field ||
342-
currBlock->mGroundType == eGroundType_Pawement ||
343-
currBlock->mGroundType == eGroundType_Road)
333+
for (int zBlock = MAP_LAYERS_COUNT - 1; zBlock > -1; --zBlock)
344334
{
345-
pos[currFindPosIter].x += MAP_BLOCK_LENGTH * 0.5f;
346-
pos[currFindPosIter].z += MAP_BLOCK_LENGTH * 0.5f;
347-
pos[currFindPosIter].y = currHeight;
348-
++currFindPosIter;
335+
BlockStyle* currBlock = gGameMap.GetBlock(xBlock, yBlock, zBlock);
336+
if (currBlock->mGroundType == eGroundType_Field ||
337+
currBlock->mGroundType == eGroundType_Pawement ||
338+
currBlock->mGroundType == eGroundType_Road)
339+
{
340+
pos[currFindPosIter++] = Convert::MapUnitsToMeters(glm::ivec3(xBlock, zBlock, yBlock));
341+
break;
342+
}
349343
}
350344
}
351345
}

src/Convert.h

Lines changed: 125 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,137 @@
33
#include "GameDefs.h"
44
#include "PhysicsDefs.h"
55

6-
// convert fix16 angle to degrees
7-
inline float ConvertFixAngleToDegs(unsigned short ang16)
6+
class Convert
87
{
9-
return (ang16 / 1024.0f) * 360.0f - SPRITE_ZERO_ANGLE;
10-
}
8+
public:
9+
// Convert map units to meters
10+
static float MapUnitsToMeters(float units) { return (units * METERS_PER_MAP_UNIT); }
1111

12-
// convert pixels coordinate to map coordinate
13-
inline float ConvertPixelsToMap(int pixels)
14-
{
15-
return (1.0f * pixels) / MAP_PIXELS_PER_TILE;
16-
}
12+
// Convert meters to map units
13+
static float MetersToMapUnits(float units) { return (units / METERS_PER_MAP_UNIT); }
1714

18-
// convert map coordinate to pixels
19-
inline int ConvertMapToPixels(float coord)
20-
{
21-
return static_cast<int>(coord * MAP_PIXELS_PER_TILE);
22-
}
15+
// Convert map units to meters
16+
static glm::vec2 MapUnitsToMeters(const glm::vec2& units)
17+
{
18+
return {
19+
MapUnitsToMeters(units.x),
20+
MapUnitsToMeters(units.y)
21+
};
22+
}
23+
static glm::vec3 MapUnitsToMeters(const glm::vec3& units)
24+
{
25+
return {
26+
MapUnitsToMeters(units.x),
27+
MapUnitsToMeters(units.y),
28+
MapUnitsToMeters(units.z)
29+
};
30+
}
31+
// Convert meters to map units
32+
static glm::vec2 MetersToMapUnits(const glm::vec2& units)
33+
{
34+
return {
35+
MetersToMapUnits(units.x),
36+
MetersToMapUnits(units.y)
37+
};
38+
}
39+
static glm::vec3 MetersToMapUnits(const glm::vec3& units)
40+
{
41+
return {
42+
MetersToMapUnits(units.x),
43+
MetersToMapUnits(units.y),
44+
MetersToMapUnits(units.z)
45+
};
46+
}
2347

24-
// convert map coordinate to physics world coordinate
25-
inline float ConvertMapToPhysics(float coord)
26-
{
27-
return coord * PHYSICS_SCALE;
28-
}
48+
// Convert fix16 angle
49+
static cxx::angle_t Fix16ToAngle(unsigned short ang16)
50+
{
51+
return cxx::angle_t::from_degrees((ang16 / 1024.0f) * 360.0f - SPRITE_ZERO_ANGLE);
52+
}
2953

30-
// convert physics world coordinate to map coordinate
31-
inline float ConvertPhysicsToMap(float coord)
32-
{
33-
return coord / PHYSICS_SCALE;
34-
}
54+
// Convert pixels to map units
55+
static float PixelsToMapUnits(int pixels) { return (1.0f * pixels) / PIXELS_PER_MAP_UNIT; }
3556

36-
// convert pixels coordinate to block coordinate
37-
inline int ConvertPixelsToTilePos(int pixels)
38-
{
39-
return pixels / MAP_PIXELS_PER_TILE;
40-
}
57+
// Convert map units to pixels
58+
static int MapUnitsToPixels(float units) { return (int) (units * PIXELS_PER_MAP_UNIT); }
4159

42-
// convert block coordinate to pixels coorinate
43-
inline int ConvertTilePosToPixels(int tilePos)
44-
{
45-
return tilePos * MAP_PIXELS_PER_TILE;
46-
}
60+
// Convert pixels to map units
61+
static glm::vec2 PixelsToMapUnits(const glm::ivec2& pixels)
62+
{
63+
return {
64+
PixelsToMapUnits(pixels.x),
65+
PixelsToMapUnits(pixels.y)
66+
};
67+
}
68+
static glm::vec3 PixelsToMapUnits(const glm::ivec3& pixels)
69+
{
70+
return {
71+
PixelsToMapUnits(pixels.x),
72+
PixelsToMapUnits(pixels.y),
73+
PixelsToMapUnits(pixels.z)
74+
};
75+
}
4776

48-
// in original gta1 map height levels is counting from top to bottom -
49-
// 0 is highest and 5 is lowest level
50-
inline int ConvertMapLevel(int tileLayer)
51-
{
52-
tileLayer = MAP_LAYERS_COUNT - tileLayer - 1;
53-
debug_assert(tileLayer > -1 && tileLayer < MAP_LAYERS_COUNT);
77+
// Convert map units to pixels
78+
static glm::ivec2 MapUnitsToPixels(const glm::vec2& units)
79+
{
80+
return {
81+
MapUnitsToPixels(units.x),
82+
MapUnitsToPixels(units.y)
83+
};
84+
}
85+
static glm::ivec3 MapUnitsToPixels(const glm::vec3& units)
86+
{
87+
return {
88+
MapUnitsToPixels(units.x),
89+
MapUnitsToPixels(units.y),
90+
MapUnitsToPixels(units.z)
91+
};
92+
}
93+
94+
// Convert pixels directly to meters
95+
static float PixelsToMeters(int pixels)
96+
{
97+
return ((1.0f * pixels) / PIXELS_PER_MAP_UNIT) * METERS_PER_MAP_UNIT;
98+
}
99+
100+
// Convert meters directly to pixels
101+
static int MetersToPixels(float meters)
102+
{
103+
return (int) ((meters / METERS_PER_MAP_UNIT) * PIXELS_PER_MAP_UNIT);
104+
}
105+
106+
// Convert pixels directly to meters
107+
static glm::vec2 PixelsToMeters(const glm::ivec2& pixels)
108+
{
109+
return {
110+
PixelsToMeters(pixels.x),
111+
PixelsToMeters(pixels.y)
112+
};
113+
}
114+
static glm::vec3 PixelsToMeters(const glm::ivec3& pixels)
115+
{
116+
return {
117+
PixelsToMeters(pixels.x),
118+
PixelsToMeters(pixels.y),
119+
PixelsToMeters(pixels.z)
120+
};
121+
}
54122

55-
return tileLayer;
56-
}
123+
// Convert meters directly to pixels
124+
static glm::ivec2 MetersToPixels(const glm::vec2& meters)
125+
{
126+
return {
127+
MetersToPixels(meters.x),
128+
MetersToPixels(meters.y)
129+
};
130+
}
131+
static glm::ivec3 MetersToPixels(const glm::vec3& meters)
132+
{
133+
return {
134+
MetersToPixels(meters.x),
135+
MetersToPixels(meters.y),
136+
MetersToPixels(meters.z)
137+
};
138+
}
139+
};

src/FollowCameraController.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
#include "TimeManager.h"
77

88
FollowCameraController::FollowCameraController()
9-
: mStartupCameraHeight(8.0f)
10-
, mFollowPedCameraHeight(5.0f)
11-
, mFollowPedCameraCatchSpeed(5.0f)
9+
: mStartupCameraHeight(32.0f)
10+
, mFollowPedCameraHeight(20.0f)
11+
, mFollowPedCameraCatchSpeed(20.0f)
1212
, mScrollHeightOffset()
1313
{
1414
}

src/FollowCameraController.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class FollowCameraController final: public CameraController
2424

2525
private:
2626
// parameters
27-
float mStartupCameraHeight;
28-
float mFollowPedCameraHeight;
29-
float mScrollHeightOffset;
30-
float mFollowPedCameraCatchSpeed;
27+
float mStartupCameraHeight; // meters
28+
float mFollowPedCameraHeight; // meters
29+
float mScrollHeightOffset; // meters
30+
float mFollowPedCameraCatchSpeed; // meters per second
3131

3232
Pedestrian* mFollowPedestrian = nullptr;
3333
};

src/FreeLookCameraController.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void FreeLookCameraController::UpdateFrame()
4949
{
5050
moveDirection -= mCamera->mRightDirection;
5151
}
52-
moveDirection = glm::normalize(moveDirection) * 5.0f * deltaTime;
52+
moveDirection = glm::normalize(moveDirection) * mMoveSpeed * deltaTime;
5353
mCamera->Translate(moveDirection);
5454
}
5555

@@ -143,6 +143,6 @@ void FreeLookCameraController::InputEvent(MouseScrollInputEvent& inputEvent)
143143
return;
144144

145145
glm::vec3 position = mCamera->mPosition;
146-
position.y = glm::max(position.y + (MAP_BLOCK_LENGTH * 0.5f * -inputEvent.mScrollY), MAP_LAYERS_COUNT * MAP_BLOCK_LENGTH);
146+
position.y = glm::max(position.y + (0.5f * -inputEvent.mScrollY), MAP_LAYERS_COUNT * 1.0f);
147147
mCamera->SetPosition(position);
148148
}

src/FreeLookCameraController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ class FreeLookCameraController final: public CameraController
2424
int mLastMouseX, mLastMouseY;
2525
int mRotateDeltaX, mRotateDeltaY;
2626
bool mMouseDragCamera;
27+
float mMoveSpeed = 20.0f; // meters per second
2728
};

0 commit comments

Comments
 (0)