initial commit
[b2ld.git] / b2dlite / bbody.d
blob2277d06bc9f5b1f85067466c38aacc9750458de8
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 b2dlite.mathutils;
16 class Body {
17 public:
18 Vec2 position;
19 float rotation;
21 Vec2 velocity;
22 float angularVelocity;
24 Vec2 force;
25 float torque;
27 Vec2 width;
29 float friction;
30 float mass, invMass;
31 float I, invI;
33 public:
34 this () {
35 position.Set(0.0f, 0.0f);
36 rotation = 0.0f;
37 velocity.Set(0.0f, 0.0f);
38 angularVelocity = 0.0f;
39 force.Set(0.0f, 0.0f);
40 torque = 0.0f;
41 friction = 0.2f;
43 width.Set(1.0f, 1.0f);
44 mass = float.max;
45 invMass = 0.0f;
46 I = float.max;
47 invI = 0.0f;
50 void Set() (in auto ref Vec2 w, float m) {
51 position.Set(0.0f, 0.0f);
52 rotation = 0.0f;
53 velocity.Set(0.0f, 0.0f);
54 angularVelocity = 0.0f;
55 force.Set(0.0f, 0.0f);
56 torque = 0.0f;
57 friction = 0.2f;
58 width = w;
59 mass = m;
60 if (mass < float.max) {
61 invMass = 1.0f/mass;
62 I = mass*(width.x*width.x+width.y*width.y)/12.0f;
63 invI = 1.0f/I;
64 } else {
65 invMass = 0.0f;
66 I = float.max;
67 invI = 0.0f;
71 void AddForce() (in auto ref Vec2 f) { pragma(inline, true); force += f; }
73 int opCmp() (Body b) pure const nothrow @trusted @nogc {
74 if (b is null) return 1;
75 if (b is this) return 0;
76 if (cast(size_t)cast(void*)this < cast(size_t)cast(void*)b) return -1;
77 if (cast(size_t)cast(void*)this > cast(size_t)cast(void*)b) return 1;
78 return 0;