Skip to content

Commit dbf0ed8

Browse files
committed
ragdoll - closes #218
ragdoll editor - closes #911
1 parent 81b7a4d commit dbf0ed8

34 files changed

Lines changed: 1316 additions & 437 deletions

src/audio/audio_scene.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ struct AudioSceneImpl : public AudioScene
110110
{
111111
auto pos = m_universe.getPosition(m_listener.entity);
112112
m_device.setListenerPosition(pos.x, pos.y, pos.z);
113-
Matrix orientation;
114-
m_universe.getRotation(m_listener.entity).toMatrix(orientation);
113+
Matrix orientation = m_universe.getRotation(m_listener.entity).toMatrix();
115114
auto front = orientation.getZVector();
116115
auto up = orientation.getYVector();
117116
m_device.setListenerOrientation(front.x, front.y, front.z, up.x, up.y, up.z);

src/editor/gizmo.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ struct GizmoImpl : public Gizmo
115115
Matrix scale_mtx = Matrix::IDENTITY;
116116
scale_mtx.m11 = scale_mtx.m22 = scale_mtx.m33 = scale;
117117
Vec3 center = m_editor.getRenderInterface()->getModelCenter(entity);
118-
mtx.setTranslation((mtx * scale_mtx).multiplyPosition(center));
118+
mtx.setTranslation((mtx * scale_mtx).transform(center));
119119
}
120120
else
121121
{
@@ -568,13 +568,13 @@ struct GizmoImpl : public Gizmo
568568
{
569569
Vec3 pos = universe->getPosition(m_editor.getSelectedEntities()[i]);
570570
Quat old_rot = universe->getRotation(m_editor.getSelectedEntities()[i]);
571-
Quat new_rot = old_rot * Quat(axis, angle);
571+
Quat new_rot = Quat(axis, angle) * old_rot;
572572
new_rot.normalize();
573573
new_rotations.push(new_rot);
574574
Vec3 pdif = mtx.getTranslation() - pos;
575575
old_rot.conjugate();
576576
pos = -pdif;
577-
pos = new_rot * (old_rot * pos);
577+
pos = new_rot.rotate(old_rot.rotate(pos));
578578
pos += mtx.getTranslation();
579579

580580
new_positions.push(pos);
@@ -587,7 +587,7 @@ struct GizmoImpl : public Gizmo
587587
else
588588
{
589589
Quat old_rot = universe->getRotation(m_entities[m_active]);
590-
Quat new_rot = old_rot * Quat(axis, angle);
590+
Quat new_rot = Quat(axis, angle) * old_rot;
591591
new_rot.normalize();
592592
new_rotations.push(new_rot);
593593
m_editor.setEntitiesRotations(&m_entities[m_active], &new_rotations[0], 1);
@@ -662,7 +662,7 @@ struct GizmoImpl : public Gizmo
662662
auto* render_interface = m_editor.getRenderInterface();
663663
bool is_ortho = render_interface->isCameraOrtho(edit_camera.handle);
664664
auto camera_pos = m_editor.getUniverse()->getPosition(edit_camera.entity);
665-
auto camera_dir = m_editor.getUniverse()->getRotation(edit_camera.entity) * Vec3(0, 0, -1);
665+
auto camera_dir = m_editor.getUniverse()->getRotation(edit_camera.entity).rotate(Vec3(0, 0, -1));
666666
float fov = render_interface->getCameraFOV(edit_camera.handle);
667667

668668
collide(camera_pos, camera_dir, fov, is_ortho);

src/editor/studio_app.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,8 @@ class StudioAppImpl : public StudioApp
612612

613613
void instantiateTemplate()
614614
{
615+
if (m_selected_template_name.length() <= 0) return;
616+
615617
Lumix::Vec3 pos = m_editor->getCameraRaycastHit();
616618
auto& template_system = m_editor->getEntityTemplateSystem();
617619
template_system.createInstance(m_selected_template_name.c_str(), pos, Lumix::Quat(0, 0, 0, 1), 1);

src/editor/world_editor.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,7 @@ struct WorldEditorImpl : public WorldEditor
15481548
mtx.setZVector(dir);
15491549

15501550
positions.push(pos);
1551-
mtx.getRotation(rotations.emplace());
1551+
rotations.emplace(mtx.getRotation());
15521552
}
15531553
}
15541554
MoveEntityCommand* cmd = LUMIX_NEW(m_allocator, MoveEntityCommand)(*this,
@@ -1827,7 +1827,7 @@ struct WorldEditorImpl : public WorldEditor
18271827
}
18281828
else
18291829
{
1830-
pos = universe->getPosition(m_camera) + universe->getRotation(m_camera) * Vec3(0, 0, -2);
1830+
pos = universe->getPosition(m_camera) + universe->getRotation(m_camera).rotate(Vec3(0, 0, -2));
18311831
}
18321832
AddEntityCommand* command = LUMIX_NEW(m_allocator, AddEntityCommand)(*this, pos);
18331833
executeCommand(command);
@@ -1854,7 +1854,7 @@ struct WorldEditorImpl : public WorldEditor
18541854
}
18551855
else
18561856
{
1857-
pos = universe->getPosition(m_camera) + universe->getRotation(m_camera) * Vec3(0, 0, -2);
1857+
pos = universe->getPosition(m_camera) + universe->getRotation(m_camera).rotate(Vec3(0, 0, -2));
18581858
}
18591859
return pos;
18601860
}
@@ -2204,7 +2204,7 @@ struct WorldEditorImpl : public WorldEditor
22042204
m_go_to_parameters.m_t = 0;
22052205
m_go_to_parameters.m_from = universe->getPosition(m_camera);
22062206
Quat camera_rot = universe->getRotation(m_camera);
2207-
Vec3 dir = camera_rot * Vec3(0, 0, 1);
2207+
Vec3 dir = camera_rot.rotate(Vec3(0, 0, 1));
22082208
m_go_to_parameters.m_to = universe->getPosition(m_selected_entities[0]) + dir * 10;
22092209
float len = (m_go_to_parameters.m_to - m_go_to_parameters.m_from).length();
22102210
m_go_to_parameters.m_speed = Math::maximum(100.0f / (len > 0 ? len : 1), 2.0f);
@@ -2465,9 +2465,9 @@ struct WorldEditorImpl : public WorldEditor
24652465

