Skip to content

Commit

Permalink
ragdoll - closes #218
Browse files Browse the repository at this point in the history
ragdoll editor - closes #911
  • Loading branch information
nem0 committed Jul 1, 2016
1 parent 81b7a4d commit dbf0ed8
Show file tree
Hide file tree
Showing 34 changed files with 1,316 additions and 437 deletions.
3 changes: 1 addition & 2 deletions src/audio/audio_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ struct AudioSceneImpl : public AudioScene
{
auto pos = m_universe.getPosition(m_listener.entity);
m_device.setListenerPosition(pos.x, pos.y, pos.z);
Matrix orientation;
m_universe.getRotation(m_listener.entity).toMatrix(orientation);
Matrix orientation = m_universe.getRotation(m_listener.entity).toMatrix();
auto front = orientation.getZVector();
auto up = orientation.getYVector();
m_device.setListenerOrientation(front.x, front.y, front.z, up.x, up.y, up.z);
Expand Down
10 changes: 5 additions & 5 deletions src/editor/gizmo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ struct GizmoImpl : public Gizmo
Matrix scale_mtx = Matrix::IDENTITY;
scale_mtx.m11 = scale_mtx.m22 = scale_mtx.m33 = scale;
Vec3 center = m_editor.getRenderInterface()->getModelCenter(entity);
mtx.setTranslation((mtx * scale_mtx).multiplyPosition(center));
mtx.setTranslation((mtx * scale_mtx).transform(center));
}
else
{
Expand Down Expand Up @@ -568,13 +568,13 @@ struct GizmoImpl : public Gizmo
{
Vec3 pos = universe->getPosition(m_editor.getSelectedEntities()[i]);
Quat old_rot = universe->getRotation(m_editor.getSelectedEntities()[i]);
Quat new_rot = old_rot * Quat(axis, angle);
Quat new_rot = Quat(axis, angle) * old_rot;
new_rot.normalize();
new_rotations.push(new_rot);
Vec3 pdif = mtx.getTranslation() - pos;
old_rot.conjugate();
pos = -pdif;
pos = new_rot * (old_rot * pos);
pos = new_rot.rotate(old_rot.rotate(pos));
pos += mtx.getTranslation();

new_positions.push(pos);
Expand All @@ -587,7 +587,7 @@ struct GizmoImpl : public Gizmo
else
{
Quat old_rot = universe->getRotation(m_entities[m_active]);
Quat new_rot = old_rot * Quat(axis, angle);
Quat new_rot = Quat(axis, angle) * old_rot;
new_rot.normalize();
new_rotations.push(new_rot);
m_editor.setEntitiesRotations(&m_entities[m_active], &new_rotations[0], 1);
Expand Down Expand Up @@ -662,7 +662,7 @@ struct GizmoImpl : public Gizmo
auto* render_interface = m_editor.getRenderInterface();
bool is_ortho = render_interface->isCameraOrtho(edit_camera.handle);
auto camera_pos = m_editor.getUniverse()->getPosition(edit_camera.entity);
auto camera_dir = m_editor.getUniverse()->getRotation(edit_camera.entity) * Vec3(0, 0, -1);
auto camera_dir = m_editor.getUniverse()->getRotation(edit_camera.entity).rotate(Vec3(0, 0, -1));
float fov = render_interface->getCameraFOV(edit_camera.handle);

collide(camera_pos, camera_dir, fov, is_ortho);
Expand Down
2 changes: 2 additions & 0 deletions src/editor/studio_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,8 @@ class StudioAppImpl : public StudioApp

void instantiateTemplate()
{
if (m_selected_template_name.length() <= 0) return;

Lumix::Vec3 pos = m_editor->getCameraRaycastHit();
auto& template_system = m_editor->getEntityTemplateSystem();
template_system.createInstance(m_selected_template_name.c_str(), pos, Lumix::Quat(0, 0, 0, 1), 1);
Expand Down
34 changes: 17 additions & 17 deletions src/editor/world_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ struct WorldEditorImpl : public WorldEditor
mtx.setZVector(dir);

positions.push(pos);
mtx.getRotation(rotations.emplace());
rotations.emplace(mtx.getRotation());
}
}
MoveEntityCommand* cmd = LUMIX_NEW(m_allocator, MoveEntityCommand)(*this,
Expand Down Expand Up @@ -1827,7 +1827,7 @@ struct WorldEditorImpl : public WorldEditor
}
else
{
pos = universe->getPosition(m_camera) + universe->getRotation(m_camera) * Vec3(0, 0, -2);
pos = universe->getPosition(m_camera) + universe->getRotation(m_camera).rotate(Vec3(0, 0, -2));
}
AddEntityCommand* command = LUMIX_NEW(m_allocator, AddEntityCommand)(*this, pos);
executeCommand(command);
Expand All @@ -1854,7 +1854,7 @@ struct WorldEditorImpl : public WorldEditor
}
else
{
pos = universe->getPosition(m_camera) + universe->getRotation(m_camera) * Vec3(0, 0, -2);
pos = universe->getPosition(m_camera) + universe->getRotation(m_camera).rotate(Vec3(0, 0, -2));
}
return pos;
}
Expand Down Expand Up @@ -2204,7 +2204,7 @@ struct WorldEditorImpl : public WorldEditor
m_go_to_parameters.m_t = 0;
m_go_to_parameters.m_from = universe->getPosition(m_camera);
Quat camera_rot = universe->getRotation(m_camera);
Vec3 dir = camera_rot * Vec3(0, 0, 1);
Vec3 dir = camera_rot.rotate(Vec3(0, 0, 1));
m_go_to_parameters.m_to = universe->getPosition(m_selected_entities[0]) + dir * 10;
float len = (m_go_to_parameters.m_to - m_go_to_parameters.m_from).length();
m_go_to_parameters.m_speed = Math::maximum(100.0f / (len > 0 ? len : 1), 2.0f);
Expand Down Expand Up @@ -2465,9 +2465,9 @@ struct WorldEditorImpl : public WorldEditor

