• 2D
  • Line2D start stop positions programmatically

I'm creating a laser effect based on drawing a Line2D. It's a board element that has a fixed start and end location. I used a 32x32 sprite, where the laser port is halfway down one side of the sprite and should shoot across the sprite maybe across more than just the sprite itself.

When I'm not shooting I remove the points from the Line2D - perfect the laser line disappears. But when I use Vector2D(16,0) as the start point the line starts in the middle of my sprite. Some experimentation says I need (-8,0) as the start point.

Where does this magic number -8 come from when my sprite is 32x32?

The second endpoint to create a horizontal line that spans the sprite length turns out to be (16,0). Why is the distance only 24 rather than my expected 32?

My sprite is 32x32 and covers 4 grid points by 4 grid points in the 2D editor.

What concept am I missing?

Here is more information:

I"m using the following code

$Tile/Laser1.add_point(Vector2(-13,1))
print("GunportA = ", $Tile/Laser1.points)
$Tile/Laser1.add_point($Tile/GunPortPosition1.position + Vector2(32,0))
print("Laser1 coords are:",$Tile/Laser1.get_point_position(0), $Tile/Laser1.get_point_position(1))

This is the output

GunportA = [(-13, 1)]
Laser1 coords are:(-13, 1)(19.1024, -1.05512)

How is this happening? I clearly added 32 to the x coord but it shows a difference of 6 in output, y grew a little too when I added nothing.

Clearly, there is s transform at work. But how do I account for this? The display is also off if I used a point2D. On the sprite, it's in the exact right spot, but when I use the point2D as the start point for my laser, it gets offset from where I know it's supposed to be.

What am I doing wrong?

From what I know, the points in a Line2D are relative to its origin, so all the coordinates used need to be relative to the Line2D node, not relative to another node. This is likely why you are getting different results than you are expecting.

I'd recommend looking at this page on the Godot documentation for Vector Math to learn more about transforms, relative positions, and all that stuff.

How is this happening? I clearly added 32 to the x coord but it shows a difference of 6 in output, y grew a little too when I added nothing.

The difference is 32. It went from negative 13 to positive 19.1024, which is a 32 difference.

If you are wanting to get a point 32 coordinates different on the X axis relative to the GunPortPosition1 node, then you'll want to use code like this (untested, written in a hurry):

$Tile/Laser1.add_point(Vector2(-13,1))
print("GunportA = ", $Tile/Laser1.points)

# Get the global position of the GunPort and then use the to_local function
# to make it relative to the Line2D node
# var end_point = $Tile/Laser1.to_local($Tile/GunPortPosition1.global_position)
# However, since we want an offset, we can do it this way instead:
var end_point = $Tile/Laser1.to_local($Tile/GunPortPosition1.to_global(Vector2(32, 0))

# Add the point
$Tile/Laser1.add_point(end_point)

print("Laser1 coords are:",$Tile/Laser1.get_point_position(0), $Tile/Laser1.get_point_position(1))

thank you, I'm familiar with matrix math and vectors. But implementing the right frame of mind in Godot on the node is a bit confusing. Your example gives me insight into how I should be manipulating the lasers I want.

I figured out part of my problem!

The Line2D had a transform and a PointArray for the start and endpoint of my laser line. The transform was messing with my mind as it was altering the location of the points relative to the transform origin. Setting the transform to (0,0) made everything intuitive again.