polyadvent

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

commit 996d503d130667acb41fd082bf19a4c983a5fc41
parent 9456beab576d8a918ec9efb15ece6a7be32bb158
Author: William Casarin <jb55@jb55.com>
Date:   Tue,  6 Nov 2018 22:53:46 -0800

fix camera

Diffstat:
Msrc/event.c | 24+++++++++++++++---------
Msrc/game.c | 2+-
Msrc/input.c | 4++++
Msrc/input.h | 1+
Msrc/main.c | 2+-
Msrc/node.c | 12++----------
Msrc/node.h | 1+
Msrc/orbit.c | 9+++++++--
Msrc/orbit.h | 7++++---
Msrc/update.c | 78++++++++++++++++++++++++++++++++++++++++--------------------------------------
10 files changed, 76 insertions(+), 64 deletions(-)

diff --git a/src/event.c b/src/event.c @@ -9,31 +9,37 @@ void process_events(struct game *game, float *camera) { int mdx = 0; int mdy = 0; - game->input.last_mx = game->input.mx; - game->input.last_my = game->input.my; + struct input *input = &game->input; + + input->last_mx = game->input.mx; + input->last_my = game->input.my; input_reset(&game->input); while (SDL_PollEvent(&event)) { switch (event.type) { + case SDL_MOUSEWHEEL: + input->wheel_x = event.wheel.x; + input->wheel_y = event.wheel.y; + break; case SDL_KEYDOWN: case SDL_KEYUP: - handle_key(&game->input, event.key); + handle_key(input, event.key); break; case SDL_MOUSEBUTTONDOWN: if (event.button.button <= MOUSE_BUTTONS) - game->input.mbuttons[event.button.button-1] = 1; + input->mbuttons[event.button.button-1] = 1; break; case SDL_MOUSEBUTTONUP: if (event.button.button <= MOUSE_BUTTONS) - game->input.mbuttons[event.button.button-1] = 0; + input->mbuttons[event.button.button-1] = 0; break; case SDL_MOUSEMOTION: - game->input.mx = event.motion.x; - game->input.my = event.motion.y; - game->input.mdx += event.motion.xrel; - game->input.mdy += event.motion.yrel; + input->mx = event.motion.x; + input->my = event.motion.y; + input->mdx += event.motion.xrel; + input->mdy += event.motion.yrel; break; case SDL_WINDOWEVENT: switch (event.window.event) { diff --git a/src/game.c b/src/game.c @@ -33,7 +33,7 @@ struct entity *get_player(struct resources *res) { static void init_user_settings(struct user_settings *settings) { SDL_SetRelativeMouseMode(SDL_TRUE); - settings->mouse_sens = 0.2; + settings->mouse_sens = 0.1; } void game_init(struct game *game, int width, int height) { diff --git a/src/input.c b/src/input.c @@ -10,6 +10,8 @@ void input_init(struct input *input) { input->my = 0; input->mdx = 0; input->mdy = 0; + input->wheel_x = 0; + input->wheel_y = 0; input->last_mx = 0; input->last_my = 0; assert(input->keystates); @@ -23,6 +25,8 @@ void handle_key(struct input *input, SDL_KeyboardEvent key) { void input_reset(struct input *input) { input->mdx = 0; input->mdy = 0; + input->wheel_x = 0; + input->wheel_y = 0; } int input_is_dragging(struct input *input, int mouse_button) { diff --git a/src/input.h b/src/input.h @@ -20,6 +20,7 @@ struct input { SDL_Keymod modifiers; int mx, my, last_mx, last_my; int mdx, mdy; + float wheel_x, wheel_y; int mbuttons[MOUSE_BUTTONS]; }; diff --git a/src/main.c b/src/main.c @@ -71,7 +71,7 @@ int main(void) }; struct render_config default_config = { - .draw_ui = 1, + .draw_ui = 0, .is_depth_pass = 0, .camera = game.test_resources.camera.node.mat, .projection = game.test_resources.proj_persp, diff --git a/src/node.c b/src/node.c @@ -121,14 +121,6 @@ void node_attach(struct node *node, struct node *to) { } void node_forward(struct node *node, float *dir) { - vec3_forward(node->pos, node->orientation, dir, node->pos); - /* quat_inverse(node->orientation, q); */ - /* quat_multiply_vec3(q, dir, movement); */ - /* vec3_add(node->pos, movement, node->pos); */ - /* printf("dir %f %f %f\nmovement %f %f %f\nquat %f %f %f %f\n", */ - /* dir[0], dir[1], dir[2], */ - /* movement[0], movement[1], movement[2], */ - /* node->orientation[0], node->orientation[1], */ - /* node->orientation[2], node->orientation[3]); */ - node_mark_for_recalc(node); + vec3_forward(node->pos, node->orientation, dir, node->pos); + node_mark_for_recalc(node); } diff --git a/src/node.h b/src/node.h @@ -30,6 +30,7 @@ void node_mark_for_recalc(struct node *node); struct node *node_init(struct node *node); void node_translate(struct node *node, float *p); void node_forward(struct node *node, float *p); +void node_forward_dir(struct node *node, float *orientation, float *p); void node_rotate(struct node *node, float *p); void node_scale(struct node *node, float val); float *node_world(struct node *node); diff --git a/src/orbit.c b/src/orbit.c @@ -24,17 +24,22 @@ float *spherical_to_cartesian(struct spherical *s, float *v3) return v3; } +vec3 *spherical_dir(struct spherical s, vec3 *dir) { + s.radius = 1.0; + spherical_to_cartesian(&s, dir); + return dir; +} // from: in // to: out -float *spherical_pos(struct spherical *s, vec3 *from, vec3 *to) { +vec3 *spherical_pos(struct spherical *s, vec3 *from, vec3 *to) { float spherical_offset[3]; spherical_to_cartesian(s, spherical_offset); vec3_add(from, spherical_offset, to); return to; } -float *spherical_look_at(struct spherical *s, vec3 *target, mat4 *mat) { +vec3 *spherical_look_at(struct spherical *s, vec3 *target, mat4 *mat) { float eye[3]; spherical_pos(s, target, eye); diff --git a/src/orbit.h b/src/orbit.h @@ -17,8 +17,9 @@ struct orbit { struct node node; }; -float *spherical_pos(struct spherical *s, vec3 *from, vec3 *to); -float *spherical_look_at(struct spherical *s, vec3 *target, mat4 *mat); -float *spherical_to_cartesian(struct spherical *s, float *v3); +vec3 *spherical_dir(struct spherical s, vec3 *dir); +vec3 *spherical_pos(struct spherical *s, vec3 *from, vec3 *to); +vec3 *spherical_look_at(struct spherical *s, vec3 *target, mat4 *mat); +vec3 *spherical_to_cartesian(struct spherical *s, float *v3); #endif /* ORBIT_H */ diff --git a/src/update.c b/src/update.c @@ -14,54 +14,54 @@ #include <math.h> static void movement(struct game *game, struct node *node, float speed_mult) { - float amt = 3.0 * game->dt; - float turn = 1.0 * game->dt; + float amt = 3.0 * game->dt; + float turn = 1.0 * game->dt; - amt *= speed_mult; + amt *= speed_mult; - if (game->input.modifiers & KMOD_SHIFT) - amt *= 50; + if (game->input.modifiers & KMOD_SHIFT) + amt *= 50; - if (game->input.keystates[SDL_SCANCODE_A]) - node_forward(node, V3(-amt,0,0)); + if (game->input.keystates[SDL_SCANCODE_A]) + node_forward(node, V3(-amt,0,0)); - if (game->input.keystates[SDL_SCANCODE_UP]) - node_forward(node, V3(0,0,amt)); + if (game->input.keystates[SDL_SCANCODE_UP]) + node_forward(node, V3(0,0,amt)); - if (game->input.keystates[SDL_SCANCODE_DOWN]) - node_forward(node, V3(0,0,-amt)); + if (game->input.keystates[SDL_SCANCODE_DOWN]) + node_forward(node, V3(0,0,-amt)); - if (game->input.keystates[SDL_SCANCODE_D]) - node_forward(node, V3(amt,0,0)); + if (game->input.keystates[SDL_SCANCODE_D]) + node_forward(node, V3(amt,0,0)); - if (game->input.keystates[SDL_SCANCODE_W]) - node_forward(node, V3(0,amt,0)); + if (game->input.keystates[SDL_SCANCODE_W]) + node_forward(node, V3(0,amt,0)); - if (game->input.keystates[SDL_SCANCODE_S]) - node_forward(node, V3(0,-amt,0)); + if (game->input.keystates[SDL_SCANCODE_S]) + node_forward(node, V3(0,-amt,0)); - if (game->input.keystates[SDL_SCANCODE_K]) - node_translate(node, V3(0, 0,amt)); + if (game->input.keystates[SDL_SCANCODE_K]) + node_forward(node, V3(0, 0,amt)); - if (game->input.keystates[SDL_SCANCODE_J]) - node_translate(node, V3(0, 0,-amt)); + if (game->input.keystates[SDL_SCANCODE_J]) + node_forward(node, V3(0, 0,-amt)); - if (game->input.keystates[SDL_SCANCODE_E]) - node_rotate(node, V3(0, 0, turn)); + if (game->input.keystates[SDL_SCANCODE_E]) + node_rotate(node, V3(0, 0, turn)); - if (game->input.keystates[SDL_SCANCODE_Q]) - node_rotate(node, V3(0, 0, -turn)); + if (game->input.keystates[SDL_SCANCODE_Q]) + node_rotate(node, V3(0, 0, -turn)); - /* if (game->input.keystates[SDL_SCANCODE_DOWN]) */ - /* node_translate(node, V3(0, 0, -amt)); */ + /* if (game->input.keystates[SDL_SCANCODE_DOWN]) */ + /* node_translate(node, V3(0, 0, -amt)); */ - if (game->input.keystates[SDL_SCANCODE_P]) { - printf("%f %f %f\n", - node->pos[0], - node->pos[1], - node->pos[2]); - mat4_print(node->mat); - } + if (game->input.keystates[SDL_SCANCODE_P]) { + printf("%f %f %f\n", + node->pos[0], + node->pos[1], + node->pos[2]); + mat4_print(node->mat); + } } static void remap_samples(struct point *points, int n_samples, @@ -318,6 +318,10 @@ void orbit_update_from_mouse(struct orbit *camera, struct input *input, camera->coords.azimuth += mx; camera->coords.inclination += my; + /* printf("coords azimuth %f inclination %f radius %f\n", */ + /* camera->coords.azimuth, */ + /* camera->coords.inclination, */ + /* camera->coords.radius); */ spherical_look_at(&camera->coords, target, camera->node.mat); } @@ -341,7 +345,6 @@ void update (struct game *game) { static int first = 1; struct resources *res = &game->test_resources; struct terrain *terrain = &game->terrain; - struct node *tnode = &get_entity(&terrain->entity_id)->node; struct node *root = &game->test_resources.root; struct entity *player = get_player(res); float *time = &res->time; @@ -355,13 +358,12 @@ void update (struct game *game) { } player_update(game, player); + /* spherical_dir(game->test_resources.camera.coords, camera_dir); */ + /* vec3_scale(camera_dir, -1, camera_dir); */ if (game->input.modifiers & KMOD_LALT) { movement(game, &res->camera.node, 1.0); } - else if (game->input.modifiers & KMOD_RCTRL) { - movement(game, tnode, 5.0); - } else { player_movement(game, player); }