24662466
right = m_is_orbit ? 0 : right;
24672467

2468-
pos += rot * Vec3(0, 0, -1) * forward * speed;
2469-
pos += rot * Vec3(1, 0, 0) * right * speed;
2470-
pos += rot * Vec3(0, 1, 0) * up * speed;
2468+
pos += rot.rotate(Vec3(0, 0, -1)) * forward * speed;
2469+
pos += rot.rotate(Vec3(1, 0, 0)) * right * speed;
2470+
pos += rot.rotate(Vec3(0, 1, 0)) * up * speed;
24712471
universe->setPosition(m_camera, pos);
24722472
}
24732473

@@ -2570,8 +2570,8 @@ struct WorldEditorImpl : public WorldEditor
25702570
m_orbit_delta.y += y;
25712571
}
25722572

2573-
pos += rot * Vec3(x, 0, 0);
2574-
pos += rot * Vec3(0, -y, 0);
2573+
pos += rot.rotate(Vec3(x, 0, 0));
2574+
pos += rot.rotate(Vec3(0, -y, 0));
25752575

25762576
universe->setPosition(m_camera, pos);
25772577
}
@@ -2599,28 +2599,28 @@ struct WorldEditorImpl : public WorldEditor
25992599

26002600
float yaw = -Math::signum(x) * (Math::pow(Math::abs((float)x / m_mouse_sensitivity.x), 1.2f));
26012601
Quat yaw_rot(Vec3(0, 1, 0), yaw);
2602-
rot = rot * yaw_rot;
2602+
rot = yaw_rot * rot;
26032603
rot.normalize();
26042604

