-
Notifications
You must be signed in to change notification settings - Fork 0
/
Object.h
197 lines (188 loc) · 5.2 KB
/
Object.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#ifndef OBJECT_H
#define OBJECT_H
#include "Polygon.h"
#include "Circle.h"
#include "Settings.h"
class Object {
public:
vector<Shape *> shapes;
double mass, base_mass;
double inertia, base_inertia;
double invMass, invInertia;
bool fixed;
Vector2D position;
RGB3f color;
Vector2D linearVelocity;
double rotationAngle, angularVelocity;
Angle angle;
Matrix3x3 transformToWorld, transformToWorldInverse;
double linearResistance, rotationResistance;
Vector2D correctiveLinearVelocity;
double correctiveAngularVelocity;
bool fibRot, selected;
int priority;
int type;
void Init() {
color = RGB3f::RandomBrightColor();
fixed = false;
priority = 1;
rotationAngle = 0.0;
angularVelocity = 0;
position = Vector2D(0, 0, 1);
linearVelocity = Vector2D(0, 0, 0);
correctiveLinearVelocity = Vector2D(0, 0, 0);
correctiveAngularVelocity = 0.0;
linearResistance = rotationResistance = 0.0001;
selected = false;
type = 0;
}
Vector2D GetPosition() {
return position;
}
Matrix3x3 GetTransformToWorld() {
return transformToWorld;
}
Vector2D GetTransformToWorld(Vector2D vec) {
return transformToWorld(vec);
}
Matrix3x3 GetTransformToWorldInverse() {
return transformToWorldInverse;
}
void SetPosition(Vector2D position) {
this->position = position;
ResetTransformToWorld();
}
void ResetTransformToWorld() {
transformToWorld.SetAsAxisTransform(position, rotationAngle);
transformToWorldInverse.SetAsAxisTransformInverse(position, rotationAngle);
for (size_t i = 0; i < shapes.size(); i++)
if (shapes[i]->GetType() == Polygon::ShapeType)
((Polygon *)shapes[i])->UpdateCurrentInformation();
angle = Angle(rotationAngle);
}
Shape *GetShape(int id) {
return shapes[id];
}
Object() {
Init();
}
Object(Shape *shape) {
Init();
AddShape(shape);
}
Object(vector<Shape *> shapes) {
Init();
AddShapes(shapes);
}
void AddShape(Shape *shape) {
shapes.push_back(shape);
shape->object = this;
Update();
}
void AddShapes(vector<Shape *> shapes) {
for (int i = 0; i < (int)shapes.size(); i++)
this->shapes.push_back(shapes[i]), shapes[i]->object = this;
Update();
}
void SetColor(RGB3f color) {
for (int i = 0; i < (int)shapes.size(); i++)
shapes[i]->color = color;
}
void Update() {
int n = shapes.size();
base_mass = 0.0;
for (int i = 0; i < n; i++) {
shapes[i]->Update();
base_mass += shapes[i]->mass;
}
Vector2D centroid = Vector2D(0, 0, 0);
for (int i = 0; i < n; i++) {
centroid += shapes[i]->centroidPosition * shapes[i]->mass;
}
centroid /= base_mass;
centroid.z = 0;
position += centroid;
for (int i = 0; i < n; i++)
shapes[i]->Move(-centroid);
base_inertia = 0.0;
for (int i = 0; i < n; i++)
base_inertia += shapes[i]->inertia + shapes[i]->mass * shapes[i]->centroidPosition.GetLength2();
if (fixed) {
mass = DBL_INF;
inertia = DBL_INF;
} else mass = base_mass, inertia = base_inertia;
invMass = 1.0 / mass;
invInertia = 1.0 / inertia;
ResetTransformToWorld();
}
bool IsPointInside(Vector2D point) {
for (int i = 0; i < (int)shapes.size(); i++)
if (shapes[i]->IsPointInside(point)) return true;
return false;
}
void SetRotationAngle(double rotationAngle) {
this->rotationAngle = rotationAngle;
ResetTransformToWorld();
}
void ApplyGravity(double T) {
if (fixed) return;
ApplyImpulse(position, Vector2D(0, -settings.gravity, 0) * mass * T);
}
void SetFixed(bool fixed) {
this->fixed = fixed;
Update();
}
bool GetFixed() {
return this->fixed;
}
void ApplyTorque(double torque) {
angularVelocity += torque * invInertia;
}
void ApplyImpulse(Vector2D r, Vector2D p) {
linearVelocity += p * invMass;
ApplyTorque((r - position) % p);
}
void ApplyCorrectiveImpulse(Vector2D r, Vector2D p, bool paintNeed = true) {
correctiveLinearVelocity += p * invMass;
correctiveAngularVelocity += (r - position) % p * invInertia;
}
void Proceed(double T) {
if (fixed) return;
position += T * (linearVelocity);
rotationAngle += T * (angularVelocity);
if (sgn(linearVelocity.GetLength2()))
linearVelocity -= (1 - pow(1 - linearResistance, T / timeInterval)) * linearVelocity.GetLength2() * linearVelocity.GetDirection();
angularVelocity -= (1 - pow(1 - rotationResistance, T / timeInterval)) * sqr(angularVelocity);
correctiveLinearVelocity = Vector2D(0, 0, 0);
correctiveAngularVelocity = 0.0;
ResetTransformToWorld();
}
void ApplyPositionCorrection(double T) {
if (fixed) return;
position += T * correctiveLinearVelocity;
rotationAngle += T * correctiveAngularVelocity;
correctiveLinearVelocity = Vector2D(0, 0, 0);
correctiveAngularVelocity = 0.0;
ResetTransformToWorld();
}
Vector2D GetPointVelocity(Vector2D p) {
if (fixed) return Vector2D(0, 0, 0);
else return linearVelocity + Vector2D(-angularVelocity * (p.y - position.y), angularVelocity * (p.x - position.x), 0);
}
void Redraw() {
for (int i = 0; i < (int)shapes.size(); i++)
shapes[i]->Redraw();
}
void SetLayerMask(int layerMask) {
for (int i = 0; i < (int)shapes.size(); i++)
shapes[i]->layerMask = layerMask;
}
void SetResistance(double linear, double angular) {
linearResistance = linear;
rotationResistance = angular;
}
public:
static const int TYPE_WORLD_BOX = 1;
static const int TYPE_GEAR = 2;
};
#endif