From a93e33f5c8afc8ce2e9c1674718ff103bfae49b0 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Sun, 22 Mar 2015 10:33:58 -0300 Subject: [PATCH] added spatial and node2d helper methods to perform operations such as translaiton, rotation, etc directly on nodes. --- scene/2d/light_2d.cpp | 10 ++-- scene/2d/light_2d.h | 2 +- scene/2d/node_2d.cpp | 49 +++++++++++++------ scene/2d/node_2d.h | 8 +++- scene/3d/camera.cpp | 18 ------- scene/3d/camera.h | 2 - scene/3d/spatial.cpp | 106 ++++++++++++++++++++++++++++++++++++++++++ scene/3d/spatial.h | 14 ++++++ 8 files changed, 166 insertions(+), 43 deletions(-) diff --git a/scene/2d/light_2d.cpp b/scene/2d/light_2d.cpp index 8f6907798ff..949c952b399 100644 --- a/scene/2d/light_2d.cpp +++ b/scene/2d/light_2d.cpp @@ -23,7 +23,7 @@ Rect2 Light2D::get_item_rect() const { Size2i s; - s = texture->get_size()*scale; + s = texture->get_size()*_scale; Point2i ofs=texture_offset; ofs-=s/2; @@ -98,8 +98,8 @@ float Light2D::get_height() const { void Light2D::set_scale( float p_scale) { - scale=p_scale; - VS::get_singleton()->canvas_light_set_scale(canvas_light,scale); + _scale=p_scale; + VS::get_singleton()->canvas_light_set_scale(canvas_light,_scale); item_rect_changed(); } @@ -107,7 +107,7 @@ void Light2D::set_scale( float p_scale) { float Light2D::get_scale() const { - return scale; + return _scale; } void Light2D::set_z_range_min( int p_min_z) { @@ -322,7 +322,7 @@ Light2D::Light2D() { shadow=false; color=Color(1,1,1); height=0; - scale=1.0; + _scale=1.0; z_min=-1024; z_max=1024; layer_min=0; diff --git a/scene/2d/light_2d.h b/scene/2d/light_2d.h index 6eb6ad52373..95196af5e0b 100644 --- a/scene/2d/light_2d.h +++ b/scene/2d/light_2d.h @@ -12,7 +12,7 @@ private: bool shadow; Color color; float height; - float scale; + float _scale; int z_min; int z_max; int layer_min; diff --git a/scene/2d/node_2d.cpp b/scene/2d/node_2d.cpp index 36b6b220b32..0b098f0cad4 100644 --- a/scene/2d/node_2d.cpp +++ b/scene/2d/node_2d.cpp @@ -65,7 +65,7 @@ void Node2D::edit_set_state(const Variant& p_state) { pos = state[0]; angle = state[1]; - scale = state[2]; + _scale = state[2]; _update_transform(); _change_notify("transform/rot"); _change_notify("transform/scale"); @@ -93,11 +93,11 @@ void Node2D::edit_set_rect(const Rect2& p_edit_rect) { Point2 new_pos = p_edit_rect.pos + p_edit_rect.size*zero_offset;//p_edit_rect.pos - r.pos; Matrix32 postxf; - postxf.set_rotation_and_scale(angle,scale); + postxf.set_rotation_and_scale(angle,_scale); new_pos = postxf.xform(new_pos); pos+=new_pos; - scale*=new_scale; + _scale*=new_scale; _update_transform(); _change_notify("transform/scale"); @@ -118,14 +118,14 @@ void Node2D::_update_xform_values() { pos=_mat.elements[2]; angle=_mat.get_rotation(); - scale=_mat.get_scale(); + _scale=_mat.get_scale(); _xform_dirty=false; } void Node2D::_update_transform() { Matrix32 mat(angle,pos); - _mat.set_rotation_and_scale(angle,scale); + _mat.set_rotation_and_scale(angle,_scale); _mat.elements[2]=pos; VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(),_mat); @@ -161,11 +161,11 @@ void Node2D::set_scale(const Size2& p_scale) { if (_xform_dirty) ((Node2D*)this)->_update_xform_values(); - scale=p_scale; - if (scale.x==0) - scale.x=CMP_EPSILON; - if (scale.y==0) - scale.y=CMP_EPSILON; + _scale=p_scale; + if (_scale.x==0) + _scale.x=CMP_EPSILON; + if (_scale.y==0) + _scale.y=CMP_EPSILON; _update_transform(); _change_notify("transform/scale"); @@ -187,7 +187,7 @@ Size2 Node2D::get_scale() const { if (_xform_dirty) ((Node2D*)this)->_update_xform_values(); - return scale; + return _scale; } void Node2D::_set_rotd(float p_angle) { @@ -224,11 +224,27 @@ Rect2 Node2D::get_item_rect() const { return Rect2(Point2(-32,-32),Size2(64,64)); } -void Node2D::rotate(float p_degrees) { +void Node2D::rotate(float p_radians) { - set_rot( get_rot() + p_degrees); + set_rot( get_rot() + p_radians); } +void Node2D::translate(const Vector2& p_amount) { + + set_pos( get_pos() + p_amount ); +} + +void Node2D::global_translate(const Vector2& p_amount) { + + set_global_pos( get_global_pos() + p_amount ); +} + +void Node2D::scale(const Vector2& p_amount) { + + set_scale( get_scale() * p_amount ); +} + + void Node2D::move_x(float p_delta,bool p_scaled){ Matrix32 t = get_transform(); @@ -345,9 +361,12 @@ void Node2D::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_rot"),&Node2D::get_rot); ObjectTypeDB::bind_method(_MD("get_scale"),&Node2D::get_scale); - ObjectTypeDB::bind_method(_MD("rotate","degrees"),&Node2D::rotate); + ObjectTypeDB::bind_method(_MD("rotate","radians"),&Node2D::rotate); ObjectTypeDB::bind_method(_MD("move_local_x","delta","scaled"),&Node2D::move_x,DEFVAL(false)); ObjectTypeDB::bind_method(_MD("move_local_y","delta","scaled"),&Node2D::move_y,DEFVAL(false)); + ObjectTypeDB::bind_method(_MD("translate","offset"),&Node2D::translate); + ObjectTypeDB::bind_method(_MD("global_translate","offset"),&Node2D::global_translate); + ObjectTypeDB::bind_method(_MD("scale","ratio"),&Node2D::scale); ObjectTypeDB::bind_method(_MD("set_global_pos","pos"),&Node2D::set_global_pos); ObjectTypeDB::bind_method(_MD("get_global_pos"),&Node2D::get_global_pos); @@ -379,7 +398,7 @@ Node2D::Node2D() { angle=0; - scale=Vector2(1,1); + _scale=Vector2(1,1); _xform_dirty=false; z=0; z_relative=true; diff --git a/scene/2d/node_2d.h b/scene/2d/node_2d.h index 7b059008c26..39a1061195d 100644 --- a/scene/2d/node_2d.h +++ b/scene/2d/node_2d.h @@ -37,7 +37,7 @@ class Node2D : public CanvasItem { Point2 pos; float angle; - Size2 scale; + Size2 _scale; int z; bool z_relative; @@ -72,9 +72,12 @@ public: void set_rot(float p_angle); void set_scale(const Size2& p_scale); - void rotate(float p_degrees); + void rotate(float p_radians); void move_x(float p_delta,bool p_scaled=false); void move_y(float p_delta,bool p_scaled=false); + void translate(const Vector2& p_amount); + void global_translate(const Vector2& p_amount); + void scale(const Vector2& p_amount); Point2 get_pos() const; float get_rot() const; @@ -96,6 +99,7 @@ public: Matrix32 get_relative_transform(const Node *p_parent) const; + Matrix32 get_transform() const; Node2D(); diff --git a/scene/3d/camera.cpp b/scene/3d/camera.cpp index 1109139180b..db69182ca07 100644 --- a/scene/3d/camera.cpp +++ b/scene/3d/camera.cpp @@ -680,8 +680,6 @@ void Camera::_bind_methods() { ObjectTypeDB::bind_method( _MD("get_projection"),&Camera::get_projection ); ObjectTypeDB::bind_method( _MD("set_visible_layers","mask"),&Camera::set_visible_layers ); ObjectTypeDB::bind_method( _MD("get_visible_layers"),&Camera::get_visible_layers ); - ObjectTypeDB::bind_method( _MD("look_at","target","up"),&Camera::look_at ); - ObjectTypeDB::bind_method( _MD("look_at_from_pos","pos","target","up"),&Camera::look_at_from_pos ); ObjectTypeDB::bind_method(_MD("set_environment","env:Environment"),&Camera::set_environment); ObjectTypeDB::bind_method(_MD("get_environment:Environment"),&Camera::get_environment); ObjectTypeDB::bind_method(_MD("set_keep_aspect_mode","mode"),&Camera::set_keep_aspect_mode); @@ -752,22 +750,6 @@ Vector Camera::get_frustum() const { -void Camera::look_at(const Vector3& p_target, const Vector3& p_up_normal) { - - Transform lookat; - lookat.origin=get_camera_transform().origin; - lookat=lookat.looking_at(p_target,p_up_normal); - set_global_transform(lookat); -} - -void Camera::look_at_from_pos(const Vector3& p_pos,const Vector3& p_target, const Vector3& p_up_normal) { - - Transform lookat; - lookat.origin=p_pos; - lookat=lookat.looking_at(p_target,p_up_normal); - set_global_transform(lookat); - -} void Camera::set_v_offset(float p_offset) { diff --git a/scene/3d/camera.h b/scene/3d/camera.h index 950688dfda3..de032820219 100644 --- a/scene/3d/camera.h +++ b/scene/3d/camera.h @@ -139,8 +139,6 @@ public: void set_keep_aspect_mode(KeepAspect p_aspect); KeepAspect get_keep_aspect_mode() const; - void look_at(const Vector3& p_target, const Vector3& p_up_normal); - void look_at_from_pos(const Vector3& p_pos,const Vector3& p_target, const Vector3& p_up_normal); void set_v_offset(float p_offset); float get_v_offset() const; diff --git a/scene/3d/spatial.cpp b/scene/3d/spatial.cpp index a5b009823cd..edca62ece51 100644 --- a/scene/3d/spatial.cpp +++ b/scene/3d/spatial.cpp @@ -593,6 +593,91 @@ bool Spatial::_is_visible_() const { return !is_hidden(); } +void Spatial::rotate(const Vector3& p_normal,float p_radians) { + + Transform t =get_transform(); + t.basis.rotate(p_normal,p_radians); + set_transform(t); +} + +void Spatial::rotate_x(float p_radians) { + + Transform t =get_transform(); + t.basis.rotate(Vector3(1,0,0),p_radians); + set_transform(t); + +} + +void Spatial::rotate_y(float p_radians){ + + Transform t =get_transform(); + t.basis.rotate(Vector3(0,1,0),p_radians); + set_transform(t); + +} +void Spatial::rotate_z(float p_radians){ + + Transform t =get_transform(); + t.basis.rotate(Vector3(0,0,1),p_radians); + set_transform(t); + +} + +void Spatial::translate(const Vector3& p_offset){ + + Transform t =get_transform(); + t.origin+=p_offset; + set_transform(t); + +} +void Spatial::scale(const Vector3& p_ratio){ + + Transform t =get_transform(); + t.basis.scale(p_ratio); + set_transform(t); + +} +void Spatial::global_rotate(const Vector3& p_normal,float p_radians){ + + Matrix3 rotation(p_normal,p_radians); + Transform t = get_global_transform(); + t.basis= rotation * t.basis; + set_global_transform(t); + +} +void Spatial::global_translate(const Vector3& p_offset){ + Transform t = get_global_transform(); + t.origin+=p_offset; + set_global_transform(t); + +} + +void Spatial::orthonormalize() { + + Transform t = get_transform(); + t.orthonormalize(); + set_transform(t); + +} + + +void Spatial::look_at(const Vector3& p_target, const Vector3& p_up_normal) { + + Transform lookat; + lookat.origin=get_global_transform().origin; + lookat=lookat.looking_at(p_target,p_up_normal); + set_global_transform(lookat); +} + +void Spatial::look_at_from_pos(const Vector3& p_pos,const Vector3& p_target, const Vector3& p_up_normal) { + + Transform lookat; + lookat.origin=p_pos; + lookat=lookat.looking_at(p_target,p_up_normal); + set_global_transform(lookat); + +} + void Spatial::_bind_methods() { @@ -633,6 +718,27 @@ void Spatial::_bind_methods() { ObjectTypeDB::bind_method(_MD("_set_visible_"), &Spatial::_set_visible_); ObjectTypeDB::bind_method(_MD("_is_visible_"), &Spatial::_is_visible_); + void rotate(const Vector3& p_normal,float p_radians); + void rotate_x(float p_radians); + void rotate_y(float p_radians); + void rotate_z(float p_radians); + void translate(const Vector3& p_offset); + void scale(const Vector3& p_ratio); + void global_rotate(const Vector3& p_normal,float p_radians); + void global_translate(const Vector3& p_offset); + + ObjectTypeDB::bind_method( _MD("rotate","normal","radians"),&Spatial::rotate ); + ObjectTypeDB::bind_method( _MD("global_rotate","normal","radians"),&Spatial::global_rotate ); + ObjectTypeDB::bind_method( _MD("rotate_x","radians"),&Spatial::rotate_x ); + ObjectTypeDB::bind_method( _MD("rotate_y","radians"),&Spatial::rotate_y ); + ObjectTypeDB::bind_method( _MD("rotate_z","radians"),&Spatial::rotate_z ); + ObjectTypeDB::bind_method( _MD("translate","offset"),&Spatial::translate ); + ObjectTypeDB::bind_method( _MD("global_translate","offset"),&Spatial::global_translate ); + ObjectTypeDB::bind_method( _MD("orthonormalize"),&Spatial::orthonormalize ); + + ObjectTypeDB::bind_method( _MD("look_at","target","up"),&Spatial::look_at ); + ObjectTypeDB::bind_method( _MD("look_at_from_pos","pos","target","up"),&Spatial::look_at_from_pos ); + BIND_CONSTANT( NOTIFICATION_TRANSFORM_CHANGED ); BIND_CONSTANT( NOTIFICATION_ENTER_WORLD ); BIND_CONSTANT( NOTIFICATION_EXIT_WORLD ); diff --git a/scene/3d/spatial.h b/scene/3d/spatial.h index 49ecf5779b7..d7addbb7922 100644 --- a/scene/3d/spatial.h +++ b/scene/3d/spatial.h @@ -167,6 +167,20 @@ public: Transform get_relative_transform(const Node *p_parent) const; + void rotate(const Vector3& p_normal,float p_radians); + void rotate_x(float p_radians); + void rotate_y(float p_radians); + void rotate_z(float p_radians); + void translate(const Vector3& p_offset); + void scale(const Vector3& p_ratio); + void global_rotate(const Vector3& p_normal,float p_radians); + void global_translate(const Vector3& p_offset); + + void look_at(const Vector3& p_target, const Vector3& p_up_normal); + void look_at_from_pos(const Vector3& p_pos,const Vector3& p_target, const Vector3& p_up_normal); + + void orthonormalize(); + void show(); void hide(); bool is_visible() const;