How to calculate distance from point to point and include scoring

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By HashTag

Imgur: The magic of the Internet <= Example

Hey guys,
I want to create a circle as a target 2d and being able to catch the mouse_pressed_on on that circle. If it is pressed in the middle it should give max amount of points per hit, let’s say 100, if it is pressed away from the center (inside the circle) it should give x points depending on the distance from the center point, if outside = 0points.
I am pretty new to the Godot and I don’t know how to solve this issue. I have no script for this scene yet and I am not sure if I should use CollisionShape2D or something else for the circle and etc. I would appreciate it if anyone could help it out and guide me in solving this issue. Thanks, fellas.

:bust_in_silhouette: Reply From: kidscancode

Use an Area2D with a CollisionShape2D set to “Circle”. Area2D has input capability - connect its input_event signal, and check for mouse click events.

Next, measure the distance from the click position to the area’s position (the center of the circle), and assign your points.

func input_event(viewport, event, shape_index):
    if event is InputEventMouseButton and event.pressed:
        var distance_from_center = position.distance_to(event.position)

Alternatively, you could add multiple CollisionShape2D circles of varying radius, and use the shape_index property to see which one was clicked, using that to assign points.

Thanks for your answer, I am getting the distance from the center! Amazing :slight_smile: Maybe you know the syntax of getting the center? Because I want to assign some points to the very center of the Area2D. var center = ?
Thanks in advance!

HashTag | 2020-03-06 08:00

The center of the collision shape is its position. But this will probably be (0, 0) if you call it on the collision shape since it’s relative to the parent. Use the area’s position for that. Or if you need global coordinates, global_position.

kidscancode | 2020-03-06 17:30

I see, Thanks a lot for your time explaining it to me! Have a nice weekend mate

HashTag | 2020-03-07 22:40

Sorry to disturb you, but maybe you could help me with another issue? I need dynamic “Sprite” with the “aim projectile” attached to one of his arms and after every click on the target “aim projectile” the “Sprite” would change his posture together with the placement of target attached to his arm. My first question would be, how could I achieve such “Dynamic” sprite and how could I attached my already created and working “Aim projectile”. I hope you are not to busy and will be able to help me out.

EXAMPLE — Imgur: The magic of the Internet

HashTag | 2020-03-09 15:33

If anyone will need the answer to this question - Dynamic Sprite with Aim attached on his arm - Archive - Godot Forum

HashTag | 2020-03-10 14:39