Best Ray Casting for y = 0

Hello everyone,

I have a fairly simple problem:

I have a 2 dimensional map, with varying models loaded on top. However, the models aren’t everywhere. If a tile is missing, nothing is drawn.
When the user clicks, I want to know the hypothetical position of the object, and draw a model there. Thus, I need to know from a screen click (or tap), what position on y=0 the user meant.
I thought of two different solutions, but could not quite solve the problem:

  1. Use a Ray and an invisible Spatial. Maybe I could cull the spatial backwards, but that seems excessive and not optimal.
  2. Use some vector magic using cam.getDirection() and cam.getLocation. I tried this, but it failed (not sure why). I used: origin + (origin.y / -direction.y) * direction.

What is the best/most efficient way of doing this?

PS: I don’t think you need any code, but feel free to ask if you need it.

Off the top of my head… something like:

Vector3f camLoc = ...
Vector3f camDir = ...

float distance = FastMath.abs(camLoc.y / camDir.y);
Vector3f groundIntersect = camLoc.add(camDir.mult(distance));

Edit: fixed distance calc… though note it now means you will get strange values if you look up.

It works, thanks!

That’s no problem, I’ll just add in an if for when dir.y isn’t within -0.5 and +0.5.

My example will give you a proper value as long as dir.y < 0. Else it will give you a point in the sky over the ground point that is a mirror of that point from your current location.

Ah yes, I was a bit confused.

Looking at the horizon will of course give you a value of y=0.
Thanks again.