@ -7,6 +7,9 @@
# define RAYGUI_IMPLEMENTATION
# define RAYGUI_IMPLEMENTATION
# include "raygui.h"
# include "raygui.h"
// Forward declarations
static Vector3 sim_to_render ( Vec3 pos , double scale ) ;
// Initialize raylib window
// Initialize raylib window
void init_renderer ( int width , int height , const char * title ) {
void init_renderer ( int width , int height , const char * title ) {
InitWindow ( width , height , title ) ;
InitWindow ( width , height , title ) ;
@ -33,7 +36,15 @@ void setup_camera(RenderState* render_state) {
}
}
// Update camera with keyboard/mouse controls
// Update camera with keyboard/mouse controls
void update_camera ( RenderState * render_state ) {
void update_camera ( RenderState * render_state , SimulationState * sim ) {
// If following a body, update target to body position
if ( render_state - > camera_follow_body & & render_state - > selected_body_index > = 0 & &
render_state - > selected_body_index < sim - > body_count ) {
CelestialBody * body = & sim - > bodies [ render_state - > selected_body_index ] ;
Vector3 body_pos = sim_to_render ( body - > position , render_state - > distance_scale ) ;
render_state - > camera . target = body_pos ;
}
// Orbital camera rotation with arrow keys
// Orbital camera rotation with arrow keys
float camera_distance = Vector3Distance ( render_state - > camera . position , render_state - > camera . target ) ;
float camera_distance = Vector3Distance ( render_state - > camera . position , render_state - > camera . target ) ;
float angle_speed = 0.02f ;
float angle_speed = 0.02f ;
@ -77,6 +88,16 @@ void update_camera(RenderState* render_state) {
if ( IsKeyPressed ( KEY_B ) ) {
if ( IsKeyPressed ( KEY_B ) ) {
render_state - > show_body_list = ! render_state - > show_body_list ;
render_state - > show_body_list = ! render_state - > show_body_list ;
}
}
// Toggle camera follow with F key
if ( IsKeyPressed ( KEY_F ) ) {
if ( render_state - > selected_body_index > = 0 ) {
render_state - > camera_follow_body = ! render_state - > camera_follow_body ;
printf ( " Camera follow: %s \n " , render_state - > camera_follow_body ? " enabled " : " disabled " ) ;
} else {
render_state - > camera_follow_body = false ;
}
}
}
}
// Transform from simulation coordinates (XY plane) to render coordinates (XZ plane)
// Transform from simulation coordinates (XY plane) to render coordinates (XZ plane)
@ -89,11 +110,7 @@ Vector3 sim_to_render(Vec3 pos, double scale) {
} ;
} ;
}
}
void focus_camera ( RenderState * render_state , CelestialBody * body , Vector3 & offset ) {
Vector3 p = sim_to_render ( body - > position , render_state - > distance_scale ) ;
render_state - > camera . position = ( Vector3 ) { p . x , p . y + offset . y , p . z + offset . z } ;
render_state - > camera . target = p ;
}
// Scale a radius for rendering (with minimum visible size)
// Scale a radius for rendering (with minimum visible size)
float scale_radius ( double radius , double scale ) {
float scale_radius ( double radius , double scale ) {