right = m_is_orbit ? 0 : right;

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

Expand Down Expand Up @@ -2570,8 +2570,8 @@ struct WorldEditorImpl : public WorldEditor
m_orbit_delta.y += y;
}

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

universe->setPosition(m_camera, pos);
}
Expand Down Expand Up @@ -2599,28 +2599,28 @@ struct WorldEditorImpl : public WorldEditor

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

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

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

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

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

universe->setRotation(m_camera, rot);
Expand Down
4 changes: 2 additions & 2 deletions src/engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ class EngineImpl : public Engine
q = Quat(axis, angle);
}

Vec3 res = q * v;
Vec3 res = q.rotate(v);

LuaWrapper::pushLua(L, res);
return 1;
Expand Down Expand Up @@ -386,7 +386,7 @@ class EngineImpl : public Engine
static Vec3 LUA_getEntityDirection(Universe* universe, Entity entity)
{
Quat rot = universe->getRotation(entity);
return rot * Vec3(0, 0, 1);
return rot.rotate(Vec3(0, 0, 1));
}


Expand Down
18 changes: 9 additions & 9 deletions src/engine/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ struct AABB

for (int j = 0; j < 8; ++j)
{
points[j] = matrix.multiplyPosition(points[j]);
points[j] = matrix.transform(points[j]);
}

