-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
This commit is contained in:
parent
4363798608
commit
3936ed882c
8 changed files with 71 additions and 0 deletions
BIN
demos/2d/lookat/arrow.png
Normal file
BIN
demos/2d/lookat/arrow.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.5 KiB |
4
demos/2d/lookat/engine.cfg
Normal file
4
demos/2d/lookat/engine.cfg
Normal file
|
@ -0,0 +1,4 @@
|
|||
[application]
|
||||
|
||||
name="Look At Pointer"
|
||||
main_scene="res://lookat.scn"
|
43
demos/2d/lookat/lookat.gd
Normal file
43
demos/2d/lookat/lookat.gd
Normal file
|
@ -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
|
||||
|
||||
|
BIN
demos/2d/lookat/lookat.scn
Normal file
BIN
demos/2d/lookat/lookat.scn
Normal file
Binary file not shown.
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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") );
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue