From df0fea1f5f2953b879fe433a651bc58fa512bd89 Mon Sep 17 00:00:00 2001 From: Ketmar Dark Date: Mon, 27 Jun 2016 02:14:12 +0300 Subject: [PATCH] adapted to new vmath --- b2dlite/arbiter.d | 92 ++++++++-------- b2dlite/bbody.d | 76 +++++++------- b2dlite/collide.d | 82 +++++++-------- b2dlite/joint.d | 42 ++++---- b2dlite/mathutils.d | 42 ++++---- b2dlite/package.d | 2 +- b2dlite/world.d | 16 +-- xmain.d | 298 ++++++++++++++++++++++++++-------------------------- 8 files changed, 327 insertions(+), 323 deletions(-) diff --git a/b2dlite/arbiter.d b/b2dlite/arbiter.d index 735cf02..fa81cbf 100644 --- a/b2dlite/arbiter.d +++ b/b2dlite/arbiter.d @@ -28,15 +28,15 @@ union FeaturePair { struct Contact { public: - vec2 position; - vec2 normal; - vec2 r1, r2; - Float separation = FloatNum!(0.0); - Float Pn = FloatNum!(0.0); // accumulated normal impulse - Float Pt = FloatNum!(0.0); // accumulated tangent impulse - Float Pnb = FloatNum!(0.0); // accumulated normal impulse for position bias - Float massNormal, massTangent; - Float bias = FloatNum!(0.0); + Vec2 position; + Vec2 normal; + Vec2 r1, r2; + VFloat separation = VFloatNum!(0.0); + VFloat Pn = VFloatNum!(0.0); // accumulated normal impulse + VFloat Pt = VFloatNum!(0.0); // accumulated tangent impulse + VFloat Pnb = VFloatNum!(0.0); // accumulated normal impulse for position bias + VFloat massNormal, massTangent; + VFloat bias = VFloatNum!(0.0); FeaturePair feature; } @@ -55,7 +55,7 @@ public: Body body2; // combined friction - Float friction; + VFloat friction; public: this () {} @@ -77,12 +77,12 @@ public: /* import iv.glbinds; - glPointSize(FloatNum!(4.0)); - glColor3f(FloatNum!(1.0), FloatNum!(0.0), FloatNum!(0.0)); + glPointSize(VFloatNum!(4.0)); + glColor3f(VFloatNum!(1.0), VFloatNum!(0.0), VFloatNum!(0.0)); glBegin(GL_POINTS); for (int i = 0; i < numContacts; ++i) glVertex2f(contacts[i].position.x, contacts[i].position.y); glEnd(); - glPointSize(FloatNum!(1.0)); + glPointSize(VFloatNum!(1.0)); */ } @@ -106,9 +106,9 @@ public: c.Pt = cOld.Pt; c.Pnb = cOld.Pnb; } else { - c.Pn = FloatNum!(0.0); - c.Pt = FloatNum!(0.0); - c.Pnb = FloatNum!(0.0); + c.Pn = VFloatNum!(0.0); + c.Pt = VFloatNum!(0.0); + c.Pnb = VFloatNum!(0.0); } } else { mergedContacts[i] = newContacts[i]; @@ -118,38 +118,38 @@ public: numContacts = numNewContacts; } - void PreStep (Float inv_dt) { + void PreStep (VFloat inv_dt) { import b2dlite.world : World; import std.algorithm : min; - enum k_allowedPenetration = FloatNum!(0.01); - Float k_biasFactor = (World.positionCorrection ? FloatNum!(0.2) : FloatNum!(0.0)); + enum k_allowedPenetration = VFloatNum!(0.01); + VFloat k_biasFactor = (World.positionCorrection ? VFloatNum!(0.2) : VFloatNum!(0.0)); for (int i = 0; i < numContacts; ++i) { Contact *c = contacts.ptr+i; - vec2 r1 = c.position-body1.position; - vec2 r2 = c.position-body2.position; + Vec2 r1 = c.position-body1.position; + Vec2 r2 = c.position-body2.position; // precompute normal mass, tangent mass, and bias - Float rn1 = r1*c.normal; //Dot(r1, c.normal); - Float rn2 = r2*c.normal; //Dot(r2, c.normal); - Float kNormal = body1.invMass+body2.invMass; + VFloat rn1 = r1*c.normal; //Dot(r1, c.normal); + VFloat rn2 = r2*c.normal; //Dot(r2, c.normal); + VFloat kNormal = body1.invMass+body2.invMass; //kNormal += body1.invI*(Dot(r1, r1)-rn1*rn1)+body2.invI*(Dot(r2, r2)-rn2*rn2); kNormal += body1.invI*((r1*r1)-rn1*rn1)+body2.invI*((r2*r2)-rn2*rn2); - c.massNormal = FloatNum!(1.0)/kNormal; + c.massNormal = VFloatNum!(1.0)/kNormal; - vec2 tangent = Cross(c.normal, FloatNum!(1.0)); - Float rt1 = r1*tangent; //Dot(r1, tangent); - Float rt2 = r2*tangent; //Dot(r2, tangent); - Float kTangent = body1.invMass+body2.invMass; + Vec2 tangent = Cross(c.normal, VFloatNum!(1.0)); + VFloat rt1 = r1*tangent; //Dot(r1, tangent); + VFloat rt2 = r2*tangent; //Dot(r2, tangent); + VFloat kTangent = body1.invMass+body2.invMass; //kTangent += body1.invI*(Dot(r1, r1)-rt1*rt1)+body2.invI*(Dot(r2, r2)-rt2*rt2); kTangent += body1.invI*((r1*r1)-rt1*rt1)+body2.invI*((r2*r2)-rt2*rt2); - c.massTangent = FloatNum!(1.0)/kTangent; + c.massTangent = VFloatNum!(1.0)/kTangent; - c.bias = -k_biasFactor*inv_dt*min(FloatNum!(0.0), c.separation+k_allowedPenetration); + c.bias = -k_biasFactor*inv_dt*min(VFloatNum!(0.0), c.separation+k_allowedPenetration); if (World.accumulateImpulses) { // apply normal + friction impulse - vec2 P = c.Pn*c.normal+c.Pt*tangent; + Vec2 P = c.Pn*c.normal+c.Pt*tangent; body1.velocity -= body1.invMass*P; body1.angularVelocity -= body1.invI*Cross(r1, P); @@ -173,24 +173,24 @@ public: 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); + Vec2 dv = b2.velocity+Cross(b2.angularVelocity, c.r2)-b1.velocity-Cross(b1.angularVelocity, c.r1); // compute normal impulse - Float vn = dv*c.normal; //Dot(dv, c.normal); + VFloat vn = dv*c.normal; //Dot(dv, c.normal); - Float dPn = c.massNormal*(-vn+c.bias); + VFloat dPn = c.massNormal*(-vn+c.bias); if (World.accumulateImpulses) { // clamp the accumulated impulse - Float Pn0 = c.Pn; - c.Pn = max(Pn0+dPn, FloatNum!(0.0)); + VFloat Pn0 = c.Pn; + c.Pn = max(Pn0+dPn, VFloatNum!(0.0)); dPn = c.Pn-Pn0; } else { - dPn = max(dPn, FloatNum!(0.0)); + dPn = max(dPn, VFloatNum!(0.0)); } // apply contact impulse - vec2 Pn = dPn*c.normal; + Vec2 Pn = dPn*c.normal; b1.velocity -= b1.invMass*Pn; b1.angularVelocity -= b1.invI*Cross(c.r1, Pn); @@ -201,24 +201,24 @@ public: // relative velocity at contact dv = b2.velocity+Cross(b2.angularVelocity, c.r2)-b1.velocity-Cross(b1.angularVelocity, c.r1); - vec2 tangent = Cross(c.normal, FloatNum!(1.0)); - Float vt = dv*tangent; //Dot(dv, tangent); - Float dPt = c.massTangent*(-vt); + Vec2 tangent = Cross(c.normal, VFloatNum!(1.0)); + VFloat vt = dv*tangent; //Dot(dv, tangent); + VFloat dPt = c.massTangent*(-vt); if (World.accumulateImpulses) { // compute friction impulse - Float maxPt = friction*c.Pn; + VFloat maxPt = friction*c.Pn; // clamp friction - Float oldTangentImpulse = c.Pt; + VFloat oldTangentImpulse = c.Pt; c.Pt = Clamp(oldTangentImpulse+dPt, -maxPt, maxPt); dPt = c.Pt-oldTangentImpulse; } else { - Float maxPt = friction*dPn; + VFloat maxPt = friction*dPn; dPt = Clamp(dPt, -maxPt, maxPt); } // apply contact impulse - vec2 Pt = dPt*tangent; + Vec2 Pt = dPt*tangent; b1.velocity -= b1.invMass*Pt; b1.angularVelocity -= b1.invI*Cross(c.r1, Pt); diff --git a/b2dlite/bbody.d b/b2dlite/bbody.d index 79ab57e..6902c55 100644 --- a/b2dlite/bbody.d +++ b/b2dlite/bbody.d @@ -16,60 +16,60 @@ import b2dlite.mathutils; class Body { public: - vec2 position; - Float rotation; + Vec2 position; + VFloat rotation; - vec2 velocity; - Float angularVelocity; + Vec2 velocity; + VFloat angularVelocity; - vec2 force; - Float torque; + Vec2 force; + VFloat torque; - vec2 width; + Vec2 width; - Float friction; - Float mass, invMass; - Float I, invI; + VFloat friction; + VFloat mass, invMass; + VFloat I, invI; public: this () { - position.set(FloatNum!(0.0), FloatNum!(0.0)); - rotation = FloatNum!(0.0); - velocity.set(FloatNum!(0.0), FloatNum!(0.0)); - angularVelocity = FloatNum!(0.0); - force.set(FloatNum!(0.0), FloatNum!(0.0)); - torque = FloatNum!(0.0); - friction = FloatNum!(0.2); + position.set(VFloatNum!(0.0), VFloatNum!(0.0)); + rotation = VFloatNum!(0.0); + velocity.set(VFloatNum!(0.0), VFloatNum!(0.0)); + angularVelocity = VFloatNum!(0.0); + force.set(VFloatNum!(0.0), VFloatNum!(0.0)); + torque = VFloatNum!(0.0); + friction = VFloatNum!(0.2); - width.set(FloatNum!(1.0), FloatNum!(1.0)); - mass = Float.max; - invMass = FloatNum!(0.0); - I = Float.max; - invI = FloatNum!(0.0); + width.set(VFloatNum!(1.0), VFloatNum!(1.0)); + mass = VFloat.max; + invMass = VFloatNum!(0.0); + I = VFloat.max; + invI = VFloatNum!(0.0); } - void set() (in auto ref vec2 w, Float m) { - position.set(FloatNum!(0.0), FloatNum!(0.0)); - rotation = FloatNum!(0.0); - velocity.set(FloatNum!(0.0), FloatNum!(0.0)); - angularVelocity = FloatNum!(0.0); - force.set(FloatNum!(0.0), FloatNum!(0.0)); - torque = FloatNum!(0.0); - friction = FloatNum!(0.2); + void set() (in auto ref Vec2 w, VFloat m) { + position.set(VFloatNum!(0.0), VFloatNum!(0.0)); + rotation = VFloatNum!(0.0); + velocity.set(VFloatNum!(0.0), VFloatNum!(0.0)); + angularVelocity = VFloatNum!(0.0); + force.set(VFloatNum!(0.0), VFloatNum!(0.0)); + torque = VFloatNum!(0.0); + friction = VFloatNum!(0.2); width = w; mass = m; - if (mass < Float.max) { - invMass = FloatNum!(1.0)/mass; - I = mass*(width.x*width.x+width.y*width.y)/FloatNum!(12.0); - invI = FloatNum!(1.0)/I; + if (mass < VFloat.max) { + invMass = VFloatNum!(1.0)/mass; + I = mass*(width.x*width.x+width.y*width.y)/VFloatNum!(12.0); + invI = VFloatNum!(1.0)/I; } else { - invMass = FloatNum!(0.0); - I = Float.max; - invI = FloatNum!(0.0); + invMass = VFloatNum!(0.0); + I = VFloat.max; + invI = VFloatNum!(0.0); } } - void AddForce() (in auto ref vec2 f) { pragma(inline, true); force += f; } + void AddForce() (in auto ref Vec2 f) { pragma(inline, true); force += f; } int opCmp() (Body b) pure const nothrow @trusted @nogc { if (b is null) return 1; diff --git a/b2dlite/collide.d b/b2dlite/collide.d index 7ca6e94..f660862 100644 --- a/b2dlite/collide.d +++ b/b2dlite/collide.d @@ -47,7 +47,7 @@ enum EdgeNumbers { struct ClipVertex { - vec2 v; + Vec2 v; FeaturePair fp; } @@ -58,21 +58,21 @@ private void Flip (ref FeaturePair fp) { } -private int ClipSegmentToLine() (ClipVertex[/*2*/] vOut, ClipVertex[/*2*/] vIn, in auto ref vec2 normal, Float offset, char clipEdge) { +private int ClipSegmentToLine() (ClipVertex[/*2*/] vOut, ClipVertex[/*2*/] vIn, in auto ref Vec2 normal, VFloat offset, char clipEdge) { // start with no output points int numOut = 0; // calculate the distance of end points to the line - Float distance0 = /*Dot*/(normal*vIn[0].v)-offset; - Float distance1 = /*Dot*/(normal*vIn[1].v)-offset; + VFloat distance0 = /*Dot*/(normal*vIn[0].v)-offset; + VFloat distance1 = /*Dot*/(normal*vIn[1].v)-offset; // if the points are behind the plane - if (distance0 <= FloatNum!(0.0)) vOut[numOut++] = vIn[0]; - if (distance1 <= FloatNum!(0.0)) vOut[numOut++] = vIn[1]; + if (distance0 <= VFloatNum!(0.0)) vOut[numOut++] = vIn[0]; + if (distance1 <= VFloatNum!(0.0)) vOut[numOut++] = vIn[1]; // if the points are on different sides of the plane - if (distance0*distance1 < FloatNum!(0.0)) { + if (distance0*distance1 < VFloatNum!(0.0)) { // find intersection point of edge and plane - Float interp = distance0/(distance0-distance1); + VFloat interp = distance0/(distance0-distance1); vOut[numOut].v = vIn[0].v+interp*(vIn[1].v-vIn[0].v); - if (distance0 > FloatNum!(0.0)) { + if (distance0 > VFloatNum!(0.0)) { vOut[numOut].fp = vIn[0].fp; vOut[numOut].fp.e.inEdge1 = clipEdge; vOut[numOut].fp.e.inEdge2 = EdgeNumbers.NO_EDGE; @@ -87,13 +87,13 @@ private int ClipSegmentToLine() (ClipVertex[/*2*/] vOut, ClipVertex[/*2*/] vIn, } -private void ComputeIncidentEdge() (ClipVertex[/*2*/] c, in auto ref vec2 h, in auto ref vec2 pos, in auto ref Mat22 Rot, in auto ref vec2 normal) { +private void ComputeIncidentEdge() (ClipVertex[/*2*/] c, in auto ref Vec2 h, in auto ref Vec2 pos, in auto ref Mat22 Rot, in auto ref Vec2 normal) { // the normal is from the reference box; convert it to the incident boxe's frame and flip sign Mat22 RotT = Rot.transpose(); - vec2 n = -(RotT*normal); - vec2 nAbs = n.abs; + Vec2 n = -(RotT*normal); + Vec2 nAbs = n.abs; if (nAbs.x > nAbs.y) { - if (Sign(n.x) > FloatNum!(0.0)) { + if (Sign(n.x) > VFloatNum!(0.0)) { c[0].v.set(h.x, -h.y); c[0].fp.e.inEdge2 = EdgeNumbers.EDGE3; c[0].fp.e.outEdge2 = EdgeNumbers.EDGE4; @@ -111,7 +111,7 @@ private void ComputeIncidentEdge() (ClipVertex[/*2*/] c, in auto ref vec2 h, in c[1].fp.e.outEdge2 = EdgeNumbers.EDGE3; } } else { - if (Sign(n.y) > FloatNum!(0.0)) { + if (Sign(n.y) > VFloatNum!(0.0)) { c[0].v.set(h.x, h.y); c[0].fp.e.inEdge2 = EdgeNumbers.EDGE4; c[0].fp.e.outEdge2 = EdgeNumbers.EDGE1; @@ -138,11 +138,11 @@ private void ComputeIncidentEdge() (ClipVertex[/*2*/] c, in auto ref vec2 h, in // the normal points from A to B int Collide (Contact* contacts, Body bodyA, Body bodyB) { // setup - vec2 hA = FloatNum!(0.5)*bodyA.width; - vec2 hB = FloatNum!(0.5)*bodyB.width; + Vec2 hA = VFloatNum!(0.5)*bodyA.width; + Vec2 hB = VFloatNum!(0.5)*bodyB.width; - vec2 posA = bodyA.position; - vec2 posB = bodyB.position; + Vec2 posA = bodyA.position; + Vec2 posB = bodyB.position; auto RotA = Mat22 (bodyA.rotation); auto RotB = Mat22 (bodyB.rotation); @@ -150,61 +150,61 @@ int Collide (Contact* contacts, Body bodyA, Body bodyB) { Mat22 RotAT = RotA.transpose(); Mat22 RotBT = RotB.transpose(); - //k8:vec2 a1 = RotA.col1, a2 = RotA.col2; - //k8:vec2 b1 = RotB.col1, b2 = RotB.col2; + //k8:Vec2 a1 = RotA.col1, a2 = RotA.col2; + //k8:Vec2 b1 = RotB.col1, b2 = RotB.col2; - vec2 dp = posB-posA; - vec2 dA = RotAT*dp; - vec2 dB = RotBT*dp; + Vec2 dp = posB-posA; + Vec2 dA = RotAT*dp; + Vec2 dB = RotBT*dp; Mat22 C = RotAT*RotB; Mat22 absC = C.abs; Mat22 absCT = absC.transpose(); // Box A faces - vec2 faceA = dA.abs-hA-absC*hB; - if (faceA.x > FloatNum!(0.0) || faceA.y > FloatNum!(0.0)) return 0; + Vec2 faceA = dA.abs-hA-absC*hB; + if (faceA.x > VFloatNum!(0.0) || faceA.y > VFloatNum!(0.0)) return 0; // Box B faces - vec2 faceB = dB.abs-absCT*hA-hB; - if (faceB.x > FloatNum!(0.0) || faceB.y > FloatNum!(0.0)) return 0; + Vec2 faceB = dB.abs-absCT*hA-hB; + if (faceB.x > VFloatNum!(0.0) || faceB.y > VFloatNum!(0.0)) return 0; // Find best axis Axis axis; - Float separation; - vec2 normal; + VFloat separation; + Vec2 normal; // Box A faces axis = Axis.FACE_A_X; separation = faceA.x; - normal = dA.x > FloatNum!(0.0) ? RotA.col1 : -RotA.col1; + normal = dA.x > VFloatNum!(0.0) ? RotA.col1 : -RotA.col1; - const Float relativeTol = FloatNum!(0.95); - const Float absoluteTol = FloatNum!(0.01); + const VFloat relativeTol = VFloatNum!(0.95); + const VFloat absoluteTol = VFloatNum!(0.01); if (faceA.y > relativeTol*separation+absoluteTol*hA.y) { axis = Axis.FACE_A_Y; separation = faceA.y; - normal = dA.y > FloatNum!(0.0) ? RotA.col2 : -RotA.col2; + normal = dA.y > VFloatNum!(0.0) ? RotA.col2 : -RotA.col2; } // Box B faces if (faceB.x > relativeTol*separation+absoluteTol*hB.x) { axis = Axis.FACE_B_X; separation = faceB.x; - normal = dB.x > FloatNum!(0.0) ? RotB.col1 : -RotB.col1; + normal = dB.x > VFloatNum!(0.0) ? RotB.col1 : -RotB.col1; } if (faceB.y > relativeTol*separation+absoluteTol*hB.y) { axis = Axis.FACE_B_Y; separation = faceB.y; - normal = dB.y > FloatNum!(0.0) ? RotB.col2 : -RotB.col2; + normal = dB.y > VFloatNum!(0.0) ? RotB.col2 : -RotB.col2; } // Setup clipping plane data based on the separating axis - vec2 frontNormal, sideNormal; + Vec2 frontNormal, sideNormal; ClipVertex[2] incidentEdge; - Float front, negSide, posSide; + VFloat front, negSide, posSide; char negEdge, posEdge; // Compute the clipping lines and the line segment to be clipped. @@ -213,7 +213,7 @@ int Collide (Contact* contacts, Body bodyA, Body bodyB) { frontNormal = normal; front = /*Dot*/(posA*frontNormal)+hA.x; sideNormal = RotA.col2; - Float side = /*Dot*/(posA*sideNormal); + VFloat side = /*Dot*/(posA*sideNormal); negSide = -side+hA.y; posSide = side+hA.y; negEdge = EdgeNumbers.EDGE3; @@ -224,7 +224,7 @@ int Collide (Contact* contacts, Body bodyA, Body bodyB) { frontNormal = normal; front = /*Dot*/(posA*frontNormal)+hA.y; sideNormal = RotA.col1; - Float side = /*Dot*/(posA*sideNormal); + VFloat side = /*Dot*/(posA*sideNormal); negSide = -side+hA.x; posSide = side+hA.x; negEdge = EdgeNumbers.EDGE2; @@ -235,7 +235,7 @@ int Collide (Contact* contacts, Body bodyA, Body bodyB) { frontNormal = -normal; front = /*Dot*/(posB*frontNormal)+hB.x; sideNormal = RotB.col2; - Float side = /*Dot*/(posB*sideNormal); + VFloat side = /*Dot*/(posB*sideNormal); negSide = -side+hB.y; posSide = side+hB.y; negEdge = EdgeNumbers.EDGE3; @@ -246,7 +246,7 @@ int Collide (Contact* contacts, Body bodyA, Body bodyB) { frontNormal = -normal; front = /*Dot*/(posB*frontNormal)+hB.y; sideNormal = RotB.col1; - Float side = /*Dot*/(posB*sideNormal); + VFloat side = /*Dot*/(posB*sideNormal); negSide = -side+hB.x; posSide = side+hB.x; negEdge = EdgeNumbers.EDGE2; diff --git a/b2dlite/joint.d b/b2dlite/joint.d index 9365845..e45962f 100644 --- a/b2dlite/joint.d +++ b/b2dlite/joint.d @@ -17,19 +17,19 @@ class Joint { private import b2dlite.bbody : Body; public: Mat22 M; - vec2 localAnchor1, localAnchor2; - vec2 r1, r2; - vec2 bias; - vec2 P; // accumulated impulse + Vec2 localAnchor1, localAnchor2; + Vec2 r1, r2; + Vec2 bias; + Vec2 P; // accumulated impulse Body body1; Body body2; - Float biasFactor = FloatNum!(0.2); - Float softness = FloatNum!(0.0); + VFloat biasFactor = VFloatNum!(0.2); + VFloat softness = VFloatNum!(0.0); public: - //Joint () : P(FloatNum!(0.0), FloatNum!(0.0)), body1(0), body2(0), biasFactor(FloatNum!(0.2)), softness(FloatNum!(0.0)) {} + //Joint () : P(VFloatNum!(0.0), VFloatNum!(0.0)), body1(0), body2(0), biasFactor(VFloatNum!(0.2)), softness(VFloatNum!(0.0)) {} - void set() (Body b1, Body b2, in auto ref vec2 anchor) { + void set() (Body b1, Body b2, in auto ref Vec2 anchor) { body1 = b1; body2 = b2; @@ -41,13 +41,13 @@ public: localAnchor1 = Rot1T*(anchor-body1.position); localAnchor2 = Rot2T*(anchor-body2.position); - P.set(FloatNum!(0.0), FloatNum!(0.0)); + P.set(VFloatNum!(0.0), VFloatNum!(0.0)); - softness = FloatNum!(0.0); - biasFactor = FloatNum!(0.2); + softness = VFloatNum!(0.0); + biasFactor = VFloatNum!(0.2); } - void PreStep (Float inv_dt) { + void PreStep (VFloat inv_dt) { import b2dlite.world : World; // pre-compute anchors, mass matrix, and bias @@ -62,8 +62,8 @@ public: // = [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 = FloatNum!(0.0); - K1.col1.y = FloatNum!(0.0); K1.col2.y = body1.invMass+body2.invMass; + K1.col1.x = body1.invMass+body2.invMass; K1.col2.x = VFloatNum!(0.0); + K1.col1.y = VFloatNum!(0.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; @@ -79,14 +79,14 @@ public: M = K.invert(); - vec2 p1 = body1.position+r1; - vec2 p2 = body2.position+r2; - vec2 dp = p2-p1; + Vec2 p1 = body1.position+r1; + Vec2 p2 = body2.position+r2; + Vec2 dp = p2-p1; if (World.positionCorrection) { bias = -biasFactor*inv_dt*dp; } else { - bias.set(FloatNum!(0.0), FloatNum!(0.0)); + bias.set(VFloatNum!(0.0), VFloatNum!(0.0)); } if (World.warmStarting) { @@ -97,13 +97,13 @@ public: body2.velocity += body2.invMass*P; body2.angularVelocity += body2.invI*Cross(r2, P); } else { - P.set(FloatNum!(0.0), FloatNum!(0.0)); + P.set(VFloatNum!(0.0), VFloatNum!(0.0)); } } void ApplyImpulse () { - vec2 dv = body2.velocity+Cross(body2.angularVelocity, r2)-body1.velocity-Cross(body1.angularVelocity, r1); - vec2 impulse = M*(bias-dv-softness*P); + Vec2 dv = body2.velocity+Cross(body2.angularVelocity, r2)-body1.velocity-Cross(body1.angularVelocity, r1); + Vec2 impulse = M*(bias-dv-softness*P); body1.velocity -= body1.invMass*impulse; body1.angularVelocity -= body1.invI*Cross(r1, impulse); diff --git a/b2dlite/mathutils.d b/b2dlite/mathutils.d index 337ebf2..655e5ea 100644 --- a/b2dlite/mathutils.d +++ b/b2dlite/mathutils.d @@ -12,32 +12,36 @@ module b2dlite.mathutils; import iv.vmath; +alias Vec2 = VecN!(2, float); +alias VFloat = Vec2.VFloat; +alias VFloatNum = Vec2.VFloatNum; + struct Mat22 { pure nothrow @safe @nogc: public: - vec2 col1, col2; + Vec2 col1, col2; public: - this (Float angle) { + this (VFloat angle) { pragma(inline, true); import std.math : cos, sin; - immutable Float c = cos(angle), s = sin(angle); + immutable VFloat c = cos(angle), s = sin(angle); col1.x = c; col1.y = s; col2.x = -s; col2.y = c; } - this() (in auto ref vec2 acol1, in auto ref vec2 acol2) { pragma(inline, true); col1 = acol1; col2 = acol2; } + this() (in auto ref Vec2 acol1, in auto ref Vec2 acol2) { pragma(inline, true); col1 = acol1; col2 = acol2; } - Mat22 transpose () const { pragma(inline, true); return Mat22(vec2(col1.x, col2.x), vec2(col1.y, col2.y)); } + Mat22 transpose () const { pragma(inline, true); return Mat22(Vec2(col1.x, col2.x), Vec2(col1.y, col2.y)); } Mat22 invert () const { pragma(inline, true); - immutable Float a = col1.x, b = col2.x, c = col1.y, d = col2.y; + immutable VFloat a = col1.x, b = col2.x, c = col1.y, d = col2.y; Mat22 B; - Float det = a*d-b*c; - assert(det != FloatNum!(0.0)); - det = FloatNum!(1.0)/det; + VFloat det = a*d-b*c; + assert(det != VFloatNum!(0.0)); + det = VFloatNum!(1.0)/det; B.col1.x = det*d; B.col2.x = -det*b; B.col1.y = -det*c; @@ -45,7 +49,7 @@ public: return B; } - vec2 opBinary(string op:"*") (in auto ref vec2 v) const { pragma(inline, true); return vec2(col1.x*v.x+col2.x*v.y, col1.y*v.x+col2.y*v.y); } + Vec2 opBinary(string op:"*") (in auto ref Vec2 v) const { pragma(inline, true); return Vec2(col1.x*v.x+col2.x*v.y, col1.y*v.x+col2.y*v.y); } Mat22 opBinary(string op:"+") (in auto ref Mat22 B) const { pragma(inline, true); return Mat22(col1+B.col1, col2+B.col2); } Mat22 opBinary(string op:"*") (in auto ref Mat22 B) const { pragma(inline, true); return Mat22(this*B.col1, this*B.col2); } @@ -54,13 +58,13 @@ public: } -Float Cross() (in auto ref vec2 a, in auto ref vec2 b) { pragma(inline, true); return a.x*b.y-a.y*b.x; } -vec2 Cross() (in auto ref vec2 a, Float s) { pragma(inline, true); return vec2(s*a.y, -s*a.x); } -vec2 Cross() (Float s, in auto ref vec2 a) { pragma(inline, true); return vec2(-s*a.y, s*a.x); } +VFloat Cross() (in auto ref Vec2 a, in auto ref Vec2 b) { pragma(inline, true); return a.x*b.y-a.y*b.x; } +Vec2 Cross() (in auto ref Vec2 a, VFloat s) { pragma(inline, true); return Vec2(s*a.y, -s*a.x); } +Vec2 Cross() (VFloat s, in auto ref Vec2 a) { pragma(inline, true); return Vec2(-s*a.y, s*a.x); } -Float Sign (Float x) { pragma(inline, true); return (x < FloatNum!(0.0) ? -FloatNum!(1.0) : FloatNum!(1.0)); } +VFloat Sign (VFloat x) { pragma(inline, true); return (x < VFloatNum!(0.0) ? -VFloatNum!(1.0) : VFloatNum!(1.0)); } -Float Clamp (Float a, Float low, Float high) { pragma(inline, true); import std.algorithm : min, max; return max(low, min(a, high)); } +VFloat Clamp (VFloat a, VFloat low, VFloat high) { pragma(inline, true); import std.algorithm : min, max; return max(low, min(a, high)); } void Swap(T) (ref T a, ref T b) { T tmp = a; @@ -69,14 +73,14 @@ void Swap(T) (ref T a, ref T b) { } // Random number in range [-1,1] -Float Random (void) { +VFloat Random (void) { pragma(inline, true); import std.random : uniform; - return cast(Float)uniform!"[]"(-FloatNum!(1.0), FloatNum!(1.0)); + return cast(VFloat)uniform!"[]"(-VFloatNum!(1.0), VFloatNum!(1.0)); } -Float Random (Float lo, Float hi) { +VFloat Random (VFloat lo, VFloat hi) { pragma(inline, true); import std.random : uniform; - return cast(Float)uniform!"[]"(lo, hi); + return cast(VFloat)uniform!"[]"(lo, hi); } diff --git a/b2dlite/package.d b/b2dlite/package.d index 655bf70..71371a3 100644 --- a/b2dlite/package.d +++ b/b2dlite/package.d @@ -4,6 +4,6 @@ public import b2dlite.arbiter; public import b2dlite.bbody; public import b2dlite.collide; public import b2dlite.joint; -public import b2dlite.mathutils : Random, Mat22; +public import b2dlite.mathutils : Random, Mat22, Vec2, VFloat, VFloatNum; public import b2dlite.world; public import iv.vmath; \ No newline at end of file diff --git a/b2dlite/world.d b/b2dlite/world.d index 609cbc0..f1f3cda 100644 --- a/b2dlite/world.d +++ b/b2dlite/world.d @@ -32,7 +32,7 @@ public: Body[] bodies; Joint[] joints; Arbiter[ArbiterKey] arbiters; - vec2 gravity; + Vec2 gravity; int iterations; Arbiter xarb; // temporary @@ -41,7 +41,7 @@ public: static bool positionCorrection = true; public: - this() (in auto ref vec2 agravity, int aiterations) { + this() (in auto ref Vec2 agravity, int aiterations) { gravity = agravity; iterations = aiterations; xarb = new Arbiter(); @@ -61,14 +61,14 @@ public: arbiters.clear(); } - void Step (Float dt) { - Float inv_dt = (dt > FloatNum!(0.0) ? FloatNum!(1.0)/dt : FloatNum!(0.0)); + void Step (VFloat dt) { + VFloat inv_dt = (dt > VFloatNum!(0.0) ? VFloatNum!(1.0)/dt : VFloatNum!(0.0)); // determine overlapping bodies and update contact points BroadPhase(); // integrate forces for (int i = 0; i < bodies.length; ++i) { Body b = bodies[i]; - if (b.invMass == FloatNum!(0.0)) continue; + if (b.invMass == VFloatNum!(0.0)) continue; b.velocity += (gravity+b.force*b.invMass)*dt; b.angularVelocity += dt*b.invI*b.torque; } @@ -87,8 +87,8 @@ public: b.position += b.velocity*dt; b.rotation += b.angularVelocity*dt; - b.force.set(FloatNum!(0.0), FloatNum!(0.0)); - b.torque = FloatNum!(0.0); + b.force.set(VFloatNum!(0.0), VFloatNum!(0.0)); + b.torque = VFloatNum!(0.0); } } @@ -98,7 +98,7 @@ public: Body bi = bodies[i]; for (int j = i+1; j < bodies.length; ++j) { Body bj = bodies[j]; - if (bi.invMass == FloatNum!(0.0) && bj.invMass == FloatNum!(0.0)) continue; + if (bi.invMass == VFloatNum!(0.0) && bj.invMass == VFloatNum!(0.0)) continue; if (auto arb = ArbiterKey(bi, bj) in arbiters) { xarb.setup(bi, bj); arb.Update(xarb.contacts.ptr, xarb.numContacts); diff --git a/xmain.d b/xmain.d index 0a6b31a..fa8af71 100644 --- a/xmain.d +++ b/xmain.d @@ -16,9 +16,9 @@ enum GHeight = 600; Body bomb = null; -Float timeStep = FloatNum!(1.0)/FloatNum!(60.0); +VFloat timeStep = VFloatNum!(1.0)/VFloatNum!(60.0); int iterations = 10; -vec2 gravity = vec2(FloatNum!(0.0), -FloatNum!(10.0)); +Vec2 gravity = Vec2(VFloatNum!(0.0), -VFloatNum!(10.0)); int demoIndex = 0; @@ -29,31 +29,31 @@ static this () { world = new World(gravity, iterations); } static void LaunchBomb () { if (bomb is null) { bomb = new Body(); - bomb.set(vec2(FloatNum!(1.0), FloatNum!(1.0)), FloatNum!(50.0)); - bomb.friction = FloatNum!(0.2); + bomb.set(Vec2(VFloatNum!(1.0), VFloatNum!(1.0)), VFloatNum!(50.0)); + bomb.friction = VFloatNum!(0.2); world.Add(bomb); } - bomb.position.set(Random(-FloatNum!(15.0), FloatNum!(15.0)), FloatNum!(15.0)); - bomb.rotation = Random(-FloatNum!(1.5), FloatNum!(1.5)); - bomb.velocity = bomb.position*(-FloatNum!(1.5)); - bomb.angularVelocity = Random(-FloatNum!(20.0), FloatNum!(20.0)); + bomb.position.set(Random(-VFloatNum!(15.0), VFloatNum!(15.0)), VFloatNum!(15.0)); + bomb.rotation = Random(-VFloatNum!(1.5), VFloatNum!(1.5)); + bomb.velocity = bomb.position*(-VFloatNum!(1.5)); + bomb.angularVelocity = Random(-VFloatNum!(20.0), VFloatNum!(20.0)); } static void DrawBody (Body body) { auto R = Mat22(body.rotation); - vec2 x = body.position; - vec2 h = body.width*FloatNum!(0.5); + Vec2 x = body.position; + Vec2 h = body.width*VFloatNum!(0.5); - vec2 v1 = x+R*vec2(-h.x, -h.y); - vec2 v2 = x+R*vec2( h.x, -h.y); - vec2 v3 = x+R*vec2( h.x, h.y); - vec2 v4 = x+R*vec2(-h.x, h.y); + Vec2 v1 = x+R*Vec2(-h.x, -h.y); + Vec2 v2 = x+R*Vec2( h.x, -h.y); + Vec2 v3 = x+R*Vec2( h.x, h.y); + Vec2 v4 = x+R*Vec2(-h.x, h.y); if (body is bomb) { - glColor3f(FloatNum!(0.4), FloatNum!(0.9), FloatNum!(0.4)); + glColor3f(VFloatNum!(0.4), VFloatNum!(0.9), VFloatNum!(0.4)); } else { - glColor3f(FloatNum!(0.8), FloatNum!(0.8), FloatNum!(0.9)); + glColor3f(VFloatNum!(0.8), VFloatNum!(0.8), VFloatNum!(0.9)); } glBegin(GL_LINE_LOOP); @@ -72,13 +72,13 @@ static void DrawJoint (Joint joint) { auto R1 = Mat22(b1.rotation); auto R2 = Mat22(b2.rotation); - vec2 x1 = b1.position; - vec2 p1 = x1+R1*joint.localAnchor1; + Vec2 x1 = b1.position; + Vec2 p1 = x1+R1*joint.localAnchor1; - vec2 x2 = b2.position; - vec2 p2 = x2+R2*joint.localAnchor2; + Vec2 x2 = b2.position; + Vec2 p2 = x2+R2*joint.localAnchor2; - glColor3f(FloatNum!(0.5), FloatNum!(0.5), FloatNum!(0.8)); + glColor3f(VFloatNum!(0.5), VFloatNum!(0.5), VFloatNum!(0.8)); glBegin(GL_LINES); glVertex2f(x1.x, x1.y); glVertex2f(p1.x, p1.y); @@ -106,13 +106,13 @@ struct DemoInfo { string descr; } // single box @DemoInfo("A Single Box") void Demo1 () { with (auto b = new Body()) { - b.set(vec2(FloatNum!(100.0), FloatNum!(20.0)), Float.max); - b.position.set(FloatNum!(0.0), -FloatNum!(0.5)*b.width.y); + b.set(Vec2(VFloatNum!(100.0), VFloatNum!(20.0)), VFloat.max); + b.position.set(VFloatNum!(0.0), -VFloatNum!(0.5)*b.width.y); world.Add(b); } with (auto b = new Body()) { - b.set(vec2(FloatNum!(1.0), FloatNum!(1.0)), FloatNum!(200.0)); - b.position.set(FloatNum!(0.0), FloatNum!(4.0)); + b.set(Vec2(VFloatNum!(1.0), VFloatNum!(1.0)), VFloatNum!(200.0)); + b.position.set(VFloatNum!(0.0), VFloatNum!(4.0)); world.Add(b); } } @@ -121,21 +121,21 @@ struct DemoInfo { string descr; } // a simple pendulum @DemoInfo("Simple Pendulum") void Demo2 () { auto b1 = new Body(); - b1.set(vec2(FloatNum!(100.0), FloatNum!(20.0)), Float.max); - b1.friction = FloatNum!(0.2); - b1.position.set(FloatNum!(0.0), -FloatNum!(0.5)*b1.width.y); - b1.rotation = FloatNum!(0.0); + b1.set(Vec2(VFloatNum!(100.0), VFloatNum!(20.0)), VFloat.max); + b1.friction = VFloatNum!(0.2); + b1.position.set(VFloatNum!(0.0), -VFloatNum!(0.5)*b1.width.y); + b1.rotation = VFloatNum!(0.0); world.Add(b1); auto b2 = new Body(); - b2.set(vec2(FloatNum!(1.0), FloatNum!(1.0)), FloatNum!(100.0)); - b2.friction = FloatNum!(0.2); - b2.position.set(FloatNum!(9.0), FloatNum!(11.0)); - b2.rotation = FloatNum!(0.0); + b2.set(Vec2(VFloatNum!(1.0), VFloatNum!(1.0)), VFloatNum!(100.0)); + b2.friction = VFloatNum!(0.2); + b2.position.set(VFloatNum!(9.0), VFloatNum!(11.0)); + b2.rotation = VFloatNum!(0.0); world.Add(b2); with (auto j = new Joint()) { - j.set(b1, b2, vec2(FloatNum!(0.0), FloatNum!(11.0))); + j.set(b1, b2, Vec2(VFloatNum!(0.0), VFloatNum!(11.0))); world.Add(j); } } @@ -144,50 +144,50 @@ struct DemoInfo { string descr; } // varying friction coefficients @DemoInfo("Varying Friction Coefficients") void Demo3 () { with (auto b = new Body()) { - b.set(vec2(FloatNum!(100.0), FloatNum!(20.0)), Float.max); - b.position.set(FloatNum!(0.0), -FloatNum!(0.5)*b.width.y); + b.set(Vec2(VFloatNum!(100.0), VFloatNum!(20.0)), VFloat.max); + b.position.set(VFloatNum!(0.0), -VFloatNum!(0.5)*b.width.y); world.Add(b); } with (auto b = new Body()) { - b.set(vec2(FloatNum!(13.0), FloatNum!(0.25)), Float.max); - b.position.set(-FloatNum!(2.0), FloatNum!(11.0)); - b.rotation = -FloatNum!(0.25); + b.set(Vec2(VFloatNum!(13.0), VFloatNum!(0.25)), VFloat.max); + b.position.set(-VFloatNum!(2.0), VFloatNum!(11.0)); + b.rotation = -VFloatNum!(0.25); world.Add(b); } with (auto b = new Body()) { - b.set(vec2(FloatNum!(0.25), FloatNum!(1.0)), Float.max); - b.position.set(FloatNum!(5.25), FloatNum!(9.5)); + b.set(Vec2(VFloatNum!(0.25), VFloatNum!(1.0)), VFloat.max); + b.position.set(VFloatNum!(5.25), VFloatNum!(9.5)); world.Add(b); } with (auto b = new Body()) { - b.set(vec2(FloatNum!(13.0), FloatNum!(0.25)), Float.max); - b.position.set(FloatNum!(2.0), FloatNum!(7.0)); - b.rotation = FloatNum!(0.25); + b.set(Vec2(VFloatNum!(13.0), VFloatNum!(0.25)), VFloat.max); + b.position.set(VFloatNum!(2.0), VFloatNum!(7.0)); + b.rotation = VFloatNum!(0.25); world.Add(b); } with (auto b = new Body()) { - b.set(vec2(FloatNum!(0.25), FloatNum!(1.0)), Float.max); - b.position.set(-FloatNum!(5.25), FloatNum!(5.5)); + b.set(Vec2(VFloatNum!(0.25), VFloatNum!(1.0)), VFloat.max); + b.position.set(-VFloatNum!(5.25), VFloatNum!(5.5)); world.Add(b); } with (auto b = new Body()) { - b.set(vec2(FloatNum!(13.0), FloatNum!(0.25)), Float.max); - b.position.set(-FloatNum!(2.0), FloatNum!(3.0)); - b.rotation = -FloatNum!(0.25); + b.set(Vec2(VFloatNum!(13.0), VFloatNum!(0.25)), VFloat.max); + b.position.set(-VFloatNum!(2.0), VFloatNum!(3.0)); + b.rotation = -VFloatNum!(0.25); world.Add(b); } - static immutable Float[5] frictions = [FloatNum!(0.75), FloatNum!(0.5), FloatNum!(0.35), FloatNum!(0.1), FloatNum!(0.0)]; + static immutable VFloat[5] frictions = [VFloatNum!(0.75), VFloatNum!(0.5), VFloatNum!(0.35), VFloatNum!(0.1), VFloatNum!(0.0)]; for (int i = 0; i < 5; ++i) { with (auto b = new Body()) { - b.set(vec2(FloatNum!(0.5), FloatNum!(0.5)), FloatNum!(25.0)); + b.set(Vec2(VFloatNum!(0.5), VFloatNum!(0.5)), VFloatNum!(25.0)); b.friction = frictions[i]; - b.position.set(-FloatNum!(7.5)+FloatNum!(2.0)*i, FloatNum!(14.0)); + b.position.set(-VFloatNum!(7.5)+VFloatNum!(2.0)*i, VFloatNum!(14.0)); world.Add(b); } } @@ -197,19 +197,19 @@ struct DemoInfo { string descr; } // a vertical stack @DemoInfo("Randomized Stacking") void Demo4 () { with (auto b = new Body()) { - b.set(vec2(FloatNum!(100.0), FloatNum!(20.0)), Float.max); - b.friction = FloatNum!(0.2); - b.position.set(FloatNum!(0.0), -FloatNum!(0.5)*b.width.y); - b.rotation = FloatNum!(0.0); + b.set(Vec2(VFloatNum!(100.0), VFloatNum!(20.0)), VFloat.max); + b.friction = VFloatNum!(0.2); + b.position.set(VFloatNum!(0.0), -VFloatNum!(0.5)*b.width.y); + b.rotation = VFloatNum!(0.0); world.Add(b); } for (int i = 0; i < 10; ++i) { with (auto b = new Body()) { - b.set(vec2(FloatNum!(1.0), FloatNum!(1.0)), FloatNum!(1.0)); - b.friction = FloatNum!(0.2); - Float x = Random(-FloatNum!(0.1), FloatNum!(0.1)); - b.position.set(x, FloatNum!(0.51)+FloatNum!(1.05)*i); + b.set(Vec2(VFloatNum!(1.0), VFloatNum!(1.0)), VFloatNum!(1.0)); + b.friction = VFloatNum!(0.2); + VFloat x = Random(-VFloatNum!(0.1), VFloatNum!(0.1)); + b.position.set(x, VFloatNum!(0.51)+VFloatNum!(1.05)*i); world.Add(b); } } @@ -219,29 +219,29 @@ struct DemoInfo { string descr; } // a pyramid @DemoInfo("Pyramid Stacking") void Demo5 () { with (auto b = new Body()) { - b.set(vec2(FloatNum!(100.0), FloatNum!(20.0)), Float.max); - b.friction = FloatNum!(0.2); - b.position.set(FloatNum!(0.0), -FloatNum!(0.5)*b.width.y); - b.rotation = FloatNum!(0.0); + b.set(Vec2(VFloatNum!(100.0), VFloatNum!(20.0)), VFloat.max); + b.friction = VFloatNum!(0.2); + b.position.set(VFloatNum!(0.0), -VFloatNum!(0.5)*b.width.y); + b.rotation = VFloatNum!(0.0); world.Add(b); } - vec2 x = vec2(-FloatNum!(6.0), FloatNum!(0.75)); - vec2 y; + Vec2 x = Vec2(-VFloatNum!(6.0), VFloatNum!(0.75)); + Vec2 y; for (int i = 0; i < 12; ++i) { y = x; for (int j = i; j < 12; ++j) { with (auto b = new Body()) { - b.set(vec2(FloatNum!(1.0), FloatNum!(1.0)), FloatNum!(10.0)); - b.friction = FloatNum!(0.2); + b.set(Vec2(VFloatNum!(1.0), VFloatNum!(1.0)), VFloatNum!(10.0)); + b.friction = VFloatNum!(0.2); b.position = y; world.Add(b); } - y += vec2(FloatNum!(1.125), FloatNum!(0.0)); + y += Vec2(VFloatNum!(1.125), VFloatNum!(0.0)); } - //x += vec2(FloatNum!(0.5625), FloatNum!(1.125)); - x += vec2(FloatNum!(0.5625), FloatNum!(2.0)); + //x += Vec2(VFloatNum!(0.5625), VFloatNum!(1.125)); + x += Vec2(VFloatNum!(0.5625), VFloatNum!(2.0)); } } @@ -249,32 +249,32 @@ struct DemoInfo { string descr; } // a teeter @DemoInfo("A Teeter") void Demo6 () { Body b1 = new Body(); - b1.set(vec2(FloatNum!(100.0), FloatNum!(20.0)), Float.max); - b1.position.set(FloatNum!(0.0), -FloatNum!(0.5)*b1.width.y); + b1.set(Vec2(VFloatNum!(100.0), VFloatNum!(20.0)), VFloat.max); + b1.position.set(VFloatNum!(0.0), -VFloatNum!(0.5)*b1.width.y); world.Add(b1); Body b2 = new Body(); - b2.set(vec2(FloatNum!(12.0), FloatNum!(0.25)), FloatNum!(100.0)); - b2.position.set(FloatNum!(0.0), FloatNum!(1.0)); + b2.set(Vec2(VFloatNum!(12.0), VFloatNum!(0.25)), VFloatNum!(100.0)); + b2.position.set(VFloatNum!(0.0), VFloatNum!(1.0)); world.Add(b2); Body b3 = new Body(); - b3.set(vec2(FloatNum!(0.5), FloatNum!(0.5)), FloatNum!(25.0)); - b3.position.set(-FloatNum!(5.0), FloatNum!(2.0)); + b3.set(Vec2(VFloatNum!(0.5), VFloatNum!(0.5)), VFloatNum!(25.0)); + b3.position.set(-VFloatNum!(5.0), VFloatNum!(2.0)); world.Add(b3); Body b4 = new Body(); - b4.set(vec2(FloatNum!(0.5), FloatNum!(0.5)), FloatNum!(25.0)); - b4.position.set(-FloatNum!(5.5), FloatNum!(2.0)); + b4.set(Vec2(VFloatNum!(0.5), VFloatNum!(0.5)), VFloatNum!(25.0)); + b4.position.set(-VFloatNum!(5.5), VFloatNum!(2.0)); world.Add(b4); Body b5 = new Body(); - b5.set(vec2(FloatNum!(1.0), FloatNum!(1.0)), FloatNum!(100.0)); - b5.position.set(FloatNum!(5.5), FloatNum!(15.0)); + b5.set(Vec2(VFloatNum!(1.0), VFloatNum!(1.0)), VFloatNum!(100.0)); + b5.position.set(VFloatNum!(5.5), VFloatNum!(15.0)); world.Add(b5); with (auto j = new Joint()) { - j.set(b1, b2, vec2(FloatNum!(0.0), FloatNum!(1.0))); + j.set(b1, b2, Vec2(VFloatNum!(0.0), VFloatNum!(1.0))); world.Add(j); } } @@ -283,51 +283,51 @@ struct DemoInfo { string descr; } // a suspension bridge @DemoInfo("A Suspension Bridge") void Demo7 () { with (auto b = new Body()) { - b.set(vec2(FloatNum!(100.0), FloatNum!(20.0)), Float.max); - b.friction = FloatNum!(0.2); - b.position.set(FloatNum!(0.0), -FloatNum!(0.5)*b.width.y); - b.rotation = FloatNum!(0.0); + b.set(Vec2(VFloatNum!(100.0), VFloatNum!(20.0)), VFloat.max); + b.friction = VFloatNum!(0.2); + b.position.set(VFloatNum!(0.0), -VFloatNum!(0.5)*b.width.y); + b.rotation = VFloatNum!(0.0); world.Add(b); } enum numPlanks = 15; - Float mass = FloatNum!(50.0); + VFloat mass = VFloatNum!(50.0); for (int i = 0; i < numPlanks; ++i) { auto b = new Body(); - b.set(vec2(FloatNum!(1.0), FloatNum!(0.25)), mass); - b.friction = FloatNum!(0.2); - b.position.set(-FloatNum!(8.5)+FloatNum!(1.25)*i, FloatNum!(5.0)); + b.set(Vec2(VFloatNum!(1.0), VFloatNum!(0.25)), mass); + b.friction = VFloatNum!(0.2); + b.position.set(-VFloatNum!(8.5)+VFloatNum!(1.25)*i, VFloatNum!(5.0)); world.Add(b); } // tuning - Float frequencyHz = FloatNum!(2.0); - Float dampingRatio = FloatNum!(0.7); + VFloat frequencyHz = VFloatNum!(2.0); + VFloat dampingRatio = VFloatNum!(0.7); // frequency in radians - Float omega = FloatNum!(2.0)*PI*frequencyHz; + VFloat omega = VFloatNum!(2.0)*PI*frequencyHz; // damping coefficient - Float d = FloatNum!(2.0)*mass*dampingRatio*omega; + VFloat d = VFloatNum!(2.0)*mass*dampingRatio*omega; // spring stifness - Float k = mass*omega*omega; + VFloat k = mass*omega*omega; // magic formulas - Float softnesss = FloatNum!(1.0)/(d+timeStep*k); - Float biasFactorr = timeStep*k/(d+timeStep*k); + VFloat softnesss = VFloatNum!(1.0)/(d+timeStep*k); + VFloat biasFactorr = timeStep*k/(d+timeStep*k); for (int i = 0; i < numPlanks; ++i) { auto j = new Joint(); - j.set(world.bodies[i], world.bodies[i+1], vec2(-FloatNum!(9.125)+FloatNum!(1.25)*i, FloatNum!(5.0))); + j.set(world.bodies[i], world.bodies[i+1], Vec2(-VFloatNum!(9.125)+VFloatNum!(1.25)*i, VFloatNum!(5.0))); j.softness = softnesss; j.biasFactor = biasFactorr; world.Add(j); } with (auto j = new Joint()) { - j.set(world.bodies[numPlanks], world.bodies[0], vec2(-FloatNum!(9.125)+FloatNum!(1.25)*numPlanks, FloatNum!(5.0))); + j.set(world.bodies[numPlanks], world.bodies[0], Vec2(-VFloatNum!(9.125)+VFloatNum!(1.25)*numPlanks, VFloatNum!(5.0))); j.softness = softnesss; j.biasFactor = biasFactorr; world.Add(j); @@ -338,76 +338,76 @@ struct DemoInfo { string descr; } // dominos @DemoInfo("Dominos") void Demo8 () { Body b1 = new Body(); - b1.set(vec2(FloatNum!(100.0), FloatNum!(20.0)), Float.max); - b1.position.set(FloatNum!(0.0), -FloatNum!(0.5)*b1.width.y); + b1.set(Vec2(VFloatNum!(100.0), VFloatNum!(20.0)), VFloat.max); + b1.position.set(VFloatNum!(0.0), -VFloatNum!(0.5)*b1.width.y); world.Add(b1); with (auto b = new Body()) { - b.set(vec2(FloatNum!(12.0), FloatNum!(0.5)), Float.max); - b.position.set(-FloatNum!(1.5), FloatNum!(10.0)); + b.set(Vec2(VFloatNum!(12.0), VFloatNum!(0.5)), VFloat.max); + b.position.set(-VFloatNum!(1.5), VFloatNum!(10.0)); world.Add(b); } for (int i = 0; i < 10; ++i) { with (auto b = new Body()) { - b.set(vec2(FloatNum!(0.2), FloatNum!(2.0)), FloatNum!(10.0)); - b.position.set(-FloatNum!(6.0)+FloatNum!(1.0)*i, FloatNum!(11.125)); - b.friction = FloatNum!(0.1); + b.set(Vec2(VFloatNum!(0.2), VFloatNum!(2.0)), VFloatNum!(10.0)); + b.position.set(-VFloatNum!(6.0)+VFloatNum!(1.0)*i, VFloatNum!(11.125)); + b.friction = VFloatNum!(0.1); world.Add(b); } } with (auto b = new Body()) { - b.set(vec2(FloatNum!(14.0), FloatNum!(0.5)), Float.max); - b.position.set(FloatNum!(1.0), FloatNum!(6.0)); - b.rotation = FloatNum!(0.3); + b.set(Vec2(VFloatNum!(14.0), VFloatNum!(0.5)), VFloat.max); + b.position.set(VFloatNum!(1.0), VFloatNum!(6.0)); + b.rotation = VFloatNum!(0.3); world.Add(b); } Body b2 = new Body(); - b2.set(vec2(FloatNum!(0.5), FloatNum!(3.0)), Float.max); - b2.position.set(-FloatNum!(7.0), FloatNum!(4.0)); + b2.set(Vec2(VFloatNum!(0.5), VFloatNum!(3.0)), VFloat.max); + b2.position.set(-VFloatNum!(7.0), VFloatNum!(4.0)); world.Add(b2); Body b3 = new Body(); - b3.set(vec2(FloatNum!(12.0), FloatNum!(0.25)), FloatNum!(20.0)); - b3.position.set(-FloatNum!(0.9), FloatNum!(1.0)); + b3.set(Vec2(VFloatNum!(12.0), VFloatNum!(0.25)), VFloatNum!(20.0)); + b3.position.set(-VFloatNum!(0.9), VFloatNum!(1.0)); world.Add(b3); with (auto j = new Joint()) { - j.set(b1, b3, vec2(-FloatNum!(2.0), FloatNum!(1.0))); + j.set(b1, b3, Vec2(-VFloatNum!(2.0), VFloatNum!(1.0))); world.Add(j); } Body b4 = new Body(); - b4.set(vec2(FloatNum!(0.5), FloatNum!(0.5)), FloatNum!(10.0)); - b4.position.set(-FloatNum!(10.0), FloatNum!(15.0)); + b4.set(Vec2(VFloatNum!(0.5), VFloatNum!(0.5)), VFloatNum!(10.0)); + b4.position.set(-VFloatNum!(10.0), VFloatNum!(15.0)); world.Add(b4); with (auto j = new Joint()) { - j.set(b2, b4, vec2(-FloatNum!(7.0), FloatNum!(15.0))); + j.set(b2, b4, Vec2(-VFloatNum!(7.0), VFloatNum!(15.0))); world.Add(j); } Body b5 = new Body(); - b5.set(vec2(FloatNum!(2.0), FloatNum!(2.0)), FloatNum!(20.0)); - b5.position.set(FloatNum!(6.0), FloatNum!(2.5)); - b5.friction = FloatNum!(0.1); + b5.set(Vec2(VFloatNum!(2.0), VFloatNum!(2.0)), VFloatNum!(20.0)); + b5.position.set(VFloatNum!(6.0), VFloatNum!(2.5)); + b5.friction = VFloatNum!(0.1); world.Add(b5); with (auto j = new Joint()) { - j.set(b1, b5, vec2(FloatNum!(6.0), FloatNum!(2.6))); + j.set(b1, b5, Vec2(VFloatNum!(6.0), VFloatNum!(2.6))); world.Add(j); } // box cap Body b6 = new Body(); - b6.set(vec2(FloatNum!(2.0), FloatNum!(0.2)), FloatNum!(10.0)); - b6.position.set(FloatNum!(6.0), FloatNum!(3.6)); + b6.set(Vec2(VFloatNum!(2.0), VFloatNum!(0.2)), VFloatNum!(10.0)); + b6.position.set(VFloatNum!(6.0), VFloatNum!(3.6)); world.Add(b6); with (auto j = new Joint()) { - j.set(b5, b6, vec2(FloatNum!(7.0), FloatNum!(3.5))); + j.set(b5, b6, Vec2(VFloatNum!(7.0), VFloatNum!(3.5))); world.Add(j); } } @@ -416,44 +416,44 @@ struct DemoInfo { string descr; } // a multi-pendulum @DemoInfo("Multi-pendulum") void Demo9 () { Body b1 = new Body(); - b1.set(vec2(FloatNum!(100.0), FloatNum!(20.0)), Float.max); - b1.friction = FloatNum!(0.2); - b1.position.set(FloatNum!(0.0), -FloatNum!(0.5)*b1.width.y); - b1.rotation = FloatNum!(0.0); + b1.set(Vec2(VFloatNum!(100.0), VFloatNum!(20.0)), VFloat.max); + b1.friction = VFloatNum!(0.2); + b1.position.set(VFloatNum!(0.0), -VFloatNum!(0.5)*b1.width.y); + b1.rotation = VFloatNum!(0.0); world.Add(b1); - Float mass = FloatNum!(10.0); + VFloat mass = VFloatNum!(10.0); // tuning - Float frequencyHz = FloatNum!(4.0); - Float dampingRatio = FloatNum!(0.7); + VFloat frequencyHz = VFloatNum!(4.0); + VFloat dampingRatio = VFloatNum!(0.7); // frequency in radians - Float omega = FloatNum!(2.0)*PI*frequencyHz; + VFloat omega = VFloatNum!(2.0)*PI*frequencyHz; // damping coefficient - Float d = FloatNum!(2.0)*mass*dampingRatio*omega; + VFloat d = VFloatNum!(2.0)*mass*dampingRatio*omega; // spring stiffness - Float k = mass*omega*omega; + VFloat k = mass*omega*omega; // magic formulas - Float softnesss = FloatNum!(1.0)/(d+timeStep*k); - Float biasFactorr = timeStep*k/(d+timeStep*k); + VFloat softnesss = VFloatNum!(1.0)/(d+timeStep*k); + VFloat biasFactorr = timeStep*k/(d+timeStep*k); - const Float y = FloatNum!(12.0); + const VFloat y = VFloatNum!(12.0); for (int i = 0; i < 15; ++i) { - vec2 x = vec2(FloatNum!(0.5)+i, y); + Vec2 x = Vec2(VFloatNum!(0.5)+i, y); auto b = new Body(); - b.set(vec2(FloatNum!(0.75), FloatNum!(0.25)), mass); - b.friction = FloatNum!(0.2); + b.set(Vec2(VFloatNum!(0.75), VFloatNum!(0.25)), mass); + b.friction = VFloatNum!(0.2); b.position = x; - b.rotation = FloatNum!(0.0); + b.rotation = VFloatNum!(0.0); world.Add(b); with (auto j = new Joint()) { - j.set(b1, b, vec2(i, y)); + j.set(b1, b, Vec2(i, y)); j.softness = softnesss; j.biasFactor = biasFactorr; world.Add(j); @@ -524,7 +524,7 @@ void SimulationLoop () { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - glTranslatef(FloatNum!(0.0), -FloatNum!(7.0), -FloatNum!(25.0)); + glTranslatef(VFloatNum!(0.0), -VFloatNum!(7.0), -VFloatNum!(25.0)); /* t_cur = k8clock_msec(NULL); @@ -556,7 +556,7 @@ void SimulationLoop () { void drawWorld () { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - glTranslatef(FloatNum!(0.0), -FloatNum!(7.0), -FloatNum!(25.0)); + glTranslatef(VFloatNum!(0.0), -VFloatNum!(7.0), -VFloatNum!(25.0)); // draw world foreach (Body b; world.bodies) b.DrawBody(); @@ -579,8 +579,8 @@ void main () { /* world.render(); if (dragVertex !is null) { - glPointSize(FloatNum!(6.0)); - glColor3f(FloatNum!(1.0), FloatNum!(1.0), FloatNum!(0.0)); + glPointSize(VFloatNum!(6.0)); + glColor3f(VFloatNum!(1.0), VFloatNum!(1.0), VFloatNum!(0.0)); glBegin(GL_POINTS); glVertex2f(dragVertex.position.x, dragVertex.position.y); glEnd(); @@ -604,11 +604,11 @@ void main () { glViewport(0, 0, GWidth, GHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - oglPerspective(45.0, cast(Float)GWidth/cast(Float)GHeight, 0.1, 100.0); + oglPerspective(45.0, cast(VFloat)GWidth/cast(VFloat)GHeight, 0.1, 100.0); sdwindow.redrawOpenGlScene(); }; - sdwindow.eventLoop(cast(int)(FloatNum!(1000.0)/FloatNum!(60.0)), + sdwindow.eventLoop(cast(int)(VFloatNum!(1000.0)/VFloatNum!(60.0)), delegate () { if (sdwindow.closed || world is null) return; if (!paused) { @@ -640,7 +640,7 @@ void main () { } else if (event.type == MouseEventType.buttonReleased) { if (event.button == MouseButton.left) dragVertex = null; } - if (dragVertex !is null) dragVertex.position = vec2(cast(Float)event.x, cast(Float)event.y); // sets the position of the dragVertex to the mouse position to drag it around + if (dragVertex !is null) dragVertex.position = Vec2(cast(VFloat)event.x, cast(VFloat)event.y); // sets the position of the dragVertex to the mouse position to drag it around */ }, delegate (dchar ch) { -- 2.11.4.GIT