Skip to content

Commit 5d1f789

Browse files
committed
Working physics module 🚀
1 parent 55f7209 commit 5d1f789

28 files changed

+309
-339
lines changed

‎source/math/Bounds4.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Forth
2626
void Allocate(const Vector4 &point)
2727
{
2828
min = Min(min, point);
29-
max = Min(max, point);
29+
max = Max(max, point);
3030
}
3131

3232
inline Vector4 center(void) const
@@ -66,15 +66,15 @@ namespace Forth
6666
}
6767

6868
/// <summary> Returns a point that either inside or touching the bound </summary>
69-
inline Vector4 Clamp(Vector4 &point) const
69+
inline Vector4 Clamp(const Vector4 &point) const
7070
{
7171
Vector4 c = center(), e = extent();
72-
point = point - c;
72+
Vector4 p = point - c;
7373

7474
for (int i = 0; i < 4; i++)
75-
point[i] = Forth::Clamp(-e[i], e[i], point[i]);
75+
p[i] = Forth::Clamp(-e[i], e[i], point[i]);
7676

77-
return point + c;
77+
return p + c;
7878
}
7979

8080
/// <summary> Returns a point that either outside or touching the bound </summary>

‎source/math/Matrix4.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ namespace Forth
8787

8888
Matrix4 Euler(const Euler4 &e)
8989
{
90-
Euler(e.x, e.y, e.z, e.t, e.u, e.v);
90+
return Euler(e.x, e.y, e.z, e.t, e.u, e.v);
9191
}
9292

9393
/// <summary>

‎source/math/Transform4.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ namespace Forth
117117

118118
/// <summary> Convert to non-orthogonal matrix by applying a scale </summary>
119119
/// <remarks> Do not inverse or multiply any matrix produced using this method. </remarks>
120-
Transform4 ToTRS(Vector4 scale)
120+
Transform4 ToTRS(const Vector4& scale)
121121
{
122122
return Transform4(position, rotation * Matrix4(scale));
123123
}

‎source/physics/Common.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ namespace Forth
1717
float Common::PENETRATION_SLOP = 0.02f;
1818
float Common::MAX_DT = 0.02f;
1919

20-
float Common::DefMixRestitution(Shape* A, Shape* B)
20+
float Common::DefMixRestitution(Shape *A, Shape *B)
2121
{
2222
return A->restitution > B->restitution ? A->restitution : B->restitution;
2323
}
24-
float Common::DefMixFriction(Shape* A, Shape* B)
24+
float Common::DefMixFriction(Shape *A, Shape *B)
2525
{
2626
return (A->friction + B->friction) * 0.5f;
2727
}
2828

29-
static Common::ShapeMixCB MixRestitution = Common::DefMixRestitution;
30-
static Common::ShapeMixCB MixFriction = Common::DefMixFriction;
29+
Common::ShapeMixCB Common::MixRestitution = Common::DefMixRestitution;
30+
Common::ShapeMixCB Common::MixFriction = Common::DefMixFriction;
3131
} // namespace Physics
3232
} // namespace Forth

‎source/physics/Common.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
#include "../extras/Utils.h"
44
#include "../math/Bounds4.h"
5-
#include "../math/math.h"
65
#include "../math/Matrix4.h"
76
#include "../math/Plane4.h"
87
#include "../math/Ray4.h"
8+
#include "../math/Tensor4.h"
99
#include "../math/Transform4.h"
1010
#include "../math/Vector4.h"
11-
#include "../math/Tensor4.h"
12-
#include <vector>
13-
#include <stack>
14-
#include <map>
11+
#include "../math/math.h"
1512
#include <algorithm>
13+
#include <map>
14+
#include <stack>
15+
#include <vector>
1616

1717
namespace Forth
1818
{
@@ -93,20 +93,20 @@ namespace Forth
9393
/// </summary>
9494
static float MAX_DT;
9595

96-
typedef float(*ShapeMixCB)(class Shape*, class Shape*);
96+
typedef float (*ShapeMixCB)(class Shape *, class Shape *);
9797

9898
static ShapeMixCB MixRestitution;
9999
static ShapeMixCB MixFriction;
100100

101101
/// <summary>
102102
/// Restitution (bounce) mixing. Default is max
103103
/// </summary>
104-
static float DefMixRestitution(class Shape* A, class Shape* B);
104+
static float DefMixRestitution(class Shape *A, class Shape *B);
105105

106106
/// <summary>
107107
/// Friction (slide) mixing. Default is average
108108
/// </summary>
109-
static float DefMixFriction(class Shape* A, class Shape* B);
109+
static float DefMixFriction(class Shape *A, class Shape *B);
110110
};
111111
} // namespace Physics
112112
} // namespace Forth

