#How to cast a camera ray in orthographic projection ?

12 messages · Page 1 of 1 (latest)

novel shard
#

I was using perspective projection before, and my code for casting a camera ray was:

let mouse_position = /* get from events */;
let (camera, camera_transform) = /* get from query */;
let Vec2 { x: width, y: height } = camera.logical_viewport_size()?;

let x = (mouse_position.x / width) * 2.0 - 1.0;
let y = (mouse_position.y / height) * 2.0 - 1.0;

let camera_inverse_matrix =
    (camera.projection_matrix() * camera_transform.compute_matrix().inverse()).inverse();
let near = camera_inverse_matrix.project_point3(Vec3::new(x, y, -1.0));
let far = camera_inverse_matrix.project_point3(Vec3::new(x, y, 1.0));

if near.is_nan() || far.is_nan() {
    warn!("Unprojection failed");
    return None;
}

let dir = far - near;
return dir.try_normalize()?;

From what I understand, I just had to change project_point3 to manually multiplying camera_inverse_matrix with Vec4(x, y, z, 1.0) and it would work. However, it did not. The resulting dir vector is always the same whatever my inputs are. I've seen a comment in OrthographicProjection that near and far planes are swapped, is my problem has to do something with this and "infinite reverse perspective projections" ? 😵‍💫

primal bane
#

For an ortho camera, wouldn't all the direction vectors always be the same since it's ortho?

simple locust
#
    commands.spawn_bundle(Camera3dBundle {
        transform: Transform::from_xyz(0.0, 10.0, 0.0)
            .with_rotation(Quat::from_rotation_x(-PI / 2.0)),
        projection: Projection::Orthographic(OrthographicProjection {
            scale: 0.01,
            ..default()
        }),
        ..Camera3dBundle::default()
    });
#

this gives you top down ortho camera

#

for making a normal cam into ortocam, just look at the bevy source code

#

basically you need to make the perspective divide be null

novel shard
#

Now that I have slept, I see that what I was doing is pointless. However, is there another way to achieve the same as with perspective point unprojection ?

#

Using another method perhaps. I've never worked with ortho before

simple locust
#

orthographic just means that the perspective divide never happens

#

you might want to read up on how cameras work first

#

then you can loop up in bevy on how cameras are constructed

novel shard
#

after some research, making near point as Vec3::new(0.0, 0.0, -1.0) and far point as Vec3::new(x, y, 1.0) works for my particular use case (to rotate an entity which is in the center of the screen towards the mouse). I'm still not sure if it's actually correct, but it seems like it, and thats enough for me honestly