2605-
Vec3 pitch_axis = rot * Vec3(1, 0, 0);
2605+
Vec3 pitch_axis = rot.rotate(Vec3(1, 0, 0));
26062606
float pitch = -Math::signum(y) * (Math::pow(Math::abs((float)y / m_mouse_sensitivity.y), 1.2f));
26072607
Quat pitch_rot(pitch_axis, pitch);
2608-
rot = rot * pitch_rot;
2608+
rot = pitch_rot * rot;
26092609
rot.normalize();
26102610

26112611
if (m_is_orbit && !m_selected_entities.empty())
26122612
{
2613-
Vec3 dir = rot * Vec3(0, 0, 1);
2613+
Vec3 dir = rot.rotate(Vec3(0, 0, 1));
26142614
Vec3 entity_pos = universe->getPosition(m_selected_entities[0]);
26152615
Vec3 nondelta_pos = pos;
26162616

2617-
nondelta_pos -= old_rot * Vec3(0, -1, 0) * m_orbit_delta.y;
2618-
nondelta_pos -= old_rot * Vec3(1, 0, 0) * m_orbit_delta.x;
2617+
nondelta_pos -= old_rot.rotate(Vec3(0, -1, 0)) * m_orbit_delta.y;
2618+
nondelta_pos -= old_rot.rotate(Vec3(1, 0, 0)) * m_orbit_delta.x;
26192619

26202620
float dist = (entity_pos - nondelta_pos).length();
26212621
pos = entity_pos + dir * dist;
2622-
pos += rot * Vec3(1, 0, 0) * m_orbit_delta.x;
2623-
pos += rot * Vec3(0, -1, 0) * m_orbit_delta.y;
2622+
pos += rot.rotate(Vec3(1, 0, 0)) * m_orbit_delta.x;
2623+
pos += rot.rotate(Vec3(0, -1, 0)) * m_orbit_delta.y;
26242624
}
26252625

26262626
universe->setRotation(m_camera, rot);

src/engine/engine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ class EngineImpl : public Engine
348348
q = Quat(axis, angle);
349349
}
350350

351-
Vec3 res = q * v;
351+
Vec3 res = q.rotate(v);
352352

353353
LuaWrapper::pushLua(L, res);
354354
return 1;
@@ -386,7 +386,7 @@ class EngineImpl : public Engine
386386
static Vec3 LUA_getEntityDirection(Universe* universe, Entity entity)
387387
{
388388
Quat rot = universe->getRotation(entity);
389-
return rot * Vec3(0, 0, 1);
389+
return rot.rotate(Vec3(0, 0, 1));
390390
}
391391

392392

src/engine/geometry.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ struct AABB
247247

248248
for (int j = 0; j < 8; ++j)
249249
{
250-
points[j] = matrix.multiplyPosition(points[j]);
250+
points[j] = matrix.transform(points[j]);
251251
}
252252

253253
Vec3 new_min = points[0];
@@ -266,21 +266,21 @@ struct AABB
266266
void getCorners(const Matrix& matrix, Vec3* points) const
267267
{
268268
Vec3 p(min.x, min.y, min.z);
269-
points[0] = matrix.multiplyPosition(p);
269+
points[0] = matrix.transform(p);
270270
p.set(min.x, min.y, max.z);
271-
points[1] = matrix.multiplyPosition(p);
271+
points[1] = matrix.transform(p);
272272
p.set(min.x, max.y, min.z);
273-
points[2] = matrix.multiplyPosition(p);
273+
points[2] = matrix.transform(p);
274274
p.set(min.x, max.y, max.z);
275-
points[3] = matrix.multiplyPosition(p);
275+
points[3] = matrix.transform(p);
276276
p.set(max.x, min.y, min.z);
277-
points[4] = matrix.multiplyPosition(p);
277+
points[4] = matrix.transform(p);
278278
p.set(max.x, min.y, max.z);
279-
points[5] = matrix.multiplyPosition(p);
279+
points[5] = matrix.transform(p);
280280
p.set(max.x, max.y, min.z);
281-
points[6] = matrix.multiplyPosition(p);
281+
points[6] = matrix.transform(p);
282282
p.set(max.x, max.y, max.z);
283-
points[7] = matrix.multiplyPosition(p);
283+
points[7] = matrix.transform(p);
284284
}
285285

