Skip to content

Commit 9a5dddc

Browse files
authored
Added viewport independent raycast (#3709)
* added viewport independent raycast * Renamed GetMouseRayEx to GetViewRay
1 parent f033b30 commit 9a5dddc

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/raylib.h

+1
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,7 @@ RLAPI void UnloadShader(Shader shader); // Un
10501050

10511051
// Screen-space-related functions
10521052
RLAPI Ray GetMouseRay(Vector2 mousePosition, Camera camera); // Get a ray trace from mouse position
1053+
RLAPI Ray GetViewRay(Vector2 mousePosition, Camera camera, float width, float height); // Get a ray trace from mouse position in a viewport
10531054
RLAPI Matrix GetCameraMatrix(Camera camera); // Get camera transform matrix (view matrix)
10541055
RLAPI Matrix GetCameraMatrix2D(Camera2D camera); // Get camera 2d transform matrix
10551056
RLAPI Vector2 GetWorldToScreen(Vector3 position, Camera camera); // Get the screen space position for a 3d world space position

src/rcore.c

+11-5
Original file line numberDiff line numberDiff line change
@@ -1404,14 +1404,20 @@ void SetShaderValueTexture(Shader shader, int locIndex, Texture2D texture)
14041404
//----------------------------------------------------------------------------------
14051405

14061406
// Get a ray trace from mouse position
1407-
Ray GetMouseRay(Vector2 mouse, Camera camera)
1407+
Ray GetMouseRay(Vector2 mousePosition, Camera camera)
1408+
{
1409+
return GetViewRay(mousePosition, camera, GetScreenWidth(), GetScreenHeight());
1410+
}
1411+
1412+
// Get a ray trace from the mouse position within a specific section of the screen
1413+
Ray GetViewRay(Vector2 mousePosition, Camera camera, float width, float height)
14081414
{
14091415
Ray ray = { 0 };
14101416

14111417
// Calculate normalized device coordinates
14121418
// NOTE: y value is negative
1413-
float x = (2.0f*mouse.x)/(float)GetScreenWidth() - 1.0f;
1414-
float y = 1.0f - (2.0f*mouse.y)/(float)GetScreenHeight();
1419+
float x = (2.0f*mousePosition.x)/width - 1.0f;
1420+
float y = 1.0f - (2.0f*mousePosition.y)/height;
14151421
float z = 1.0f;
14161422

14171423
// Store values in a vector
@@ -1425,11 +1431,11 @@ Ray GetMouseRay(Vector2 mouse, Camera camera)
14251431
if (camera.projection == CAMERA_PERSPECTIVE)
14261432
{
14271433
// Calculate projection matrix from perspective
1428-
matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)GetScreenWidth()/(double)GetScreenHeight()), RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
1434+
matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)width/(double)height), RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
14291435
}
14301436
else if (camera.projection == CAMERA_ORTHOGRAPHIC)
14311437
{
1432-
double aspect = (double)CORE.Window.screen.width/(double)CORE.Window.screen.height;
1438+
double aspect = (double)width/(double)height;
14331439
double top = camera.fovy/2.0;
14341440
double right = top*aspect;
14351441

0 commit comments

Comments
 (0)