-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseManager.cpp
More file actions
169 lines (138 loc) · 4.81 KB
/
ResponseManager.cpp
File metadata and controls
169 lines (138 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "ResponseManager.h"
std::mutex response_manager::_responseMutex;
//Implmented with use of of Ed Milligton: Game Physics Engine Development
response_manager::response_manager(contact_manifold* contactManifold, const int positionIterations, const int velocityIterations, const float positionEpsilon, const float velocityEpsilon) : m_position_iterations_done_(0), m_position_iterations_(positionIterations), m_velocity_iterations_count_(0), m_velocity_count_(velocityIterations), m_pos_epsilon_(positionEpsilon), m_vel_epsilon_(velocityEpsilon), m_contact_manifold_(contactManifold)
{
}
void response_manager::contact_resolve(const float dt)
{
std::lock_guard<std::mutex> lock(_responseMutex);
m_position_iterations_done_ = 0;
m_velocity_iterations_count_ = 0;
//Return if no contacts then
if (!m_contact_manifold_->GetNumberOfPoints())
{
return;
}
//Prepare Contacts
ready_contacts(dt);
change_pos(dt);
change_vel(dt);
}
void response_manager::change_vel(const float dt)
{
//std::lock_guard<std::mutex> lock(_responseMutex);
//Now need to update velocities
XMVECTOR velocityChange[2]{};
XMVECTOR angularVelocityChange[2];
auto deltaVelocity = XMVECTOR();
while (m_velocity_iterations_count_ < m_velocity_count_)
{
auto max = m_vel_epsilon_;
auto index = m_contact_manifold_->GetNumberOfPoints();
for (unsigned i = 0; i < m_contact_manifold_->GetNumberOfPoints(); i++)
{
const auto& point = m_contact_manifold_->GetPoint(i);
if (point.desiredDeltaVelocity > max)
{
max = point.desiredDeltaVelocity;
index = i;
}
}
if (index == m_contact_manifold_->GetNumberOfPoints())
{
break;
}
auto& point = m_contact_manifold_->GetPoint(index);
point.awake_state(); //Get awake state
point.apply_velocity_change(velocityChange, angularVelocityChange);
//Contacts updates with new velocity changes
auto i = 0;
while ( i < m_contact_manifold_->GetNumberOfPoints())
{
auto& localPoint = m_contact_manifold_->GetPoint(i);
auto j = 0;
while ( j < 2&& localPoint.contactID[j])
{
for (unsigned k = 0; k < 2; k++)
{
if (localPoint.contactID[j] == point.contactID[k])
{
deltaVelocity = XMVectorAdd(velocityChange[k], XMVector3Cross(angularVelocityChange[k], localPoint.relativeContactPosition[j]));
const auto contactToWorldTranspose = XMMatrixTranspose(localPoint.contactToWorld);
const auto contactDeltaVelocity = XMVector3Transform(deltaVelocity, contactToWorldTranspose);
localPoint.contactVelocity += XMVectorScale(contactDeltaVelocity, j ? -1 : 1);
localPoint.calculate_desired_delta_velocity(dt);
}
}
j++;
}
i++;
}
m_velocity_iterations_count_++;
}
}
void response_manager::change_pos(const float dt)
{
//std::lock_guard<std::mutex> lock(_responseMutex);
//PositionComponent chnage and resolving of penetrations
unsigned int i = 0;
unsigned int index = 0;
XMVECTOR linearChange[2];
XMVECTOR angularChange[2];
auto deltaPosition = XMVECTOR();
auto maxPenetration = 0.0f;
m_position_iterations_done_ = 0;
while (m_position_iterations_done_ < m_position_iterations_)
{
//Largest peneration to be found
maxPenetration = m_pos_epsilon_; // Small value set so any small penetrations cna be ignored
for (i = 0; i < m_contact_manifold_->GetNumberOfPoints(); i++)
{
auto& point = m_contact_manifold_->GetPoint(i);
if (point.penetrationDepth > maxPenetration)
{
maxPenetration = point.penetrationDepth;
index = i;
}
}
if (index == m_contact_manifold_->GetNumberOfPoints())
{
break;
}
auto& point = m_contact_manifold_->GetPoint(index);
point.awake_state(); //Match awake state
point.resolve_penetration(linearChange, angularChange, maxPenetration);
//Update contacts with new penetrations so the same penetration isn't resolved again
//And also so other contacts don't have the wrong penetrations
auto i = 0;
while (i < m_contact_manifold_->GetNumberOfPoints())
{
auto& otherPoint = m_contact_manifold_->GetPoint(i);
auto b = 0;
while ( b < 2&&otherPoint.contactID[b])
{
for (unsigned int d = 0; d < 2; d++)
{
if (otherPoint.contactID[b] == point.contactID[d])
{
deltaPosition = XMVectorAdd(linearChange[d], XMVector3Cross(angularChange[d], otherPoint.relativeContactPosition[b]));
XMStoreFloat(&otherPoint.penetrationDepth, XMVectorAdd(XMLoadFloat(&otherPoint.penetrationDepth), XMVectorScale(XMVector3Dot(deltaPosition, otherPoint.contactNormal), (b ? 1 : -1))));
}
}
b++;
}
i++;
}
m_position_iterations_done_++;
}
}
void response_manager::ready_contacts(const float dt) const
{
//std::lock_guard<std::mutex> lock(_responseMutex);
for (auto collision = 0; collision < m_contact_manifold_->GetNumberOfPoints(); ++collision)
{
auto& point = m_contact_manifold_->GetPoint(collision);
point.internals_calcs(dt);
}
}