I want to make a code with which you can get length and rotation of line between two points

Godot Version

4.2.2

Question

I made a post already, but it was quite undefined, so making another one. So like I said I’m trying to make a code for getting length and rotation of line between two points (start point is created then you press mouse button and end point is created then you release it). Like this:
. - End point
|
. - Start point
And from this I want to get length between the points (so one “|”, of course I need it in Godot measurements, so meters) and direction (upwards, it would be best in degrees).
Please help, haha, I need a suggestion how could I do such a thing.

Use a.distance_to(b) for distance measuring, and a.angle_to_point(b) for angle measuring in radians. And you might want to convert radians to degrees using the rad_to_deg() function.

Thanks, and any tips on how to save the position of the middle of the screen and rotate it as you rotate the camera?

I’m not pretty sure what you mean.

So I want to make a system which makes two points, like I said and I want them to be based on the direction you are looking at. So then you are holding down mouse and rotate the camera (change the direction your player is looking at) you are drawing a straight line (between your mouse and the position at which you started holding down the mouse). For this to work I need to somehow draw a 2d line based on your camera rotation and I can’t figure out how to.

Maybe I can actually do it, just thought of a way to, but I still might need help on this.

Nope, didn’t succeed. At least I thought of more simplified description of what I’m trying to do: Add two points of which one is the middle of the screen right now and the other one is the position of the middle of the screen at the start (then mouse button was pressed).

The camera’s position just is the world position of the center of the screen.

ah, I get that, I need help with creating points only with code which can be measured and moved by code

Then what’s the issue?

I don’t know how to :\

Specificly?

creating points with code which can be rotated/measured (saving positions)

Oh

extends Node

@export var camera: Camera2D

var start_point: Node2D
var end_point: Node2D

func start(point: Vector2) -> void:
    start_point = Node2D.new()
    add_child(start_point)
    start_point.global_position = point
    camera.global_position = point

func end(point: Vector2) -> void:
    end_point = Node2D.new()
    add_child(end_point)
    end_point.global_position = point
    camera.global_position = point

Thanks.

Make sure you understand these code. Also, if you want to use your own scene for the points, you can always replace them as follows:

@export var my_point_scene: PackedScene

# This var will store the root node of your scene, so if the root node
# of your scene is of other types, such as "Sprite2D", ensure you replace
# the "Node2D" type annotation with the type you actually used.
var start_point: Node2D

func start(point: Vector2) -> void:
    start_point = my_point_scene.instantiate()

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.