diff --git a/demos/2d/lookat/arrow.png b/demos/2d/lookat/arrow.png new file mode 100644 index 00000000000..25db91e8d17 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 00000000000..56917a39ec7 --- /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 00000000000..742c5b06710 --- /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 00000000000..880070b4c7c 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 fc5be255ceb..99c33c787d2 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 74612b3c6da..8efce33cdac 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 4d9feb3af1f..3bb64e54c67 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 fba67256103..c3c339ac5de 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);