Quick sanity check for my mouse selection process and a question about ray casting in general:

I'm calling XMVector3Unproject twice, once with z dimension at ViewportMinZ (0.0) and once at ViewportMaxZ (1.0).

I find the diff between near plane z and target entity z (which is fixed and known for now), turn that into a ratio over the total z distance between near and far plane, then multiply that ratio by the differences in x,y between the near and far plane to find the x,y at the target z depth.

It looks something like this:

vec3 pos = {0}; pos.z = (zDepth - rayPoints[0].z); vec3 diff = rayPoints[1] - rayPoints[0]; ASSERT(diff.z != 0.0f); float zRatio = pos.z/diff.z; ASSERT(zRatio >= 0.0f && zRatio <= 1.0f); pos.xy = rayPoints[0].xy + zRatio*diff.xy; return pos;

This works splendidly for my current wonky prototyping, but I made this up on my own instead of copying something someone else did so I'm checking whether I *should* be doing this differently?

ALSO, for ray casting in general (for when I *don't* know the depth of my target), would I do the same thing as I'm doing here but then brute force iterate over the positions of all entities in the scene testing their z depths to see if they're in the ray to aggregate a collection of entities that the ray passes through? Or is there a better approach?

Thanks, Robert