‎source/physics/broadphase/BroadPhase.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,16 @@ namespace Forth
55
{
66
namespace Physics
77
{
8-
BroadPhase::BroadPhase(ContactManager* manager)
9-
{
10-
Manager = manager;
11-
12-
PairBuffer = ::std::vector<ContactPair>();
13-
MoveBuffer = ::std::vector<int>();
14-
}
8+
BroadPhase::BroadPhase(ContactManager *manager) : Tree(), Manager(manager), PairBuffer(), MoveBuffer() {}
159

16-
void BroadPhase::InsertShape(Shape* shape, const Bounds4 &aabb)
10+
void BroadPhase::InsertShape(Shape *shape, const Bounds4 &aabb)
1711
{
1812
int id = Tree.Insert(aabb, shape);
1913
shape->broadPhaseIndex = id;
2014
BufferMove(id);
2115
}
2216

23-
void BroadPhase::RemoveShape(Shape* shape)
17+
void BroadPhase::RemoveShape(Shape *shape)
2418
{
2519
Tree.Remove(shape->broadPhaseIndex);
2620
}
@@ -45,7 +39,7 @@ namespace Forth
4539
for (size_t i = 0; i < PairBuffer.size(); ++i)
4640
{
4741
// Add contact to manager
48-
ContactPair pair = PairBuffer[i];
42+
ContactPair &pair = PairBuffer[i];
4943
Manager->AddContact(Tree.GetShape(pair.A), Tree.GetShape(pair.B));
5044
}
5145
}
@@ -75,7 +69,7 @@ namespace Forth
7569
int iA = Min(index, CurrentIndex);
7670
int iB = Max(index, CurrentIndex);
7771

78-
PairBuffer.push_back({ iA, iB });
72+
PairBuffer.push_back({iA, iB});
7973
}
8074

8175
} // namespace Physics

