Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide the projection and view matrices separately in the 3D camera, or add a screen-to-world method #725

Open
Alexandre425 opened this issue May 22, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@Alexandre425
Copy link

Mouse picking, the process of picking an entity or point in 3D space with your mouse, requires that the projection and view matrices be separate.

Currently, these two are available only together, through the matrix() method:

macroquad/src/camera.rs

Lines 209 to 225 in bd00be0

fn matrix(&self) -> Mat4 {
let aspect = self.aspect.unwrap_or(screen_width() / screen_height());
match self.projection {
Projection::Perspective => {
Mat4::perspective_rh_gl(self.fovy, aspect, Self::Z_NEAR, Self::Z_FAR)
* Mat4::look_at_rh(self.position, self.target, self.up)
}
Projection::Orthographics => {
let top = self.fovy / 2.0;
let right = top * aspect;
Mat4::orthographic_rh_gl(-right, right, -top, top, Self::Z_NEAR, Self::Z_FAR)
* Mat4::look_at_rh(self.position, self.target, self.up)
}
}
}

If they were provided separately, it would be much easier to get the ray corresponding to a specific point on the screen. Alternatively, the entire operation (pictured below) could be provided as a method, returning the ray in world space:

// Ray in eye/view space
let ray_eye = projection_matrix.inverse() * vec4(screen_pos.x, screen_pos.y, -1., 1.);
// Ray in world space
let ray_world = view_matrix.inverse() * vec4(ray_eye.x, ray_eye.y, -1., 0.).normalize();
@not-fl3 not-fl3 added the enhancement New feature or request label May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants