more cosmetix
[b2ld.git] / b2dlite / bbody.d
blobfa029311e1bf786eb4f2556ebbc9532809e08323
1 /*
2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
4 * Permission to use, copy, modify, distribute and sell this software
5 * and its documentation for any purpose is hereby granted without fee,
6 * provided that the above copyright notice appear in all copies.
7 * Erin Catto makes no representations about the suitability
8 * of this software for any purpose.
9 * It is provided "as is" without express or implied warranty.
11 module b2dlite.bbody;
13 import iv.vmath;
14 import b2dlite.mathutils;
17 // ////////////////////////////////////////////////////////////////////////// //
18 public class Body {
19 private:
20 static shared uint lastidx;
22 package(b2dlite):
23 uint midx;
25 public:
26 Vec2 position;
27 VFloat rotation;
29 Vec2 velocity;
30 VFloat angularVelocity;
32 Vec2 force;
33 VFloat torque;
35 Vec2 width;
37 VFloat friction;
38 VFloat mass, invMass;
39 VFloat I, invI;
41 public:
42 this () @trusted {
43 import core.atomic : atomicOp;
44 midx = atomicOp!"+="(lastidx, 1);
45 position.set(VFloatNum!(0.0), VFloatNum!(0.0));
46 rotation = VFloatNum!(0.0);
47 velocity.set(VFloatNum!(0.0), VFloatNum!(0.0));
48 angularVelocity = VFloatNum!(0.0);
49 force.set(VFloatNum!(0.0), VFloatNum!(0.0));
50 torque = VFloatNum!(0.0);
51 friction = VFloatNum!(0.2);
53 width.set(VFloatNum!(1.0), VFloatNum!(1.0));
54 mass = VFloat.max;
55 invMass = VFloatNum!(0.0);
56 I = VFloat.max;
57 invI = VFloatNum!(0.0);
60 @property uint id () const pure nothrow @safe @nogc { pragma(inline, true); return midx; }
62 void set() (in auto ref Vec2 w, VFloat m) {
63 position.set(VFloatNum!(0.0), VFloatNum!(0.0));
64 rotation = VFloatNum!(0.0);
65 velocity.set(VFloatNum!(0.0), VFloatNum!(0.0));
66 angularVelocity = VFloatNum!(0.0);
67 force.set(VFloatNum!(0.0), VFloatNum!(0.0));
68 torque = VFloatNum!(0.0);
69 friction = VFloatNum!(0.2);
70 width = w;
71 mass = m;
72 if (mass < VFloat.max) {
73 invMass = VFloatNum!(1.0)/mass;
74 I = mass*(width.x*width.x+width.y*width.y)/VFloatNum!(12.0);
75 invI = VFloatNum!(1.0)/I;
76 } else {
77 invMass = VFloatNum!(0.0);
78 I = VFloat.max;
79 invI = VFloatNum!(0.0);
83 void addForce() (in auto ref Vec2 f) { pragma(inline, true); force += f; }
85 int opCmp() (Body b) pure const nothrow @trusted @nogc {
86 pragma(inline, true);
87 return (b !is null ? b.midx == midx : 1);