‎source/physics/broadphase/BroadPhase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Forth
1616
class BroadPhase : public ITreeCallback
1717
{
1818
public:
19-
BroadPhase() {}
19+
BroadPhase() {}
2020

2121
BroadPhase(class ContactManager *manager);
2222

@@ -37,7 +37,7 @@ namespace Forth
3737

3838
::std::vector<int> MoveBuffer;
3939

40-
DynamicTree Tree = DynamicTree();
40+
DynamicTree Tree;
4141

4242
int CurrentIndex;
4343

‎source/physics/broadphase/DynamicTree.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace Forth
5858
int id = stack.top();
5959
stack.pop();
6060

61-
Node n = Nodes[id];
61+
Node &n = Nodes[id];
6262

6363
if (IsIntersecting(aabb, n.aabb))
6464
{
@@ -93,7 +93,7 @@ namespace Forth
9393
if (id == -1)
9494
continue;
9595

96-
Node n = Nodes[id];
96+
Node &n = Nodes[id];
9797
if (!n.aabb.Raycast(p0, p1))
9898
continue;
9999

@@ -120,13 +120,13 @@ namespace Forth
120120
// Find the closest leaf
121121

122122
Vector4 c = Nodes[id].aabb.center();
123-
Node N = Nodes[Root], R, L, P;
123+
Node &N = Nodes[Root];
124124
int n = Root, r, l, p;
125125

126126
while (N.height > 0)
127127
{
128-
R = Nodes[r = N.right];
129-
L = Nodes[l = N.left];
128+
Node &R = Nodes[r = N.right];
129+
Node &L = Nodes[l = N.left];
130130
if (Compare(c, L.aabb, R.aabb) >= 0)
131131
{
132132
N = L;
@@ -140,7 +140,7 @@ namespace Forth
140140
}
141141

142142
{
143-
P = Nodes[p = AllocateNode()];
143+
Node &P = Nodes[p = AllocateNode()];
144144
int tmp;
145145
// Make P becomes parent of N and ID
146146
SWAP(P.left, N.left, tmp)
@@ -168,10 +168,10 @@ namespace Forth
168168
return;
169169
}
170170

171-
Node N = Nodes[id], P = Nodes[N.parent], S;
171+
Node &N = Nodes[id], &P = Nodes[N.parent];
172172
int s = (P.left == id) ? P.right : P.left, p = N.parent, tmp;
173173

174-
S = Nodes[s];
174+
Node &S = Nodes[s];
175175
// Make S become part of grandparent N, detach N & P
176176
SWAP(S.parent, P.parent, tmp)
177177

@@ -192,7 +192,7 @@ namespace Forth
192192
{
193193
while (index != -1)
194194
{
195-
Node n = Nodes[index];
195+
Node &n = Nodes[index];
196196

197197
n.height = Max(Nodes[n.left].height, Nodes[n.right].height) + 1;
198198
n.aabb = Combine(Nodes[n.left].aabb, Nodes[n.right].aabb);
@@ -202,7 +202,7 @@ namespace Forth
202202
}
203203
int DynamicTree::AllocateNode()
204204
{
205-
Node F = Nodes[FreeNode];
205+
Node &F = Nodes[FreeNode];
206206
int f = FreeNode;
207207

208208
if (F.next == NodesCap)
@@ -256,6 +256,6 @@ namespace Forth
256256
right = r;
257257
}
258258
DynamicTree::Node::Node() {}
259-
DynamicTree::Node::Node(int next) { this->next = next; }
259+
DynamicTree::Node::Node(int next) : Node() { this->next = next; }
260260
} // namespace Physics
261261
} // namespace Forth

‎source/physics/broadphase/DynamicTree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Forth
1111
{
1212
struct ITreeCallback
1313
{
14-
virtual void TreeCallback(int id);
14+
virtual void TreeCallback(int id)=0;
1515
};
1616

1717
/// Dynamic Bounds4 Tree

‎source/physics/collision/Algorithm.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ namespace Forth
55
{
66
namespace Physics
77
{
8+
Vector4 Algorithm::input[Common::MULTICONTACT_COUNT];
9+
Vector4 Algorithm::result[Common::MULTICONTACT_COUNT];
10+
Vector4 Algorithm::incident[8];
11+
812
//--------------------------------------------------------------------------------------------------
913
// Get nearest point from two edges, return true if parallel
1014

@@ -72,7 +76,7 @@ namespace Forth
7276
//--------------------------------------------------------------------------------------------------
7377
// Return (W-axis) oriented box extent & rotation necessary for clipping
7478

75-
void Algorithm::ComputeReferenceEdgesAndBasis(const Vector4 &eR, const Transform4& rtx, const Vector4 &n, int axis, Matrix4 &basis, Vector4 &e)
79+
void Algorithm::ComputeReferenceEdgesAndBasis(const Vector4 &eR, const Transform4 &rtx, const Vector4 &n, int axis, Matrix4 &basis, Vector4 &e)
7680
{
7781
Vector4 nr = n / rtx.rotation;
7882

@@ -141,7 +145,7 @@ namespace Forth
141145
// Get the most potential face candidate that Incidents N
142146
// That is: The face that most parallel to N
143147

144-
void Algorithm::ComputeIncidentFace(const Transform4& itx, const Vector4 &e, const Vector4 &n, Vector4 result[])
148+
void Algorithm::ComputeIncidentFace(const Transform4 &itx, const Vector4 &e, const Vector4 &n, Vector4 result[])
145149
{
146150
auto ni = (n / itx.rotation);
147151

‎source/physics/collision/Box.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

3-
#include "Shape.h"
43
#include "../dynamics/RaycastHit4.h"
4+
#include "Shape.h"
55

66
namespace Forth
77
{

‎source/physics/collision/Shape.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ namespace Forth
3434
this->type = type;
3535
}
3636

37-
class Body* body;
37+
class Body* body = NULL;
3838

39-
int hash;
39+
int hash = 0;
4040

41-
int broadPhaseIndex;
41+
int broadPhaseIndex = 0;
4242

4343
ShapeType type;
4444

@@ -68,7 +68,7 @@ namespace Forth
6868

6969
// -- Public accessor
7070

71-
void *Tag;
71+
void *Tag = NULL;
7272

7373
static Shape* CreateShape(ShapeType type);
7474
};

‎source/physics/collision/Sphere.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ namespace Forth
2424

2525
Sphere() : Shape(CSH_Sphere) {}
2626

27+
Sphere(float radius) : radius(radius), Shape(CSH_Sphere) {}
28+
2729
//float Radius{ get { return radius; } set { radius = value; if (body != NULL) body.flags |= BFL_DirtyMass; } }
2830
};
2931
} // namespace Physics

0 commit comments

Comments
 (0)