Topic was automatically imported from the old Question2Answer platform.
Asked By
curioussavage
Hi I am trying to use a line2D to draw a line between two sprites for a second to indicate a laser being fired. My problem is that the “lasers” show up positioned wrong. The target in the example below is a KinematicBody2D (same as the parent of this Line2D node. Its the same scene). I have some other logic around setting destinations for these ships and selecting them that uses global_position too and it works fine.
I think I’m stuck because I’m still getting the hang of thinking in terms of these different coordinate systems.
extends Line2D
var damage = 0
var laser_time = 1
var timer = null
var gun_target = null
func _ready():
timer = get_node("gun_timer")
func _physics_process(delta):
remove_point(1)
remove_point(0)
if timer.is_stopped():
gun_target = null
else:
add_point(get_parent().global_position)
add_point(gun_target.global_position)
func shoot(target):
gun_target = target
target.take_damage(damage)
timer.start()
I do something similar, I have a simple "chain lightning effect that makes a line move between objects. This is the draw method (3.0). I move the object towards the object. This draws a line from where the object is to the other object.
func _draw():
var from = _origin_pos - get_global_position()
var to = Vector2(0, 0)
from = from.normalized() * 100
to = to.normalized() * 100
# blue
draw_line(from, to , Color(0, 0, 1), 5)
# white
draw_line(from, to, Color(1, 1, 1), 1)
And this is how I move the object toward its destination. I call this from _process() but should be fine in _process_physics…actually maybe that’s where it SHOULD be.
func _move_towards_target(delta):
# _wr_target_obj is a weak reference to the object I'm trying to
# move towards.
var target_obj = _wr_target_obj.get_ref()
var _target_pos = target_obj.get_global_position()
var pos = get_global_position()
var dist_to = get_global_position().distance_to(_target_pos)
var movement = _target_pos - pos
movement = movement.normalized()
movement = movement * delta * CHAIN_SPEED
move_and_collide(movement)
I’m glad you posted this because making sure I don’t keep around refs to nodes is one thing I have been meaning to fix. Didn’t know about get_ref. I don’t think this solves my root problem though - that the coordinates I am using for my line are wrong. If I were to follow this I would still be using the same positions. I am using global_position but It doesn’t seem like that is working. It does work for the movement of ships ( they move to where I click on the screen. Even when the camera is scrolled.)
I suspect I am not accounting for how the camera affects positions. Or it could be something else.
curioussavage | 2018-03-08 05:33
Have you tried tacking on the normalized()? I remember knowing how this all worked when I made that method. It has since left me.
Looks like the solution was to call to_local on both Vector2 before adding the points. This, sort of, makes sense to me. Probably need to review the docs about the coordinate system etc.