samples/guiAndGfx: eC port of JFD's Mekano
[sdk] / samples / guiAndGfx / mekano / mekanoobject.ec
1 import "mekanopoint"
2 import "mekanosimulation"
3
4 class ObjectAttributes : uint { bool selected:1, steady:1, highlighted:1; };
5
6 class MekanoObject
7 {
8    void addPoint(MekanoPoint point)
9    {
10       m_Points.Add(point);
11    }
12
13    float m_fMass;
14    float m_fInertiaMoment;
15
16    float m_fCosRotation;
17    float m_fSinRotation;
18
19    float m_fAppliedTorque;
20    float m_fAngularAcceleration;
21    float m_fAngularSpeed;
22    float m_fRotation;
23    float m_fAngularFriction;
24
25    float m_fBoundingRadius;
26
27    Vector2D m_AppliedForce;
28    Vector2D m_Acceleration;
29    Vector2D m_Speed;
30    Vector2D m_Position;
31    Vector2D m_LastPosition;
32
33    ObjectAttributes m_Attributes;
34    List<MekanoPoint> m_Points { };
35
36 public:
37
38    ~MekanoObject()
39    {
40       m_Points.Free();
41    }
42
43    property MekanoSimulation simulation { set { value.addObject(this); } }
44
45    mass = 1.0f;
46    inertiaMoment = 10;
47    angularFriction = 0.1f;
48
49    property float cosRotation { get { return m_fCosRotation; } };
50    property float sinRotation { get { return m_fSinRotation; } };
51
52    property float mass
53    {
54       set { m_fMass=value; }
55       get { return m_fMass; }
56    }
57
58    property float inertiaMoment
59    {
60       set { m_fInertiaMoment=value; }
61       get { return m_fInertiaMoment; }
62    }
63
64    property float boundingRadius
65    {
66       set { m_fBoundingRadius=value; }
67       get { return m_fBoundingRadius; }
68    }
69
70    property float angularSpeed
71    {
72       set { m_fAngularSpeed=value; }
73       get { return m_fAngularSpeed; }
74    }
75
76    property float angularAcceleration
77    {
78       set { m_fAngularAcceleration=value; }
79       get { return m_fAngularAcceleration; }
80    }
81
82    property float angularFriction
83    {
84       set { m_fAngularFriction=value; }
85       get { return m_fAngularFriction; }
86    }
87
88    property Vector2D lastPosition
89    {
90       set { m_LastPosition=value; }
91       get { value = m_LastPosition; }
92    }
93
94    property Vector2D position
95    {
96       set { m_Position=value; }
97       get { value = m_Position; }
98    }
99
100    property Vector2D speed
101    {
102       set { m_Speed=value; }
103       get { value = m_Speed; }
104    }
105
106    property Vector2D acceleration
107    {
108       set { m_Acceleration=value; }
109       get { value = m_Acceleration; }
110    }
111
112    property Vector2D deltaPosition { get { value.subtract(m_Position, m_LastPosition); } };
113
114    property ObjectAttributes attributes { set { m_Attributes=value; } get { return m_Attributes; } }
115
116    property Vector2D AppliedForce
117    {
118       set { m_AppliedForce=value; }
119       get { value = m_AppliedForce; }
120    }
121
122    property float appliedTorque
123    {
124       set { m_fAppliedTorque=value; }
125       get { return m_fAppliedTorque; }
126    }
127
128    property List<MekanoPoint> points { get { return m_Points; } };
129
130    property MekanoPoint centerPoint
131    {
132       get
133       {
134          for(p : m_Points; p.type == center)
135             return p;
136          return null;
137       }
138    }
139
140    property float rotation
141    {
142       get { return m_fRotation; }
143       set
144       {
145          m_fRotation=value;
146          m_fCosRotation=(float)cos(value);
147          m_fSinRotation=(float)sin(value);
148       }
149    }
150
151    virtual void resetForces() { m_AppliedForce = { }, m_fAppliedTorque = 0; };
152    virtual void exertForces(MekanoSimulation sim);
153    virtual void applyForce(Vector2D f, MekanoPoint pt);
154    virtual void draw(MekanoDisplay display);
155    virtual void step(Time dt) { lastPosition = m_Position; }
156    virtual bool isInside(Vector2D v) { return false; }
157    virtual bool isColliding(MekanoPoint pt, Vector2D normal) { return false; }
158
159    void computeBoundingRadius()
160    {
161       for(p : m_Points; p.type == vertex)
162          p.computeBoundingRadius();
163    }
164 }