adapted to new vmath
[b2ld.git] / b2dlite / bbody.d
blob6902c55a92a04f73b659840e1c946136ef0fdcce
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 class Body {
18 public:
19 Vec2 position;
20 VFloat rotation;
22 Vec2 velocity;
23 VFloat angularVelocity;
25 Vec2 force;
26 VFloat torque;
28 Vec2 width;
30 VFloat friction;
31 VFloat mass, invMass;
32 VFloat I, invI;
34 public:
35 this () {
36 position.set(VFloatNum!(0.0), VFloatNum!(0.0));
37 rotation = VFloatNum!(0.0);
38 velocity.set(VFloatNum!(0.0), VFloatNum!(0.0));
39 angularVelocity = VFloatNum!(0.0);
40 force.set(VFloatNum!(0.0), VFloatNum!(0.0));
41 torque = VFloatNum!(0.0);
42 friction = VFloatNum!(0.2);
44 width.set(VFloatNum!(1.0), VFloatNum!(1.0));
45 mass = VFloat.max;
46 invMass = VFloatNum!(0.0);
47 I = VFloat.max;
48 invI = VFloatNum!(0.0);
51 void set() (in auto ref Vec2 w, VFloat m) {
52 position.set(VFloatNum!(0.0), VFloatNum!(0.0));
53 rotation = VFloatNum!(0.0);
54 velocity.set(VFloatNum!(0.0), VFloatNum!(0.0));
55 angularVelocity = VFloatNum!(0.0);
56 force.set(VFloatNum!(0.0), VFloatNum!(0.0));
57 torque = VFloatNum!(0.0);
58 friction = VFloatNum!(0.2);
59 width = w;
60 mass = m;
61 if (mass < VFloat.max) {
62 invMass = VFloatNum!(1.0)/mass;
63 I = mass*(width.x*width.x+width.y*width.y)/VFloatNum!(12.0);
64 invI = VFloatNum!(1.0)/I;
65 } else {
66 invMass = VFloatNum!(0.0);
67 I = VFloat.max;
68 invI = VFloatNum!(0.0);
72 void AddForce() (in auto ref Vec2 f) { pragma(inline, true); force += f; }
74 int opCmp() (Body b) pure const nothrow @trusted @nogc {
75 if (b is null) return 1;
76 if (b is this) return 0;
77 if (cast(size_t)cast(void*)this < cast(size_t)cast(void*)b) return -1;
78 if (cast(size_t)cast(void*)this > cast(size_t)cast(void*)b) return 1;
79 return 0;