How do I get mouse clicked position on chessboard mesh

Hi all,

I’m using the chessboard from the marketplace

when I click on a square on the board I can get the mouse position in screen coords but it isn’t the offset into the chessboard itself so I can’t yet convert that to a square clicked such as (x,y)=(3,4) where x = 1 to 8, y=1 to 8

Any ideas how I can get the mouse coords in relation to the chess board only?

I’ve been searching the forums for a few days now without working it out.

Kind regards,
Max

You could convert mouse *Screenspace *to Worldspace, trace and do InverseTransformLocation, this will spit out the mesh’s local coordinates (the offset you’re after), as seen below:

From here, you should be able to divide it into 1-8 blocks.

Thanks Everynone.

I assume I will need the get player controller to the left of ConvertMouseLocationToWorldSpace.

That’s what I currently have anyway from my messing about. I was trying the LineTraceForObjects and breakHitResult, but I see the line trace more going toward the camera rather than the board position being clicked.

I wasn’t aware of the GetActorTransform or InverseTransformLocation nodes, I will try to work out what they do, and give your technique a go today.

Many thanks, I will report back.

Cheers,
Max

Update:

I’m now getting the board x,y values, although still not totally accurate. In some places I might get x=2, despite being about 4 or 5 pixels inside column 1.

I’m not sure I can count on each square (of the image material) being exactly the same width or height right across the board, even though by eye it appears uniform. I might have to do some pixel colour interrogation if we are close-ish to a border of 2 squares and make an intelligent guess or perhaps just deny the click if it is within say about 5% of the border.

So thank you again Everynone, I’m pretty happy to get this close. :slight_smile:

Cheers,
Max

The lack of accuracy might be caused by the fact that inverse transform gives you the coordinates of the mesh counting from the very centre - this includes the Z axis. So you’re not getting the XY of the surface. If you trace from an angle, there will be an offset. You may need to account for that in the calculations. Alternatively, you could place an invisible plane on top of the board and trace against that. Or do a normal trace and simply subtract the position of the board. Many ways to do what you need.

[HR][/HR]
On the other hand, consider the following alternative:

  • every chess field is a separate actor (or component)
  • create 64, each with its own material instance


[HR][/HR]
The chess field actor:

  • cube
  • text render
  • bool isLight
  • material instance dynamic reference
  • the material is default with just one vector parameter - Colour

To interact with the field:

Make sure that your controller has mouseover events enabled.
No tracing needed, no math.

[HR][/HR]
End result:

https://i.gyazo.com/2dd57f97f2a7eac8635d4bfb87cdedc3.gif

Wow what a huge effort on your part to help. I thank you very much. Much appreciated.

Over a long time of finding small amounts of time I’ve tried various things, such as dynamically generating a board, but not doing all the things you are suggesting. I do like the look of my purchased chess model to the right in the image. The left ones are generated dynamically.

I will have a closer look at your techniques, I’m certain I can adopt some or all of it. I’m just reluctant to not use a purchased asset lol, but that’s a bad reason to not use dynamic methods I guess.

Many thanks once again.

p.s. you can see the line traces at x,y =(8,3) (H3) on my larger board, should give coords 8,3 but in the debug text you will see 7 and 3.

Cheers,
Max

741b8cb1461c13d6b573f0a9266e9c16f331b35c.jpeg

Definitely use the assets you have. Perhaps you could overlay the board with invisible collision boxes instead. Good luck. Looks neat.