aboutsummaryrefslogtreecommitdiff
path: root/Client/Source
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-12-02 11:41:42 +0800
committerchai <chaifix@163.com>2021-12-02 11:41:42 +0800
commit7a798253441a8bd499d36bd8153b92ab08de8a9f (patch)
tree32e4ee995534a4a8cd7c2e0b530ff02c45217e8f /Client/Source
parentbcc11176480f403ab294de24d61bab993ce2fdfd (diff)
*fixed
Diffstat (limited to 'Client/Source')
-rw-r--r--Client/Source/Phy2DLite/Arbiter.cpp25
-rw-r--r--Client/Source/Phy2DLite/Arbiter.h3
-rw-r--r--Client/Source/Phy2DLite/Body.cpp45
-rw-r--r--Client/Source/Phy2DLite/Collide.cpp33
-rw-r--r--Client/Source/Phy2DLite/Constants.h19
-rw-r--r--Client/Source/Phy2DLite/Joint.cpp15
-rw-r--r--Client/Source/Phy2DLite/Math.cpp0
-rw-r--r--Client/Source/Phy2DLite/World.cpp12
-rw-r--r--Client/Source/float2fixed/main.cpp17
9 files changed, 105 insertions, 64 deletions
diff --git a/Client/Source/Phy2DLite/Arbiter.cpp b/Client/Source/Phy2DLite/Arbiter.cpp
index 4163154..567e6f7 100644
--- a/Client/Source/Phy2DLite/Arbiter.cpp
+++ b/Client/Source/Phy2DLite/Arbiter.cpp
@@ -2,6 +2,7 @@
#include "World.h"
#include "Body.h"
#include "Joint.h"
+#include "Constants.h"
using namespace Phy2D;
@@ -54,9 +55,9 @@ void Arbiter::Update(Contact* newContacts, int numNewContacts)
}
else
{
- c->Pn = 0.0f;
- c->Pt = 0.0f;
- c->Pnb = 0.0f;
+ c->Pn = _0;
+ c->Pt = _0;
+ c->Pnb = _0;
}
}
else
@@ -74,8 +75,8 @@ void Arbiter::Update(Contact* newContacts, int numNewContacts)
void Arbiter::PreStep(number inv_dt)
{
- const number k_allowedPenetration = 0.01f;
- number k_biasFactor = World::positionCorrection ? 0.2f : 0.0f;
+ const number k_allowedPenetration = _0_01;
+ number k_biasFactor = World::positionCorrection ? _0_2 : _0;
for (int i = 0; i < numContacts; ++i)
{
@@ -89,16 +90,16 @@ void Arbiter::PreStep(number inv_dt)
number rn2 = Dot(r2, c->normal);
number kNormal = body1->invMass + body2->invMass;
kNormal += body1->invI * (Dot(r1, r1) - rn1 * rn1) + body2->invI * (Dot(r2, r2) - rn2 * rn2);
- c->massNormal = (number) 1.0f / kNormal;
+ c->massNormal = (number)_1 / kNormal;
- Vec2 tangent = Cross(c->normal, 1.0f);
+ Vec2 tangent = Cross(c->normal, _1);
number rt1 = Dot(r1, tangent);
number rt2 = Dot(r2, tangent);
number kTangent = body1->invMass + body2->invMass;
kTangent += body1->invI * (Dot(r1, r1) - rt1 * rt1) + body2->invI * (Dot(r2, r2) - rt2 * rt2);
- c->massTangent = (number) 1.0f / kTangent;
+ c->massTangent = (number)_1 / kTangent;
- c->bias = -k_biasFactor * inv_dt * Min(0.0f, c->separation + k_allowedPenetration);
+ c->bias = -k_biasFactor * inv_dt * Min(_0, c->separation + k_allowedPenetration);
if (World::accumulateImpulses)
{
@@ -137,12 +138,12 @@ void Arbiter::ApplyImpulse()
{
// Clamp the accumulated impulse
number Pn0 = c->Pn;
- c->Pn = Max(Pn0 + dPn, 0.0f);
+ c->Pn = Max(Pn0 + dPn, _0);
dPn = c->Pn - Pn0;
}
else
{
- dPn = Max(dPn, 0.0f);
+ dPn = Max(dPn, _0);
}
// Apply contact impulse
@@ -157,7 +158,7 @@ void Arbiter::ApplyImpulse()
// 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.0f);
+ Vec2 tangent = Cross(c->normal, _1);
number vt = Dot(dv, tangent);
number dPt = c->massTangent * (-vt);
diff --git a/Client/Source/Phy2DLite/Arbiter.h b/Client/Source/Phy2DLite/Arbiter.h
index 2232210..167978f 100644
--- a/Client/Source/Phy2DLite/Arbiter.h
+++ b/Client/Source/Phy2DLite/Arbiter.h
@@ -1,6 +1,7 @@
#pragma once
#include "Math.h"
+#include "Constants.h"
namespace Phy2D
{
@@ -21,7 +22,7 @@ namespace Phy2D
struct Contact
{
- Contact() : Pn(0.0f), Pt(0.0f), Pnb(0.0f) {}
+ Contact() : Pn(_0), Pt(_0), Pnb(_0) {}
Vec2 position;
Vec2 normal;
diff --git a/Client/Source/Phy2DLite/Body.cpp b/Client/Source/Phy2DLite/Body.cpp
index 1d896fd..993c3f8 100644
--- a/Client/Source/Phy2DLite/Body.cpp
+++ b/Client/Source/Phy2DLite/Body.cpp
@@ -1,47 +1,48 @@
#include "Body.h"
+#include "Constants.h"
using namespace Phy2D;
Body::Body()
{
- position.Set(0.0f, 0.0f);
- rotation = 0.0f;
- velocity.Set(0.0f, 0.0f);
- angularVelocity = 0.0f;
- force.Set(0.0f, 0.0f);
- torque = 0.0f;
- friction = 0.2f;
+ position.Set(_0, _0);
+ rotation = _0;
+ velocity.Set(_0, _0);
+ angularVelocity = _0;
+ force.Set(_0, _0);
+ torque = _0;
+ friction = _0_2;
- width.Set(1.0f, 1.0f);
+ width.Set(_1, _1);
mass = NUMBER_MAX;
- invMass = 0.0f;
+ invMass = _0;
I = NUMBER_MAX;
- invI = 0.0f;
+ invI = _0;
}
void Body::Set(const Vec2& w, number m)
{
- position.Set(0.0f, 0.0f);
- rotation = 0.0f;
- velocity.Set(0.0f, 0.0f);
- angularVelocity = 0.0f;
- force.Set(0.0f, 0.0f);
- torque = 0.0f;
- friction = 0.2f;
+ position.Set(_0, _0);
+ rotation = _0;
+ velocity.Set(_0, _0);
+ angularVelocity = _0;
+ force.Set(_0, _0);
+ torque = _0;
+ friction = _0_2;
width = w;
mass = m;
if (mass < NUMBER_MAX)
{
- invMass = (number)1.0f / mass;
- I = mass * (width.x * width.x + width.y * width.y) / 12.0f;
- invI = (number)1.0f / I;
+ invMass = (number)_1 / mass;
+ I = mass * (width.x * width.x + width.y * width.y) / _12;
+ invI = (number)_1 / I;
}
else
{
- invMass = 0.0f;
+ invMass = _0;
I = NUMBER_MAX;
- invI = 0.0f;
+ invI = _0;
}
}
diff --git a/Client/Source/Phy2DLite/Collide.cpp b/Client/Source/Phy2DLite/Collide.cpp
index 6849c0e..116a72e 100644
--- a/Client/Source/Phy2DLite/Collide.cpp
+++ b/Client/Source/Phy2DLite/Collide.cpp
@@ -2,6 +2,7 @@
#include "Body.h"
#include "World.h"
#include "Joint.h"
+#include "Constants.h"
using namespace Phy2D;
@@ -58,16 +59,16 @@ int ClipSegmentToLine(ClipVertex vOut[2], ClipVertex vIn[2],
number distance1 = Dot(normal, vIn[1].v) - offset;
// If the points are behind the plane
- if (distance0 <= 0.0f) vOut[numOut++] = vIn[0];
- if (distance1 <= 0.0f) vOut[numOut++] = vIn[1];
+ if (distance0 <= _0) vOut[numOut++] = vIn[0];
+ if (distance1 <= _0) vOut[numOut++] = vIn[1];
// If the points are on different sides of the plane
- if (distance0 * distance1 < 0.0f)
+ if (distance0 * distance1 < _0)
{
// Find intersection point of edge and plane
number interp = distance0 / (distance0 - distance1);
vOut[numOut].v = vIn[0].v + interp * (vIn[1].v - vIn[0].v);
- if (distance0 > 0.0f)
+ if (distance0 > _0)
{
vOut[numOut].fp = vIn[0].fp;
vOut[numOut].fp.e.inEdge1 = clipEdge;
@@ -96,7 +97,7 @@ static void ComputeIncidentEdge(ClipVertex c[2], const Vec2& h, const Vec2& pos,
if (nAbs.x > nAbs.y)
{
- if (Sign(n.x) > 0.0f)
+ if (Sign(n.x) > _0)
{
c[0].v.Set(h.x, -h.y);
c[0].fp.e.inEdge2 = EDGE3;
@@ -119,7 +120,7 @@ static void ComputeIncidentEdge(ClipVertex c[2], const Vec2& h, const Vec2& pos,
}
else
{
- if (Sign(n.y) > 0.0f)
+ if (Sign(n.y) > _0)
{
c[0].v.Set(h.x, h.y);
c[0].fp.e.inEdge2 = EDGE4;
@@ -152,8 +153,8 @@ namespace Phy2D
int Collide(Contact* contacts, Body* bodyA, Body* bodyB)
{
// Setup
- Vec2 hA = 0.5f * bodyA->width;
- Vec2 hB = 0.5f * bodyB->width;
+ Vec2 hA = _0_5 * bodyA->width;
+ Vec2 hB = _0_5 * bodyB->width;
Vec2 posA = bodyA->position;
Vec2 posB = bodyB->position;
@@ -173,12 +174,12 @@ namespace Phy2D
// Box A faces
Vec2 faceA = Abs(dA) - hA - absC * hB;
- if (faceA.x > 0.0f || faceA.y > 0.0f)
+ if (faceA.x > _0 || faceA.y > _0)
return 0;
// Box B faces
Vec2 faceB = Abs(dB) - absCT * hA - hB;
- if (faceB.x > 0.0f || faceB.y > 0.0f)
+ if (faceB.x > _0 || faceB.y > _0)
return 0;
// Find best axis
@@ -189,16 +190,16 @@ namespace Phy2D
// Box A faces
axis = FACE_A_X;
separation = faceA.x;
- normal = dA.x > 0.0f ? RotA.col1 : -RotA.col1;
+ normal = dA.x > _0 ? RotA.col1 : -RotA.col1;
- const number relativeTol = 0.95f;
- const number absoluteTol = 0.01f;
+ const number relativeTol = _0_95;
+ const number absoluteTol = _0_01;
if (faceA.y > relativeTol * separation + absoluteTol * hA.y)
{
axis = FACE_A_Y;
separation = faceA.y;
- normal = dA.y > 0.0f ? RotA.col2 : -RotA.col2;
+ normal = dA.y > _0 ? RotA.col2 : -RotA.col2;
}
// Box B faces
@@ -206,14 +207,14 @@ namespace Phy2D
{
axis = FACE_B_X;
separation = faceB.x;
- normal = dB.x > 0.0f ? RotB.col1 : -RotB.col1;
+ normal = dB.x > _0 ? RotB.col1 : -RotB.col1;
}
if (faceB.y > relativeTol * separation + absoluteTol * hB.y)
{
axis = FACE_B_Y;
separation = faceB.y;
- normal = dB.y > 0.0f ? RotB.col2 : -RotB.col2;
+ normal = dB.y > _0 ? RotB.col2 : -RotB.col2;
}
// Setup clipping plane data based on the separating axis
diff --git a/Client/Source/Phy2DLite/Constants.h b/Client/Source/Phy2DLite/Constants.h
new file mode 100644
index 0000000..aa50e8c
--- /dev/null
+++ b/Client/Source/Phy2DLite/Constants.h
@@ -0,0 +1,19 @@
+#pragma once
+
+#include "Settings.h"
+
+namespace Phy2D
+{
+#if NUMBER_ALIAS == NUMBER_FPM
+#define CONSTANT(name, value) static number name = number::from_raw_value(value)
+
+ CONSTANT(_1, 65536);
+ CONSTANT(_0, 0);
+ CONSTANT(_0_01, 655);
+ CONSTANT(_0_2, 13107);
+ CONSTANT(_0_5, 32768);
+ CONSTANT(_0_95, 62259);
+ CONSTANT(_12, 786432);
+
+#endif
+} \ No newline at end of file
diff --git a/Client/Source/Phy2DLite/Joint.cpp b/Client/Source/Phy2DLite/Joint.cpp
index 95f7c64..71375d0 100644
--- a/Client/Source/Phy2DLite/Joint.cpp
+++ b/Client/Source/Phy2DLite/Joint.cpp
@@ -1,6 +1,7 @@
#include "Joint.h"
#include "Body.h"
#include "World.h"
+#include "Constants.h"
using namespace Phy2D;
@@ -17,10 +18,10 @@ void Joint::Set(Body* b1, Body* b2, const Vec2& anchor)
localAnchor1 = Rot1T * (anchor - body1->position);
localAnchor2 = Rot2T * (anchor - body2->position);
- P.Set(0.0f, 0.0f);
+ P.Set(_0, _0);
- softness = 0.0f;
- biasFactor = 0.2f;
+ softness = _0;
+ biasFactor = _0_2;
}
void Joint::PreStep(number inv_dt)
@@ -37,8 +38,8 @@ void Joint::PreStep(number inv_dt)
// = [1/m1+1/m2 0 ] + invI1 * [r1.y*r1.y -r1.x*r1.y] + invI2 * [r1.y*r1.y -r1.x*r1.y]
// [ 0 1/m1+1/m2] [-r1.x*r1.y r1.x*r1.x] [-r1.x*r1.y r1.x*r1.x]
Mat22 K1;
- K1.col1.x = body1->invMass + body2->invMass; K1.col2.x = 0.0f;
- K1.col1.y = 0.0f; K1.col2.y = body1->invMass + body2->invMass;
+ K1.col1.x = body1->invMass + body2->invMass; K1.col2.x = _0;
+ K1.col1.y = _0; K1.col2.y = body1->invMass + body2->invMass;
Mat22 K2;
K2.col1.x = body1->invI * r1.y * r1.y; K2.col2.x = -body1->invI * r1.x * r1.y;
@@ -64,7 +65,7 @@ void Joint::PreStep(number inv_dt)
}
else
{
- bias.Set(0.0f, 0.0f);
+ bias.Set(_0, _0);
}
if (World::warmStarting)
@@ -78,7 +79,7 @@ void Joint::PreStep(number inv_dt)
}
else
{
- P.Set(0.0f, 0.0f);
+ P.Set(_0, _0);
}
}
diff --git a/Client/Source/Phy2DLite/Math.cpp b/Client/Source/Phy2DLite/Math.cpp
deleted file mode 100644
index e69de29..0000000
--- a/Client/Source/Phy2DLite/Math.cpp
+++ /dev/null
diff --git a/Client/Source/Phy2DLite/World.cpp b/Client/Source/Phy2DLite/World.cpp
index 4f48b69..8852f1f 100644
--- a/Client/Source/Phy2DLite/World.cpp
+++ b/Client/Source/Phy2DLite/World.cpp
@@ -2,10 +2,10 @@
#include "Body.h"
#include "Joint.h"
#include "Arbiter.h"
+#include "Constants.h"
using namespace Phy2D;
-
using std::vector;
using std::map;
using std::pair;
@@ -45,7 +45,7 @@ void World::BroadPhase()
{
Body* bj = bodies[j];
- if (bi->invMass == 0.0f && bj->invMass == 0.0f)
+ if (bi->invMass == _0 && bj->invMass == _0)
continue;
Arbiter newArb(bi, bj);
@@ -73,7 +73,7 @@ void World::BroadPhase()
void World::Step(number dt)
{
- number inv_dt = dt > 0.0f ? (number)1.0f / dt : (number)0.0f;
+ number inv_dt = dt > _0 ? _1 / dt : _0;
// Determine overlapping bodies and update contact points.
BroadPhase();
@@ -83,7 +83,7 @@ void World::Step(number dt)
{
Body* b = bodies[i];
- if (b->invMass == 0.0f)
+ if (b->invMass == _0)
continue;
b->velocity += dt * (gravity + b->invMass * b->force);
@@ -123,7 +123,7 @@ void World::Step(number dt)
b->position += dt * b->velocity;
b->rotation += dt * b->angularVelocity;
- b->force.Set(0.0f, 0.0f);
- b->torque = 0.0f;
+ b->force.Set(_0, _0);
+ b->torque = _0;
}
}
diff --git a/Client/Source/float2fixed/main.cpp b/Client/Source/float2fixed/main.cpp
new file mode 100644
index 0000000..5c4fa40
--- /dev/null
+++ b/Client/Source/float2fixed/main.cpp
@@ -0,0 +1,17 @@
+#include "fpm/include/fpm/fixed.hpp"
+#include "fpm/include/fpm/ios.hpp"
+#include <iostream>
+
+using namespace std;
+
+typedef fpm::fixed_16_16 fixed16;
+
+int main()
+{
+ fixed16 n = 0.95f;
+ cout << n.raw_value();
+
+ getchar();
+
+ return 0;
+} \ No newline at end of file