Vec3 new_min = points[0];
Expand All @@ -266,21 +266,21 @@ struct AABB
void getCorners(const Matrix& matrix, Vec3* points) const
{
Vec3 p(min.x, min.y, min.z);
points[0] = matrix.multiplyPosition(p);
points[0] = matrix.transform(p);
p.set(min.x, min.y, max.z);
points[1] = matrix.multiplyPosition(p);
points[1] = matrix.transform(p);
p.set(min.x, max.y, min.z);
points[2] = matrix.multiplyPosition(p);
points[2] = matrix.transform(p);
p.set(min.x, max.y, max.z);
points[3] = matrix.multiplyPosition(p);
points[3] = matrix.transform(p);
p.set(max.x, min.y, min.z);
points[4] = matrix.multiplyPosition(p);
points[4] = matrix.transform(p);
p.set(max.x, min.y, max.z);
points[5] = matrix.multiplyPosition(p);
points[5] = matrix.transform(p);
p.set(max.x, max.y, min.z);
points[6] = matrix.multiplyPosition(p);
points[6] = matrix.transform(p);
p.set(max.x, max.y, max.z);
points[7] = matrix.multiplyPosition(p);
points[7] = matrix.transform(p);
}

Vec3 minCoords(const Vec3& a, const Vec3& b)
Expand Down
14 changes: 12 additions & 2 deletions src/engine/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ namespace Lumix
{


Matrix Transform::toMatrix() const
{
Matrix mtx = rot.toMatrix();
mtx.setTranslation(pos);
return mtx;
}


const Matrix Matrix::IDENTITY(
1, 0, 0, 0,
0, 1, 0, 0,
Expand Down Expand Up @@ -112,8 +120,9 @@ Matrix Matrix::operator *(const Matrix& rhs) const
}


void Matrix::getRotation(Quat& rot) const
Quat Matrix::getRotation() const
{
Quat rot;
float tr = m11 + m22 + m33;

if (tr > 0)
Expand Down Expand Up @@ -153,6 +162,7 @@ void Matrix::getRotation(Quat& rot) const
rot.y = (m32 + m23) * s;
rot.z = s * t;
}
return rot;
}


Expand Down Expand Up @@ -198,7 +208,7 @@ void Matrix::multiply3x3(float scale)
}


Vec3 Matrix::multiplyPosition(const Vec3& rhs) const
Vec3 Matrix::transform(const Vec3& rhs) const
{
return Vec3(
m11 * rhs.x + m21 * rhs.y + m31 * rhs.z + m41,
Expand Down
49 changes: 46 additions & 3 deletions src/engine/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


#include "engine/lumix.h"
#include "engine/quat.h"
#include "engine/vec.h"


Expand All @@ -12,6 +13,47 @@ namespace Lumix
struct Quat;


struct LUMIX_ENGINE_API Transform
{
Transform() {}


Transform(const Vec3& _pos, const Quat& _rot)
: pos(_pos)
, rot(_rot)
{
}


Transform inverted() const
{
Transform result;
result.rot = rot.conjugated();
result.pos = result.rot.rotate(-pos);
return result;
}


Transform operator*(const Transform& rhs) const
{
return {rot.rotate(rhs.pos) + pos, rot * rhs.rot};
}


Vec3 transform(const Vec3& value) const
{
return pos + rot.rotate(value);
}


Matrix toMatrix() const;


Quat rot;
Vec3 pos;
};


struct LUMIX_ENGINE_API Matrix
{
Matrix() {}
Expand Down Expand Up @@ -80,7 +122,7 @@ struct LUMIX_ENGINE_API Matrix
}


float determinant()
float determinant() const
{
return
m14 * m23 * m32 * m41 - m13 * m24 * m32 * m41 - m14 * m22 * m33 * m41 + m12 * m24 * m33 * m41 +
Expand Down Expand Up @@ -223,9 +265,10 @@ struct LUMIX_ENGINE_API Matrix
return Vec3(m41, m42, m43);
}

void getRotation(Quat& rot) const;
Transform toTransform() { return {getTranslation(), getRotation()}; }
Quat getRotation() const;
void transpose();
Vec3 multiplyPosition(const Vec3& pos) const;
Vec3 transform(const Vec3& pos) const;
void multiply3x3(float scale);
void setIdentity();

Expand Down
Loading

0 comments on commit dbf0ed8

Please sign in to comment.