Skip to content

Commit 8f4252c

Browse files
committed
refactore physics, work in progress
1 parent 919d5da commit 8f4252c

8 files changed

Lines changed: 94 additions & 70 deletions

src/GameCheatsWindow.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ void GameCheatsWindow::DoUI(ImGuiIO& imguiContext)
6868
{
6969
ImGui::Separator();
7070
glm::vec3 pedPosition = pedestrian->mPhysicsComponent->GetPosition();
71-
ImGui::Text("pos: %f, %f, %f", pedPosition.x, pedPosition.y, pedPosition.z);
71+
ImGui::Text("physical pos: %.3f, %.3f, %.3f", pedPosition.x, pedPosition.y, pedPosition.z);
72+
glm::vec3 logicalPosition = Convert::MetersToMapUnits(pedPosition);
73+
ImGui::Text("logical pos: %.3f, %.3f, %.3f", logicalPosition.x, logicalPosition.y, logicalPosition.z);
7274

7375
cxx::angle_t pedHeading = pedestrian->mPhysicsComponent->GetRotationAngle();
7476
ImGui::Text("heading: %f", pedHeading.mDegrees);
@@ -146,9 +148,13 @@ void GameCheatsWindow::DoUI(ImGuiIO& imguiContext)
146148

147149
if (ImGui::CollapsingHeader("Ped"))
148150
{
149-
ImGui::SliderFloat("Turn speed", &gGameParams.mPedestrianTurnSpeed, 10.0f, 640.0f, "%.2f");
150-
ImGui::SliderFloat("Run speed", &gGameParams.mPedestrianRunSpeed, 0.1f, 16.0f, "%.2f");
151-
ImGui::SliderFloat("Walk speed", &gGameParams.mPedestrianWalkSpeed, 0.1f, 16.0f, "%.2f");
151+
ImGui::SliderFloat("Turn speed (degs/s)", &gGameParams.mPedestrianTurnSpeed, 10.0f, 640.0f, "%.2f");
152+
ImGui::SliderFloat("Run speed (m/s)", &gGameParams.mPedestrianRunSpeed,
153+
Convert::MapUnitsToMeters(0.1f),
154+
Convert::MapUnitsToMeters(16.0f), "%.2f");
155+
ImGui::SliderFloat("Walk speed (m/s)", &gGameParams.mPedestrianWalkSpeed,
156+
Convert::MapUnitsToMeters(0.1f),
157+
Convert::MapUnitsToMeters(16.0f), "%.2f");
152158
}
153159

154160
if (ImGui::CollapsingHeader("Graphics"))

src/GameMapHelpers.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,47 +98,47 @@ void GameMapHelpers::PutBlockFace(GameMapManager& cityScape, MapMeshData& meshDa
9898
{
9999
// N, 26 low, high
100100
case 1: case 2:
101-
cubePoints[0].y = cubePoints[1].y = ((slope - 1) / 2.0f);
102-
cubePoints[4].y = cubePoints[5].y = ((slope - 1 + 1) / 2.0f);
101+
cubePoints[0].y = cubePoints[1].y = ((slope - 1) / 2.0f) * METERS_PER_MAP_UNIT;
102+
cubePoints[4].y = cubePoints[5].y = ((slope - 1 + 1) / 2.0f) * METERS_PER_MAP_UNIT;
103103
break;
104104
// S, 26 low, high
105105
case 3: case 4:
106-
cubePoints[4].y = cubePoints[5].y = ((slope - 3) / 2.0f);
107-
cubePoints[0].y = cubePoints[1].y = ((slope - 3 + 1) / 2.0f);
106+
cubePoints[4].y = cubePoints[5].y = ((slope - 3) / 2.0f) * METERS_PER_MAP_UNIT;
107+
cubePoints[0].y = cubePoints[1].y = ((slope - 3 + 1) / 2.0f) * METERS_PER_MAP_UNIT;
108108
break;
109109
// W, 26 low, high
110110
case 5: case 6:
111-
cubePoints[1].y = cubePoints[5].y = ((slope - 5) / 2.0f);
112-
cubePoints[0].y = cubePoints[4].y = ((slope - 5 + 1) / 2.0f);
111+
cubePoints[1].y = cubePoints[5].y = ((slope - 5) / 2.0f) * METERS_PER_MAP_UNIT;
112+
cubePoints[0].y = cubePoints[4].y = ((slope - 5 + 1) / 2.0f) * METERS_PER_MAP_UNIT;
113113
break;
114114
// E, 26 low, high
115115
case 7: case 8:
116-
cubePoints[0].y = cubePoints[4].y = ((slope - 7) / 2.0f);
117-
cubePoints[1].y = cubePoints[5].y = ((slope - 7 + 1) / 2.0f);
116+
cubePoints[0].y = cubePoints[4].y = ((slope - 7) / 2.0f) * METERS_PER_MAP_UNIT;
117+
cubePoints[1].y = cubePoints[5].y = ((slope - 7 + 1) / 2.0f) * METERS_PER_MAP_UNIT;
118118
break;
119119
// N, 7 low - high
120120
case 9: case 10: case 11: case 12:
121121
case 13: case 14: case 15: case 16:
122-
cubePoints[0].y = cubePoints[1].y = ((slope - 9) / 8.0f);
123-
cubePoints[4].y = cubePoints[5].y = ((slope - 9 + 1) / 8.0f);
122+
cubePoints[0].y = cubePoints[1].y = ((slope - 9) / 8.0f) * METERS_PER_MAP_UNIT;
123+
cubePoints[4].y = cubePoints[5].y = ((slope - 9 + 1) / 8.0f) * METERS_PER_MAP_UNIT;
124124
break;
125125
// S, 7 low - high
126126
case 17: case 18: case 19: case 20:
127127
case 21: case 22: case 23: case 24:
128-
cubePoints[4].y = cubePoints[5].y = ((slope - 17) / 8.0f);
129-
cubePoints[0].y = cubePoints[1].y = ((slope - 17 + 1) / 8.0f);
128+
cubePoints[4].y = cubePoints[5].y = ((slope - 17) / 8.0f) * METERS_PER_MAP_UNIT;
129+
cubePoints[0].y = cubePoints[1].y = ((slope - 17 + 1) / 8.0f) * METERS_PER_MAP_UNIT;
130130
break;
131131
// W, 7 low - high
132132
case 25: case 26: case 27: case 28:
133133
case 29: case 30: case 31: case 32:
134-
cubePoints[1].y = cubePoints[5].y = ((slope - 25) / 8.0f);
135-
cubePoints[0].y = cubePoints[4].y = ((slope - 25 + 1) / 8.0f);
134+
cubePoints[1].y = cubePoints[5].y = ((slope - 25) / 8.0f) * METERS_PER_MAP_UNIT;
135+
cubePoints[0].y = cubePoints[4].y = ((slope - 25 + 1) / 8.0f) * METERS_PER_MAP_UNIT;
136136
break;
137137
// E, 7 low - high
138138
case 33: case 34: case 35: case 36:
139139
case 37: case 38: case 39: case 40:
140-
cubePoints[0].y = cubePoints[4].y = ((slope - 33) / 8.0f);
141-
cubePoints[1].y = cubePoints[5].y = ((slope - 33 + 1) / 8.0f);
140+
cubePoints[0].y = cubePoints[4].y = ((slope - 33) / 8.0f) * METERS_PER_MAP_UNIT;
141+
cubePoints[1].y = cubePoints[5].y = ((slope - 33 + 1) / 8.0f) * METERS_PER_MAP_UNIT;
142142
break;
143143
// 41 - 44 = 45 N,S,W,E
144144
case 41: cubePoints[0].y = cubePoints[1].y = 0.0f; break;
@@ -254,6 +254,8 @@ void GameMapHelpers::PutBlockFace(GameMapManager& cityScape, MapMeshData& meshDa
254254

255255
int GameMapHelpers::GetSlopeHeight(int slopeType, int pxcoord_x, int pxcoord_y)
256256
{
257+
return 0.0f; // todo: redo
258+
257259
debug_assert(pxcoord_x >= 0 && pxcoord_x < PIXELS_PER_MAP_UNIT);
258260
debug_assert(pxcoord_y >= 0 && pxcoord_y < PIXELS_PER_MAP_UNIT);
259261

src/GameParams.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ GameParams::GameParams()
1010

1111
void GameParams::SetToDefaults()
1212
{
13-
mPedestrianBoundsSphereRadius = 0.15f;
13+
mPedestrianBoundsSphereRadius = Convert::MapUnitsToMeters(0.15f);
1414

1515
mPedestrianTurnSpeed = 260.0f;
1616
mPedestrianTurnSpeedSlideOnCar = 120.0f;
17-
mPedestrianSlideOnCarSpeed = 1.2f;
18-
mPedestrianWalkSpeed = 0.7f;
19-
mPedestrianRunSpeed = 1.5f;
20-
mPedestrianSpotTheCarDistance = 3.0f;
17+
mPedestrianSlideOnCarSpeed = Convert::MapUnitsToMeters(1.2f);
18+
mPedestrianWalkSpeed = Convert::MapUnitsToMeters(0.7f);
19+
mPedestrianRunSpeed = Convert::MapUnitsToMeters(1.5f);
20+
mPedestrianSpotTheCarDistance = Convert::MapUnitsToMeters(3.0f);
2121
mPedestrianKnockedDownTime = 3.0f;
22-
mPedestrianFallDeathHeight = 2.0f;
22+
mPedestrianFallDeathHeight = Convert::MapUnitsToMeters(2.0f);
2323
mPedestrianDrowningTime = 0.05f;
2424

2525
// weapons time todo
@@ -30,9 +30,9 @@ void GameParams::SetToDefaults()
3030
mWeaponsRechargeTime[eWeaponType_RocketLauncher] = 1.0f * 3.0f;
3131

3232
// weapons distance todo
33-
mWeaponsDistance[eWeaponType_Fists] = mPedestrianBoundsSphereRadius + 0.6f;
34-
mWeaponsDistance[eWeaponType_Pistol] = 1.0f;
35-
mWeaponsDistance[eWeaponType_Machinegun] = 1.0f;
36-
mWeaponsDistance[eWeaponType_Flamethrower] = 1.0f;
37-
mWeaponsDistance[eWeaponType_RocketLauncher] = 1.0f;
33+
mWeaponsDistance[eWeaponType_Fists] = mPedestrianBoundsSphereRadius + Convert::MapUnitsToMeters(0.6f);
34+
mWeaponsDistance[eWeaponType_Pistol] = Convert::MapUnitsToMeters(1.0f);
35+
mWeaponsDistance[eWeaponType_Machinegun] = Convert::MapUnitsToMeters(1.0f);
36+
mWeaponsDistance[eWeaponType_Flamethrower] = Convert::MapUnitsToMeters(1.0f);
37+
mWeaponsDistance[eWeaponType_RocketLauncher] = Convert::MapUnitsToMeters(1.0f);
3838
}

src/GameParams.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ class GameParams
1313

1414
public:
1515
// pedestrians
16-
float mPedestrianBoundsSphereRadius; // bounding sphere radius, map units
16+
float mPedestrianBoundsSphereRadius; // bounding sphere radius, meters
1717

1818
float mPedestrianTurnSpeed; // degrees per second
1919
float mPedestrianTurnSpeedSlideOnCar; // degrees per second
20-
float mPedestrianSlideOnCarSpeed; // in map units per second
21-
float mPedestrianWalkSpeed; // in map units per second
22-
float mPedestrianRunSpeed; // in map units per second
20+
float mPedestrianSlideOnCarSpeed; // meters per second
21+
float mPedestrianWalkSpeed; // meters per second
22+
float mPedestrianRunSpeed; // meters per second
2323
float mPedestrianKnockedDownTime; // knocked down duration after the punch in face, seconds
24-
float mPedestrianSpotTheCarDistance; // max distance to detect the car, map units
25-
float mPedestrianFallDeathHeight; // falling distance which causes pedestrian death, map units
24+
float mPedestrianSpotTheCarDistance; // max distance to detect the car, meters
25+
float mPedestrianFallDeathHeight; // falling distance which causes pedestrian death, meters
2626
float mPedestrianDrowningTime; // seconds
2727
float mWeaponsRechargeTime[eWeaponType_COUNT]; // weapons reload time, seconds
28-
float mWeaponsDistance[eWeaponType_COUNT]; // map units
28+
float mWeaponsDistance[eWeaponType_COUNT]; // hit distance, meters
2929
};
3030

3131
extern GameParams gGameParams;

src/PedestrianStates.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,13 @@ void PedestrianStatesManager::ProcessRotateActions()
129129
turnSpeed = gGameParams.mPedestrianTurnSpeedSlideOnCar;
130130
}
131131

132-
float angularVelocity = turnSpeed * (mPedestrian->mCtlActions[ePedestrianAction_TurnLeft] ? -1.0f : 1.0f);
132+
cxx::angle_t angularVelocity = cxx::angle_t::from_degrees(turnSpeed * (mPedestrian->mCtlActions[ePedestrianAction_TurnLeft] ? -1.0f : 1.0f));
133133
mPedestrian->mPhysicsComponent->SetAngularVelocity(angularVelocity);
134134
}
135135
else
136136
{
137-
mPedestrian->mPhysicsComponent->SetAngularVelocity(0.0f);
137+
cxx::angle_t angularVelocity;
138+
mPedestrian->mPhysicsComponent->SetAngularVelocity(angularVelocity);
138139
}
139140
}
140141

@@ -145,18 +146,18 @@ void PedestrianStatesManager::ProcessMotionActions()
145146
{
146147
glm::vec2 linearVelocity = gGameParams.mPedestrianSlideOnCarSpeed * mPedestrian->mPhysicsComponent->GetSignVector();
147148
mPedestrian->mPhysicsComponent->SetLinearVelocity(linearVelocity);
148-
149149
return;
150150
}
151151

152+
glm::vec2 linearVelocity {};
152153
// generic case
153154
if (mPedestrian->mCtlActions[ePedestrianAction_WalkForward] ||
154155
mPedestrian->mCtlActions[ePedestrianAction_WalkBackward] ||
155156
mPedestrian->mCtlActions[ePedestrianAction_Run])
156157
{
157158
float moveSpeed = gGameParams.mPedestrianWalkSpeed;
158159

159-
glm::vec2 linearVelocity = mPedestrian->mPhysicsComponent->GetSignVector();
160+
linearVelocity = mPedestrian->mPhysicsComponent->GetSignVector();
160161
if (mPedestrian->mCtlActions[ePedestrianAction_Run])
161162
{
162163
moveSpeed = gGameParams.mPedestrianRunSpeed;
@@ -165,12 +166,14 @@ void PedestrianStatesManager::ProcessMotionActions()
165166
{
166167
linearVelocity = -linearVelocity;
167168
}
168-
mPedestrian->mPhysicsComponent->SetLinearVelocity(linearVelocity * moveSpeed);
169+
170+
linearVelocity *= moveSpeed;
169171
}
170172
else
171173
{
172-
mPedestrian->mPhysicsComponent->SetLinearVelocity({}); // force stop
174+
// force stop
173175
}
176+
mPedestrian->mPhysicsComponent->SetLinearVelocity(linearVelocity);
174177
}
175178

176179
bool PedestrianStatesManager::TryToShoot()

src/PhysicsComponents.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ PhysicsComponent::PhysicsComponent(b2World* physicsWorld)
1111
: mHeight()
1212
, mPhysicsWorld(physicsWorld)
1313
, mPhysicsBody()
14+
, mPreviousPosition()
15+
, mSmoothPosition()
1416
{
1517
debug_assert(physicsWorld);
1618
}
@@ -75,9 +77,9 @@ glm::vec2 PhysicsComponent::GetLinearVelocity() const
7577
return { b2position.x, b2position.y };
7678
}
7779

