0
\$\begingroup\$

So i have two coordinate planes. One is player and mouse click position relative to player. Other is the world map.

I need to find the point on the world map, where the mouse clicks.

The map is generated from a 64*64 heightmap, so for now i want to find the mouse click inside the 64*64 bounds.

Im having trouble determining the coordinates at the world map, where the mouse clicked.

upper - player and mouse click, lower - world map

The map has a multiplier of 10. Im not really sure how it is applied, but when it is 10, the world map is in bounds -10,-10 to 10, 10. When it is 20, it is in -20,-20 to 20, 20, when 50 then in -50,-50 to 50,50. Those are click coordinates in relation to player.

To get the world vertex i need to supply a coordinate (0-63, 0-63).

How do i transform the mouse click coordinates so that they are aproppriate to the world map?

\$\endgroup\$
1
\$\begingroup\$

This looks like simple multiplication & division, no?

float2 mapHalfExtents = new float2(10.0f, 10.0f);
int2 mapVertexCount = new int2(64, 64);

int2 GetClosestVertexCoordinates(float2 clickedWorldPoint) {
    // Reduce to the range -0.5...0.5
    float2 normalizedPosition = clickedWorldPoint / (2.0f * mapHalfExtents);

    // Shift to 0...1 (placing the bottom-left corner at the origin)
    normalizedPosition += new float2(0.5f, 0.5f);

    // Map to the range 0...64
    float2 mapSpace = normalizedPosition * mapVertexCount;

    return RoundToInt2(mapSpace);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.