2
\$\begingroup\$

I'm using Vector2.distance_to to get the distance between a MeshInstance and a KinematicBody but the results aren't as expected.

I'm using distance_to like so:

var player_global_transform = $KinematicBody.get_global_transform().origin var player_position = Vector2(player_global_transform.x, player_global_transform.z) # Here I loop through the planes and compare positions but to simplify it I'll just show # the one. var plane_global_transform = $MeshInstance.get_global_transform().origin var plane_position = Vector2(plane_global_transform.x, plane_global_transform.z) var player_distance_to_plane = player_position.distance_to(plane_position) 

Also I'm using Vector2s because I don't care about Y values for this calculation.

Lets say I have 9 mesh planes (240x240) in a grid and the player is standing in the middle mesh of them:

initial grid

The distance from the KinematicBody and the middle square is (0, 0) as the player is right in the middle of it. Now if I move over a square to the left:

grid with extra column

The distance returned is (240, 0) which is expected as I'm now 1 mesh plane distance away from the center of the previous mesh. Now if I extend the grid and add 3 more meshes to the left and move over to that one:

grid with two extra columns

The distance is now (360, 0) instead of (480, 0) and I can't seem to figure out why. It also seems that if I add another column of plane meshes to the left and move over another square, it's still (360, 0).

\$\endgroup\$
2
  • \$\begingroup\$ It sounds like, by creating more planes, you've also shifted their locations. Can you share a minimal complete verifiable example of the problem, where we can see the code that places your planes alongside the code that calculates distances to them? \$\endgroup\$ Commented Jun 26, 2020 at 20:41
  • \$\begingroup\$ Which, exactly, is the middle plane from which you measure when there are four rows? Notice that 240 * 1.5 = 360, you seem to be measuring from the middle of the grid. \$\endgroup\$ Commented Sep 28, 2020 at 10:04

1 Answer 1

0
\$\begingroup\$

You started with a 3x3 grid/mesh of unit 240px positioning the dot in its center (0,0).

Next you move the dot 1 unit (240, 0) to the left which is the center of the first column.

By adding a column to the right the mesh center point the mesh is now 4 units wide moving the dot half a unit (120px) relative to the new center which equals to 240px + 120px = 360px aka (-360,0)

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.