Is the camera’s cframe at the center of the head?
if it is, Is the unit ray origin at the center of head?
Does the unitray point to the 2d x and y location
would like a explanation, a visual would be great
Is the camera’s cframe at the center of the head?
if it is, Is the unit ray origin at the center of head?
Does the unitray point to the 2d x and y location
would like a explanation, a visual would be great
No, almost never. It’s usually around 0.25 to 0.75 studs away from the head and changes when you move the camera, and also because of animations and stuff. You can test this like so:
local head = script.Parent:WaitForChild("Head")
local camera = game.Workspace.CurrentCamera
while wait() do
print("Difference: ", camera.CFrame:PointToObjectSpace(head.Position))
print("Distance: ", camera.CFrame:PointToObjectSpace(head.Position).Magnitude)
end
No, for the above reason. But more importantly, even if the camera was in the middle of the head, the ray origin wouldn’t be. Because the ray origin isn’t at the center of the camera. It changes depending on which screen-space or viewport-space coordinates you use, but even when casting in the center of the screen you get differences like these:
0, 2.38418579e-07, -0.100000262
So almost 0 on X and Y, but ~0.1 studs in in front of the camera. Tested like this:
while wait() do
local r = camera:ViewportPointToRay(camera.ViewportSize.X/2, camera.ViewportSize.Y/2)
print(camera.CFrame:PointToObjectSpace(r.Origin))
end
No, that’s impossible and even nonsensical. Rays are 3D objects, screen coordinates are 2D. What it actually does is described on the wiki:
The
Ray
originates from theVector3
equivalent of the 2D position in the world at the given depth (in studs) away from theCamera
.
- Camera:ViewportPointToRay
On the 3rd paramter:
depth float 0 The depth from the
Camera
, in studs, from which to offset the origin of theRay
Weirdly enough the docs are wrong with regards to which depth is used. It’s the 3rd param to the function call, 0 by default. But I got 0.1 studs in front of the camera with the default value, and I tested and got 1.1 studs when passing in 1. So that’s weird.
Here’s an illustration
- Illustration of what goes on, seen from the side of the camera (although it’d look identical from above except for the angle). The black segments represent a segment in 3D space and its projected onto the render plane.
Also check out this perspective one from wikipedia:
- File:Perspective Projection Principle.jpg - Wikimedia Commons
Right, so the screen is 2D which means it doesn’t show anything in 3D. Instead it shows a projection of 3D things onto a 2D surface. This 2D surface could be anything, but AFAIK in all 3D games it’s a 3D plane hanging a short distance in front of the camera. There’s also something called the near and far clipping planes which is something else. The reason that the render plane is not right at the same position as the camera is that it wouldn’t be a plane anymore. Try drawing a vertical line through the camera position and see where it intersects the two legs of the angle… it degenerates into a single point.
So this is why converting a point in 2D screen space to 3D world space isn’t the same as just the camera position.
When you call ViewportPointToRay with 0 as the 3rd argument you get a ray that’s constructed something like this:
… at least I think that the plane which the ray starts on is the render plane. If not, it’s just a different plane. Regardless, it explains why the ray origin isn’t the camera position either.
Hope this helps. I’m pretty sure it’s right at least, or at least the basic ideas are. I may be wrong on some of the details tho. Let me know if anything is unclear!
is it safe to say that if none of the above made any sense at all that I should quit trying to learn this?
edit: I was able to figure out some things and I posted how I understand this all working over at this thread: