From 3936ed882c6f39c74a3b124a95d5adc5e61ab3c5 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Tue, 12 May 2015 08:17:09 -0300 Subject: -Added lookat/get_angle_to functions in 2D, and a demo, closes #1843 -Added get_viewport().get_mouse_pos() to replace Input.get_mouse_pos(), closes #1838 --- demos/2d/lookat/arrow.png | Bin 0 -> 2528 bytes demos/2d/lookat/engine.cfg | 4 ++++ demos/2d/lookat/lookat.gd | 43 +++++++++++++++++++++++++++++++++++++++++++ demos/2d/lookat/lookat.scn | Bin 0 -> 1622 bytes scene/2d/node_2d.cpp | 14 ++++++++++++++ scene/2d/node_2d.h | 3 +++ scene/main/viewport.cpp | 6 ++++++ scene/main/viewport.h | 1 + 8 files changed, 71 insertions(+) create mode 100644 demos/2d/lookat/arrow.png create mode 100644 demos/2d/lookat/engine.cfg create mode 100644 demos/2d/lookat/lookat.gd create mode 100644 demos/2d/lookat/lookat.scn diff --git a/demos/2d/lookat/arrow.png b/demos/2d/lookat/arrow.png new file mode 100644 index 0000000000..25db91e8d1 Binary files /dev/null and b/demos/2d/lookat/arrow.png differ diff --git a/demos/2d/lookat/engine.cfg b/demos/2d/lookat/engine.cfg new file mode 100644 index 0000000000..56917a39ec --- /dev/null +++ b/demos/2d/lookat/engine.cfg @@ -0,0 +1,4 @@ +[application] + +name="Look At Pointer" +main_scene="res://lookat.scn" diff --git a/demos/2d/lookat/lookat.gd b/demos/2d/lookat/lookat.gd new file mode 100644 index 0000000000..742c5b0671 --- /dev/null +++ b/demos/2d/lookat/lookat.gd @@ -0,0 +1,43 @@ + +extends Sprite + +# member variables here, example: +# var a=2 +# var b="textvar" + +const MODE_DIRECT=0 +const MODE_CONSTANT=1 +const MODE_SMOOTH=2 + +const ROTATION_SPEED = 1 +const SMOOTH_SPEED = 2.0 + +export(int,"Direct","Constant","Smooth") var mode=MODE_DIRECT + +func _process(delta): + var mpos = get_viewport().get_mouse_pos() + + if (mode==MODE_DIRECT): + + look_at(mpos) + + elif (mode==MODE_CONSTANT): + + var ang = get_angle_to(mpos) + var s = sign(ang) + ang=abs(ang) + + rotate( min(ang,ROTATION_SPEED*delta)*s ) + + elif (mode==MODE_SMOOTH): + + var ang = get_angle_to(mpos) + + rotate( ang*delta*SMOOTH_SPEED ) + +func _ready(): + # Initialization here + set_process(true) + pass + + diff --git a/demos/2d/lookat/lookat.scn b/demos/2d/lookat/lookat.scn new file mode 100644 index 0000000000..880070b4c7 Binary files /dev/null and b/demos/2d/lookat/lookat.scn differ diff --git a/scene/2d/node_2d.cpp b/scene/2d/node_2d.cpp index fc5be255ce..99c33c787d 100644 --- a/scene/2d/node_2d.cpp +++ b/scene/2d/node_2d.cpp @@ -346,6 +346,17 @@ Matrix32 Node2D::get_relative_transform(const Node *p_parent) const { return parent_2d->get_relative_transform(p_parent) * get_transform(); } + +void Node2D::look_at(const Vector2& p_pos) { + + rotate(get_angle_to(p_pos)); +} + +float Node2D::get_angle_to(const Vector2& p_pos) const { + + return (get_global_transform().affine_inverse().xform(p_pos)).atan2(); +} + void Node2D::_bind_methods() { @@ -374,6 +385,9 @@ void Node2D::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_transform","xform"),&Node2D::set_transform); ObjectTypeDB::bind_method(_MD("set_global_transform","xform"),&Node2D::set_global_transform); + ObjectTypeDB::bind_method(_MD("look_at","point"),&Node2D::look_at); + ObjectTypeDB::bind_method(_MD("get_angle_to","point"),&Node2D::get_angle_to); + ObjectTypeDB::bind_method(_MD("set_z","z"),&Node2D::set_z); ObjectTypeDB::bind_method(_MD("get_z"),&Node2D::get_z); diff --git a/scene/2d/node_2d.h b/scene/2d/node_2d.h index 74612b3c6d..8efce33cda 100644 --- a/scene/2d/node_2d.h +++ b/scene/2d/node_2d.h @@ -93,6 +93,9 @@ public: void set_z(int p_z); int get_z() const; + void look_at(const Vector2& p_pos); + float get_angle_to(const Vector2& p_pos) const; + void set_z_as_relative(bool p_enabled); bool is_z_relative() const; diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index 4d9feb3af1..3bb64e54c6 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -1177,6 +1177,11 @@ void Viewport::_vp_unhandled_input(const InputEvent& p_ev) { } +Vector2 Viewport::get_mouse_pos() const { + + return (get_final_transform().affine_inverse() * _get_input_pre_xform()).xform(Input::get_singleton()->get_mouse_pos()); +} + void Viewport::warp_mouse(const Vector2& p_pos) { Vector2 gpos = (get_final_transform().affine_inverse() * _get_input_pre_xform()).affine_inverse().xform(p_pos); @@ -1377,6 +1382,7 @@ void Viewport::_bind_methods() { ObjectTypeDB::bind_method(_MD("is_audio_listener_2d","enable"), &Viewport::is_audio_listener_2d); ObjectTypeDB::bind_method(_MD("set_render_target_to_screen_rect"), &Viewport::set_render_target_to_screen_rect); + ObjectTypeDB::bind_method(_MD("get_mouse_pos"), &Viewport::get_mouse_pos); ObjectTypeDB::bind_method(_MD("warp_mouse","to_pos"), &Viewport::warp_mouse); ADD_PROPERTY( PropertyInfo(Variant::RECT2,"rect"), _SCS("set_rect"), _SCS("get_rect") ); diff --git a/scene/main/viewport.h b/scene/main/viewport.h index fba6725610..c3c339ac5d 100644 --- a/scene/main/viewport.h +++ b/scene/main/viewport.h @@ -252,6 +252,7 @@ public: void set_render_target_to_screen_rect(const Rect2& p_rect); Rect2 get_render_target_to_screen_rect() const; + Vector2 get_mouse_pos() const; void warp_mouse(const Vector2& p_pos); void set_physics_object_picking(bool p_enable); -- cgit v1.2.3