aboutsummaryrefslogtreecommitdiff
path: root/Client/Source/Phy2DLite/Arbiter.cpp
blob: d29bacefd906e6d23461308847b5dcd9cd6223d4 (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "Arbiter.h"
#include "World.h"
#include "Body.h"
#include "Joint.h"
#include "Constants.h"

using namespace Phy2D;

Arbiter::Arbiter(Body* b1, Body* b2)
{
    if (b1 < b2)
    {
        body1 = b1;
        body2 = b2;
    }
    else
    {
        body1 = b2;
        body2 = b1;
    }

    numContacts = Collide(contacts, body1, body2);

    friction = SQRT(body1->friction * body2->friction);
}

void Arbiter::Update(Contact* newContacts, int numNewContacts)
{
    Contact mergedContacts[2];

    for (int i = 0; i < numNewContacts; ++i)
    {
        Contact* cNew = newContacts + i;
        int k = -1;
        for (int j = 0; j < numContacts; ++j)
        {
            Contact* cOld = contacts + j;
            if (cNew->feature.value == cOld->feature.value)
            {
                k = j;
                break;
            }
        }

        if (k > -1)
        {
            Contact* c = mergedContacts + i;
            Contact* cOld = contacts + k;
            *c = *cNew;
            if (World::warmStarting)
            {
                c->Pn = cOld->Pn;
                c->Pt = cOld->Pt;
                c->Pnb = cOld->Pnb;
            }
            else
            {
                c->Pn = _0;
                c->Pt = _0;
                c->Pnb = _0;
            }
        }
        else
        {
            mergedContacts[i] = newContacts[i];
        }
    }

    for (int i = 0; i < numNewContacts; ++i)
        contacts[i] = mergedContacts[i];

    numContacts = numNewContacts;
}


void Arbiter::PreStep(fixed inv_dt)
{
    const fixed k_allowedPenetration = _0_01;
    fixed k_biasFactor = World::positionCorrection ? _0_2 : _0;

    for (int i = 0; i < numContacts; ++i)
    {
        Contact* c = contacts + i;

        Vec2 r1 = c->position - body1->position;
        Vec2 r2 = c->position - body2->position;

        // Precompute normal mass, tangent mass, and bias.
        fixed rn1 = Dot(r1, c->normal);
        fixed rn2 = Dot(r2, c->normal);
        fixed kNormal = body1->invMass + body2->invMass;
        kNormal += body1->invI * (Dot(r1, r1) - rn1 * rn1) + body2->invI * (Dot(r2, r2) - rn2 * rn2);
        c->massNormal = (fixed)_1 / kNormal;

        Vec2 tangent = Cross(c->normal, _1);
        fixed rt1 = Dot(r1, tangent);
        fixed rt2 = Dot(r2, tangent);
        fixed kTangent = body1->invMass + body2->invMass;
        kTangent += body1->invI * (Dot(r1, r1) - rt1 * rt1) + body2->invI * (Dot(r2, r2) - rt2 * rt2);
        c->massTangent = (fixed)_1 / kTangent;

        c->bias = -k_biasFactor * inv_dt * Min(_0, c->separation + k_allowedPenetration);

        if (World::accumulateImpulses)
        {
            // Apply normal + friction impulse
            Vec2 P = c->Pn * c->normal + c->Pt * tangent;

            body1->velocity -= body1->invMass * P;
            body1->angularVelocity -= body1->invI * Cross(r1, P);

            body2->velocity += body2->invMass * P;
            body2->angularVelocity += body2->invI * Cross(r2, P);
        }
    }
}

void Arbiter::ApplyImpulse()
{
    Body* b1 = body1;
    Body* b2 = body2;

    for (int i = 0; i < numContacts; ++i)
    {
        Contact* c = contacts + i;
        c->r1 = c->position - b1->position;
        c->r2 = c->position - b2->position;

        // Relative velocity at contact
        Vec2 dv = b2->velocity + Cross(b2->angularVelocity, c->r2) - b1->velocity - Cross(b1->angularVelocity, c->r1);

        // Compute normal impulse
        fixed vn = Dot(dv, c->normal);

        fixed dPn = c->massNormal * (-vn + c->bias);

        if (World::accumulateImpulses)
        {
            // Clamp the accumulated impulse
            fixed Pn0 = c->Pn;
            c->Pn = Max(Pn0 + dPn, _0);
            dPn = c->Pn - Pn0;
        }
        else
        {
            dPn = Max(dPn, _0);
        }

        // Apply contact impulse
        Vec2 Pn = dPn * c->normal;

        b1->velocity -= b1->invMass * Pn;
        b1->angularVelocity -= b1->invI * Cross(c->r1, Pn);

        b2->velocity += b2->invMass * Pn;
        b2->angularVelocity += b2->invI * Cross(c->r2, Pn);

        // Relative velocity at contact
        dv = b2->velocity + Cross(b2->angularVelocity, c->r2) - b1->velocity - Cross(b1->angularVelocity, c->r1);

        Vec2 tangent = Cross(c->normal, _1);
        fixed vt = Dot(dv, tangent);
        fixed dPt = c->massTangent * (-vt);

        if (World::accumulateImpulses)
        {
            // Compute friction impulse
            fixed maxPt = friction * c->Pn;

            // Clamp friction
            fixed oldTangentImpulse = c->Pt;
            c->Pt = Clamp(oldTangentImpulse + dPt, -maxPt, maxPt);
            dPt = c->Pt - oldTangentImpulse;
        }
        else
        {
            fixed maxPt = friction * dPn;
            dPt = Clamp(dPt, -maxPt, maxPt);
        }

        // Apply contact impulse
        Vec2 Pt = dPt * tangent;

        b1->velocity -= b1->invMass * Pt;
        b1->angularVelocity -= b1->invI * Cross(c->r1, Pt);

        b2->velocity += b2->invMass * Pt;
        b2->angularVelocity += b2->invI * Cross(c->r2, Pt);
    }
}