polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

orbit_util.c (1569B)


      1 
      2 #include "orbit.h"
      3 #include "input.h"
      4 #include "debug.h"
      5 #include "entity.h"
      6 
      7 // entity geom offset
      8 //V3(0.0, 0.0, player_geom->max[2])
      9 
     10 void orbit_update_from_mouse(struct orbit *camera, struct input *input,
     11                              float mouse_sens, struct node_id *target_id,
     12 			     vec3 *offset, float dt)
     13 {
     14     float target[3];
     15 
     16     struct node *cam_node = get_node(&camera->node_id);
     17     assert(cam_node);
     18 
     19     struct node *target_node = get_node(target_id);
     20     assert(target_node);
     21 
     22     node_recalc(target_node);
     23     vec3_copy(node_world(target_node), target);
     24     if (offset)
     25 	    vec3_add(target, offset, target);
     26 
     27     float mx = 0.0, my = 0.0;
     28     if (input_is_dragging(input, SDL_BUTTON_LEFT) ||
     29         input_is_dragging(input, SDL_BUTTON_RIGHT)) {
     30         mx = -input->mdx * mouse_sens * dt;
     31         my = -input->mdy * mouse_sens * dt;
     32     }
     33 
     34     // zoom
     35     if (input->keystates[SDL_SCANCODE_V]) {
     36         if (input->modifiers & KMOD_SHIFT)
     37             camera->coords.radius += dt * 100.0;
     38         else
     39             camera->coords.radius -= dt * 100.0;
     40 
     41     }
     42     else if (input->wheel_y) {
     43         camera->coords.radius += input->wheel_y * dt * 100.0;
     44     }
     45 
     46     camera->coords.radius = max(1.0, camera->coords.radius);
     47 
     48     camera->coords.azimuth     += mx;
     49     camera->coords.inclination += my;
     50 
     51     /*
     52     printf("coords azimuth %f inclination %f radius %f\n",
     53            camera->coords.azimuth,
     54            camera->coords.inclination,
     55            camera->coords.radius);
     56 	   */
     57 
     58     spherical_look_at(&camera->coords, target, cam_node->mat);
     59 
     60 }
     61