78-
float PhysicsComponent::GetAngularVelocity() const
80+
cxx::angle_t PhysicsComponent::GetAngularVelocity() const
7981
{
80-
float angularVelocity = glm::degrees(mPhysicsBody->GetAngularVelocity());
82+
cxx::angle_t angularVelocity = cxx::angle_t::from_radians(mPhysicsBody->GetAngularVelocity());
8183
return angularVelocity;
8284
}
8385

@@ -86,9 +88,9 @@ void PhysicsComponent::AddAngularImpulse(float impulse)
8688
mPhysicsBody->ApplyAngularImpulse(impulse, true);
8789
}
8890

89-
void PhysicsComponent::SetAngularVelocity(float angularVelocity)
91+
void PhysicsComponent::SetAngularVelocity(cxx::angle_t angularVelocity)
9092
{
91-
mPhysicsBody->SetAngularVelocity(glm::radians(angularVelocity));
93+
mPhysicsBody->SetAngularVelocity(angularVelocity.to_radians());
9294
}
9395

9496
void PhysicsComponent::SetLinearVelocity(const glm::vec2& velocity)
@@ -151,7 +153,7 @@ PedPhysicsComponent::PedPhysicsComponent(b2World* physicsWorld, const glm::vec3&
151153
debug_assert(mPhysicsBody);
152154

153155
b2CircleShape shapeDef;
154-
shapeDef.m_radius = Convert::MapUnitsToMeters(gGameParams.mPedestrianBoundsSphereRadius);
156+
shapeDef.m_radius = gGameParams.mPedestrianBoundsSphereRadius;
155157

156158
b2FixtureDef fixtureDef;
157159
fixtureDef.shape = &shapeDef;
@@ -162,7 +164,7 @@ PedPhysicsComponent::PedPhysicsComponent(b2World* physicsWorld, const glm::vec3&
162164
debug_assert(b2fixture);
163165

164166
// create sensor
165-
shapeDef.m_radius = Convert::MapUnitsToMeters(gGameParams.mPedestrianBoundsSphereRadius);
167+
shapeDef.m_radius = gGameParams.mPedestrianBoundsSphereRadius;
166168
fixtureDef.shape = &shapeDef;
167169
fixtureDef.isSensor = true;
168170
fixtureDef.filter.categoryBits = PHYSICS_OBJCAT_PED_SENSOR;

src/PhysicsComponents.h

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,57 @@ class PhysicsComponent: public cxx::noncopyable
2525
glm::vec3 mSmoothPosition; // for rendering only
2626

2727
public:
28-
// set/get object's world position and rotation angle
29-
// @param position: Coordinate
30-
// @param rotationAngle: Rotation, optional
28+
virtual ~PhysicsComponent();
29+
30+
// Set or get object's world position and rotation angle
31+
// @param position: Coordinate, meters
32+
// @param rotationAngle: Angle
3133
void SetPosition(const glm::vec3& position);
3234
void SetPosition(const glm::vec3& position, cxx::angle_t rotationAngle);
3335
glm::vec3 GetPosition() const;
34-
// set/get object's heading angle
35-
// @param rotationAngle: Angle value
36+
37+
// Set or get object's heading angle
38+
// @param rotationAngle: Angle
3639
void SetRotationAngle(cxx::angle_t rotationAngle);
3740
cxx::angle_t GetRotationAngle() const;
38-
// set/get current angular velocity
39-
// @param velocity: new angular velocity in degrees/second
40-
void SetAngularVelocity(float angularVelocity);
41-
float GetAngularVelocity() const;
42-
/// set/get the linear velocity of the center of mass
43-
/// @param velocity: new linear velocity of the center of mass
41+
42+
// Set or get current angular velocity
43+
// @param velocity: New angular velocity in degrees/second
44+
void SetAngularVelocity(cxx::angle_t angularVelocity);
45+
cxx::angle_t GetAngularVelocity() const;
46+
47+
/// Set or get the linear velocity of the center of mass
48+
/// @param velocity: New linear velocity of the center of mass, meters per second
4449
void SetLinearVelocity(const glm::vec2& velocity);
4550
glm::vec2 GetLinearVelocity() const;
4651
glm::vec2 GetSignVector() const;
47-
// convert coordinate from local to world space and vice versa
52+
53+
// Convert coordinate from local to world space and vice versa
54+
// @param localPosition, worldPosition: Coordinate in meters
4855
glm::vec2 GetWorldPoint(const glm::vec2& localPosition) const;
4956
glm::vec2 GetLocalPoint(const glm::vec2& worldPosition) const;
50-
// apply an impulse to the center of mass
57+
58+
// Apply an impulse to the center of mass
5159
// @param impulse: The world impulse vector, usually in N-seconds or kg-m/s
5260
void AddLinearImpulse(const glm::vec2& impulse);
53-
// apply a force to the center of mass
61+
62+
// Apply a force to the center of mass
5463
// @param force: Force, the world force vector, usually in Newtons (N)
5564
void AddForce(const glm::vec2& force);
56-
// apply an angular impulse
65+
66+
// Apply an angular impulse
5767
// @param impulse the angular impulse in units of kg*m*m/s
5868
void AddAngularImpulse(float impulse);
59-
// cancel currently active forces
69+
70+
// Cancel currently active forces
6071
void ClearForces();
61-
// clear state
72+
73+
// Clear state
6274
void SetRespawned();
6375

6476
protected:
6577
// only derived classes could be instantiated
6678
PhysicsComponent(b2World* physicsWorld);
67-
virtual ~PhysicsComponent();
6879

6980
protected:
7081
b2World* mPhysicsWorld;

src/Vehicle.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ void Vehicle::DrawDebug(DebugRenderer& debugRender)
101101
{
102102
glm::vec2 rotated_pos;
103103
GetDoorPos(idoor, rotated_pos);
104-
debugRender.DrawCube(glm::vec3(rotated_pos.x, position.y + 0.05f, rotated_pos.y), glm::vec3(0.05f, 0.05f, 0.05f), Color32_Yellow, false);
104+
debugRender.DrawCube(glm::vec3(rotated_pos.x, position.y + 0.15f, rotated_pos.y), glm::vec3(0.15f, 0.15f, 0.15f), Color32_Yellow, false);
105105
}
106106

107107
if (mCarStyle->mDoorsCount > 0)
108108
{
109109
glm::vec2 seatpos;
110110
GetSeatPos(eCarSeat_Driver, seatpos);
111-
debugRender.DrawCube(glm::vec3(seatpos.x, position.y + 0.05f, seatpos.y), glm::vec3(0.05f, 0.05f, 0.05f), Color32_Green, false);
111+
debugRender.DrawCube(glm::vec3(seatpos.x, position.y + 0.15f, seatpos.y), glm::vec3(0.15f, 0.15f, 0.15f), Color32_Green, false);
112112
}
113113

114114
// draw wheels

0 commit comments

Comments
 (0)