mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-15 00:11:55 +07:00
node: translation, rotation and scale functions
This commit is contained in:
@@ -22,20 +22,21 @@ namespace e2d
|
||||
static node_iptr create();
|
||||
static node_iptr create(const node_iptr& parent);
|
||||
|
||||
//
|
||||
// transform
|
||||
//
|
||||
|
||||
void transform(const t3f& transform) noexcept;
|
||||
const t3f& transform() const noexcept;
|
||||
|
||||
void translation(const v3f& translation) noexcept;
|
||||
const v3f& translation() const noexcept;
|
||||
|
||||
void rotation(const q4f& rotation) noexcept;
|
||||
const q4f& rotation() const noexcept;
|
||||
|
||||
void scale(const v3f& scale) noexcept;
|
||||
const v3f& scale() const noexcept;
|
||||
|
||||
const m4f& local_matrix() const noexcept;
|
||||
const m4f& world_matrix() const noexcept;
|
||||
|
||||
//
|
||||
// container
|
||||
//
|
||||
|
||||
node_iptr root() noexcept;
|
||||
const_node_iptr root() const noexcept;
|
||||
|
||||
|
||||
@@ -46,15 +46,42 @@ namespace e2d
|
||||
return transform_;
|
||||
}
|
||||
|
||||
void node::translation(const v3f& translation) noexcept {
|
||||
transform_.translation = translation;
|
||||
mark_dirty_local_matrix_();
|
||||
}
|
||||
|
||||
const v3f& node::translation() const noexcept {
|
||||
return transform_.translation;
|
||||
}
|
||||
|
||||
void node::rotation(const q4f& rotation) noexcept {
|
||||
transform_.rotation = rotation;
|
||||
mark_dirty_local_matrix_();
|
||||
}
|
||||
|
||||
const q4f& node::rotation() const noexcept {
|
||||
return transform_.rotation;
|
||||
}
|
||||
|
||||
void node::scale(const v3f& scale) noexcept {
|
||||
transform_.scale = scale;
|
||||
mark_dirty_local_matrix_();
|
||||
}
|
||||
|
||||
const v3f& node::scale() const noexcept {
|
||||
return transform_.scale;
|
||||
}
|
||||
|
||||
const m4f& node::local_matrix() const noexcept {
|
||||
if ( math::check_and_clear_all_flags(flags_, fm_dirty_local_matrix) ) {
|
||||
if ( math::check_and_clear_any_flags(flags_, fm_dirty_local_matrix) ) {
|
||||
update_local_matrix_();
|
||||
}
|
||||
return local_matrix_;
|
||||
}
|
||||
|
||||
const m4f& node::world_matrix() const noexcept {
|
||||
if ( math::check_and_clear_all_flags(flags_, fm_dirty_world_matrix) ) {
|
||||
if ( math::check_and_clear_any_flags(flags_, fm_dirty_world_matrix) ) {
|
||||
update_world_matrix_();
|
||||
}
|
||||
return world_matrix_;
|
||||
@@ -92,14 +119,11 @@ namespace e2d
|
||||
if ( !parent ) {
|
||||
return has_parent();
|
||||
}
|
||||
const node* n = this;
|
||||
while ( n ) {
|
||||
if ( n->parent_ == parent ) {
|
||||
return true;
|
||||
}
|
||||
n = n->parent_;
|
||||
const node* p = parent_;
|
||||
while ( p && p != parent ) {
|
||||
p = p->parent_;
|
||||
}
|
||||
return false;
|
||||
return !!p;
|
||||
}
|
||||
|
||||
bool node::remove_from_parent() noexcept {
|
||||
|
||||
@@ -159,10 +159,14 @@ TEST_CASE("node") {
|
||||
|
||||
auto pp = node::create();
|
||||
REQUIRE_FALSE(n->has_parent(pp));
|
||||
REQUIRE_FALSE(pp->has_parent(pp));
|
||||
|
||||
pp->add_child(p);
|
||||
REQUIRE(n->has_parent(p));
|
||||
REQUIRE(n->has_parent(pp));
|
||||
|
||||
REQUIRE_FALSE(n->has_parent(n));
|
||||
REQUIRE_FALSE(pp->has_parent(pp));
|
||||
}
|
||||
SECTION("auto_remove/remove_all_children") {
|
||||
{
|
||||
@@ -822,10 +826,10 @@ TEST_CASE("node") {
|
||||
SECTION("world_matrix") {
|
||||
{
|
||||
auto p = node::create();
|
||||
p->transform(math::make_translation_trs3(v3f{10.f,0.f,0.f}));
|
||||
p->translation({10.f,0.f,0.f});
|
||||
|
||||
auto n = node::create(p);
|
||||
n->transform(math::make_translation_trs3(v3f{20.f,0.f,0.f}));
|
||||
n->translation({20.f,0.f,0.f});
|
||||
|
||||
auto v = v4f(5.f,0.f,0.f,1.f);
|
||||
REQUIRE(v * n->world_matrix() == v4f{35.f,0.f,0.f,1.f});
|
||||
@@ -837,7 +841,7 @@ TEST_CASE("node") {
|
||||
}
|
||||
{
|
||||
auto n = node::create();
|
||||
n->transform(math::make_translation_trs3(v3f{20.f,0.f,0.f}));
|
||||
n->translation({20.f,0.f,0.f});
|
||||
REQUIRE(n->world_matrix() ==
|
||||
math::make_translation_matrix4(20.f,0.f,0.f));
|
||||
|
||||
@@ -850,7 +854,7 @@ TEST_CASE("node") {
|
||||
}
|
||||
{
|
||||
auto p1 = node::create();
|
||||
p1->transform(math::make_translation_trs3(v3f{10.f,0.f,0.f}));
|
||||
p1->translation({10.f,0.f,0.f});
|
||||
|
||||
auto p2 = node::create();
|
||||
p2->transform(math::make_translation_trs3(v3f{20.f,0.f,0.f}));
|
||||
|
||||
Reference in New Issue
Block a user