Skip to content

Commit 7810bb8

Browse files
committed
raycast fixes
1 parent 5da4126 commit 7810bb8

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

src/main.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ int main(void) {
256256

257257
Vector2 mouseDelta = GetMouseDelta();
258258
hovered_sat = NULL;
259-
float hit_radius = 0.4f * cfg.ui_scale;
260259

261260
// moving the camera around smoothly via targets
262261
if (is_2d_view) {
@@ -275,8 +274,9 @@ int main(void) {
275274
active_lock = LOCK_NONE;
276275
}
277276

278-
Vector2 mouseWorld = GetScreenToWorld2D(GetMousePosition(), camera2d);
279-
float closest_dist = 9999.0f, hit_radius_2d = 10.0f * cfg.ui_scale / camera2d.zoom;
277+
Vector2 mousePos = GetMousePosition();
278+
float closest_dist = 9999.0f;
279+
float hit_radius_pixels = 12.0f * cfg.ui_scale; // hitbox size in screen pixels
280280

281281
for (int i = 0; i < sat_count; i++) {
282282
satellites[i].current_pos = calculate_position(&satellites[i], current_epoch);
@@ -285,8 +285,11 @@ int main(void) {
285285

286286
float mx, my;
287287
get_map_coordinates(satellites[i].current_pos, gmst_deg, cfg.earth_rotation_offset, map_w, map_h, &mx, &my);
288-
float dist = Vector2Distance(mouseWorld, (Vector2){mx, my});
289-
if (dist < hit_radius_2d && dist < closest_dist) { closest_dist = dist; hovered_sat = &satellites[i]; }
288+
289+
Vector2 screenPos = GetWorldToScreen2D((Vector2){mx, my}, camera2d);
290+
float dist = Vector2Distance(mousePos, screenPos);
291+
292+
if (dist < hit_radius_pixels && dist < closest_dist) { closest_dist = dist; hovered_sat = &satellites[i]; }
290293
}
291294
} else {
292295
if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) {
@@ -317,7 +320,12 @@ int main(void) {
317320
if (hide_unselected && selected_sat != NULL && &satellites[i] != selected_sat) continue;
318321

319322
Vector3 draw_pos = Vector3Scale(satellites[i].current_pos, 1.0f / DRAW_SCALE);
320-
RayCollision col = GetRayCollisionSphere(mouseRay, draw_pos, hit_radius);
323+
324+
// adjust sphere radius based on distance to maintain consistent screen-size hitbox
325+
float distToCam = Vector3Distance(camera3d.position, draw_pos);
326+
float hit_radius_3d = 0.015f * distToCam * cfg.ui_scale;
327+
328+
RayCollision col = GetRayCollisionSphere(mouseRay, draw_pos, hit_radius_3d);
321329
if (col.hit && col.distance < closest_dist) { closest_dist = col.distance; hovered_sat = &satellites[i]; }
322330
}
323331
}

0 commit comments

Comments
 (0)