286286
Vec3 minCoords(const Vec3& a, const Vec3& b)

src/engine/matrix.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ namespace Lumix
88
{
99

1010

11+
Matrix Transform::toMatrix() const
12+
{
13+
Matrix mtx = rot.toMatrix();
14+
mtx.setTranslation(pos);
15+
return mtx;
16+
}
17+
18+
1119
const Matrix Matrix::IDENTITY(
1220
1, 0, 0, 0,
1321
0, 1, 0, 0,
@@ -112,8 +120,9 @@ Matrix Matrix::operator *(const Matrix& rhs) const
112120
}
113121

114122

115-
void Matrix::getRotation(Quat& rot) const
123+
Quat Matrix::getRotation() const
116124
{
125+
Quat rot;
117126
float tr = m11 + m22 + m33;
118127

119128
if (tr > 0)
@@ -153,6 +162,7 @@ void Matrix::getRotation(Quat& rot) const
153162
rot.y = (m32 + m23) * s;
154163
rot.z = s * t;
155164
}
165+
return rot;
156166
}
157167

158168

@@ -198,7 +208,7 @@ void Matrix::multiply3x3(float scale)
198208
}
199209

200210

201-
Vec3 Matrix::multiplyPosition(const Vec3& rhs) const
211+
Vec3 Matrix::transform(const Vec3& rhs) const
202212
{
203213
return Vec3(
204214
m11 * rhs.x + m21 * rhs.y + m31 * rhs.z + m41,

src/engine/matrix.h

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
#include "engine/lumix.h"
5+
#include "engine/quat.h"
56
#include "engine/vec.h"
67

78

@@ -12,6 +13,47 @@ namespace Lumix
1213
struct Quat;
1314

1415

16+
struct LUMIX_ENGINE_API Transform
17+
{
18+
Transform() {}
19+
20+
21+
Transform(const Vec3& _pos, const Quat& _rot)
22+
: pos(_pos)
23+
, rot(_rot)
24+
{
25+
}
26+
27+
28+
Transform inverted() const
29+
{
30+
Transform result;
31+
result.rot = rot.conjugated();
32+
result.pos = result.rot.rotate(-pos);
33+
return result;
34+
}
35+
36+
37+
Transform operator*(const Transform& rhs) const
38+
{
39+
return {rot.rotate(rhs.pos) + pos, rot * rhs.rot};
40+
}
41+
42+
43+
Vec3 transform(const Vec3& value) const
44+
{
45+
return pos + rot.rotate(value);
46+
}
47+
48+
49+
Matrix toMatrix() const;
50+
51+
52+
Quat rot;
53+
Vec3 pos;
54+
};
55+
56+
1557
struct LUMIX_ENGINE_API Matrix
1658
{
1759
Matrix() {}
@@ -80,7 +122,7 @@ struct LUMIX_ENGINE_API Matrix
80122
}
81123

82124

83-
float determinant()
125+
float determinant() const
84126
{
85127
return
86128
m14 * m23 * m32 * m41 - m13 * m24 * m32 * m41 - m14 * m22 * m33 * m41 + m12 * m24 * m33 * m41 +
@@ -223,9 +265,10 @@ struct LUMIX_ENGINE_API Matrix
223265
return Vec3(m41, m42, m43);
224266
}
225267

226-
void getRotation(Quat& rot) const;
268+
Transform toTransform() { return {getTranslation(), getRotation()}; }
269+
Quat getRotation() const;
227270
void transpose();
228-
Vec3 multiplyPosition(const Vec3& pos) const;
271+
Vec3 transform(const Vec3& pos) const;
229272
void multiply3x3(float scale);
230273
void setIdentity();
231274

0 commit comments

Comments
 (0)