ohsp

Prototype for a game with dual thruster controls.
git clone git://git.amin.space/ohsp.git
Log | Files | Refs | LICENSE

commit c191ef7c25f198bd7fa34d16a176087216995d43
parent f0068617e7ee03ceaabde90e32e32aedd255101d
Author: amin <dev@aminmesbah.com>
Date:   Fri, 15 Dec 2017 05:25:54 +0000

Change vector representation

- Use (x,y) rather than (angle, length)
- Use a fancy union for convenience
- Add some basic vector functions

FossilOrigin-Name: 366bf4f147aab9c876b8f03fc0b87a1da07abb64a4eb0f17c7fc68939daa2d95
Diffstat:
Msrc/entity.c | 59+++++++++++++++++++++++++++++++++++++++++++++++++----------
Msrc/entity.h | 44++++++++++++++++++++++++++++++++------------
Msrc/game.c | 89++++++++++++++++++++++++++++++++++++++++---------------------------------------
Msrc/platform_sdl.c | 2+-
4 files changed, 127 insertions(+), 67 deletions(-)

diff --git a/src/entity.c b/src/entity.c @@ -4,24 +4,63 @@ #include <stdio.h> -struct Vec2d vec2d_add(float angle1, float length1, float angle2, float length2) +void entity_accelerate(struct Entity *e, struct Vec2d *v) { - float x = sinf(angle1) * length1 + sinf(angle2) * length2; - float y = cosf(angle1) * length1 + cosf(angle2) * length2; + struct Vec2d new_vec = vec2d_add(e->velocity.x, e->velocity.y, v->x, v->y); + //printf("(%f, %f)\n", new_vec.x, new_vec.y); + e->velocity.x = new_vec.x; + e->velocity.y = new_vec.y; +} + +struct Vec2d vec2d_add(float x0, float y0, float x1, float y1) +{ struct Vec2d new_vec = { - .angle = 0.5f * M_PI - atan2f(y, x), - .length = hypotf(x, y), + .x = x0 + x1, + .y = y0 + y1 }; return new_vec; } -void entity_accelerate(struct Entity *e, float angle, float acceleration) +bool vec2d_equal(struct Vec2d v0, struct Vec2d v1) +{ + return (v0.x == v1.x) && (v0.y == v1.y); +} + + +float vec2d_get_angle(float x, float y) { - struct Vec2d new_vec = vec2d_add(e->angle, e->speed, angle, acceleration); - //printf("(%f, %f)\n", new_vec.angle, new_vec.length); - e->angle = new_vec.angle; - e->speed = new_vec.length; + return 0.5f * M_PI - atan2f(y, x); +} + + +float vec2d_get_length(float x, float y) +{ + return hypotf(x, y); +} + + +struct Vec2d vec2d_negate(struct Vec2d v) +{ + return vec2d_scale(v.x, v.y, -1.0f); +} + + +struct Vec2d vec2d_normalize(float x, float y) +{ + float length = vec2d_get_length(x, y); + return vec2d_scale(x, y, 1.0f / length); +} + + +struct Vec2d vec2d_scale(float x, float y, float s) +{ + struct Vec2d new_vec = + { + .x = x * s, + .y = y * s + }; + return new_vec; } diff --git a/src/entity.h b/src/entity.h @@ -1,30 +1,50 @@ #ifndef ENTITY_H #define ENTITY_H +#include <stdbool.h> #include <stdint.h> #ifndef M_PI #define M_PI 3.141592f #endif +struct Vec2d +{ + union + { + struct + { + float x; + float y; + }; + struct + { + float w; + float h; + }; + float xy[2]; + float d[2]; + }; +}; + struct Entity { - float angle; - float speed; float mass; float size; - float x; - float y; + struct Vec2d position; + struct Vec2d velocity; uint32_t color; }; -struct Vec2d -{ - float angle; - float length; -}; - -struct Vec2d vec2d_add(float angle1, float length1, float angle2, float length2); -void entity_accelerate(struct Entity *e, float angle, float acceleration); +void entity_accelerate(struct Entity *e, struct Vec2d *v); +struct Vec2d vec2d_add(float x0, float y0, float x1, float y1); +struct Vec2d vec2d_al_to_xy(struct Vec2d v); +float vec2d_get_angle(float x, float y); +bool vec2d_equal(struct Vec2d v0, struct Vec2d v1); +float vec2d_get_length(float x, float y); +struct Vec2d vec2d_negate(struct Vec2d v); +struct Vec2d vec2d_normalize(float x, float y); +struct Vec2d vec2d_scale(float x, float y, float s); +struct Vec2d vec2d_xy_to_al(struct Vec2d v); #endif diff --git a/src/game.c b/src/game.c @@ -17,19 +17,21 @@ void game_init(struct GameState *game_state, int field_width, int field_height) return; } - game_state->player.angle = 0; - game_state->player.speed = 0; game_state->player.mass = 10; game_state->player.size = 30; - game_state->player.x = field_width / 2; - game_state->player.y = field_height / 2; game_state->player.color = 0x888888; - game_state->thrust_vector01.angle = 0; - game_state->thrust_vector01.length = 0; - game_state->thrust_vector02.angle = 0; - game_state->thrust_vector02.length = 0; - game_state->thrust_vector_sum.angle = 0; - game_state->thrust_vector_sum.length = 0; + game_state->player.position.x = field_width / 2; + game_state->player.position.y = field_height / 2; + game_state->player.velocity.x = 0; + game_state->player.velocity.y = 0; + + game_state->thrust_vector01.x = 0; + game_state->thrust_vector01.y = 0; + game_state->thrust_vector02.x = 0; + game_state->thrust_vector02.y = 0; + game_state->thrust_vector_sum.x = 0; + game_state->thrust_vector_sum.y = 0; + game_state->view.dx = 0; game_state->view.dy = 0; game_state->view.zoom = 1; @@ -61,35 +63,33 @@ void game_update(struct GameState *game_state, struct GameControllerInput game_i return; } - game_state->thrust_vector01.angle = atan2f(game_input.left_stick_y, game_input.left_stick_x); - game_state->thrust_vector01.length = hypotf(game_input.left_stick_x, game_input.left_stick_y); - //printf("(lx: %f, ly: %f, thrust_a: %f, thrust_l: %f)\n", - // game_input.left_stick_x, - // game_input.left_stick_y, - // game_state->thrust_vector01.angle, - // game_state->thrust_vector01.length); - - game_state->thrust_vector02.angle = atan2f(game_input.right_stick_y, game_input.right_stick_x); - game_state->thrust_vector02.length = hypotf(game_input.right_stick_x, game_input.right_stick_y); - //printf("(rx: %f, ry: %f, thrust_a: %f, thrust_l: %f)\n", - // game_input.right_stick_x, - // game_input.right_stick_y, - // game_state->thrust_vector02.angle, - // game_state->thrust_vector02.length); + game_state->thrust_vector01.x = game_input.left_stick_x; + game_state->thrust_vector01.y = game_input.left_stick_y; + //printf("(lx: %f, ly: %f, thrust_x: %f, thrust_y: %f)\n", game_input.left_stick_x, game_input.left_stick_y, game_state->thrust_vector01.x, game_state->thrust_vector01.y); + + game_state->thrust_vector02.x = game_input.right_stick_x; + game_state->thrust_vector02.y = game_input.right_stick_y; + //printf("(rx: %f, ry: %f, thrust_x: %f, thrust_y: %f)\n", game_input.right_stick_x, game_input.right_stick_y, game_state->thrust_vector02.x, game_state->thrust_vector02.y); game_state->thrust_vector_sum = vec2d_add( - game_state->thrust_vector01.angle, - game_state->thrust_vector01.length, - game_state->thrust_vector02.angle, - game_state->thrust_vector02.length); + game_state->thrust_vector01.x, + game_state->thrust_vector01.y, + game_state->thrust_vector02.x, + game_state->thrust_vector02.y); + + game_state->thrust_vector_sum = vec2d_scale( + game_state->thrust_vector_sum.x, + game_state->thrust_vector_sum.y, + 0.01f); + //printf("(total_thrust_x: %f, total_thrust_y: %f)\n", game_state->thrust_vector_sum.x, game_state->thrust_vector_sum.y); struct Entity *player = &game_state->player; - entity_accelerate(player, game_state->thrust_vector_sum.angle, game_state->thrust_vector_sum.length * 0.01f); + entity_accelerate(player, &game_state->thrust_vector_sum); - player->x += player->speed * cosf(player->angle); - player->y += player->speed * sinf(player->angle); - player->x = wrap(player->x, 0, field_width); - player->y = wrap(player->y, 0, field_height); + player->position.x += player->velocity.x; + player->position.y += player->velocity.y; + player->position.x = wrap(player->position.x, 0, field_width); + player->position.y = wrap(player->position.y, 0, field_height); } @@ -101,14 +101,15 @@ void game_render(struct OffscreenBuffer *buffer, float dt, struct GameState *gam return; } struct Entity player = game_state->player; - game_render_circle(buffer, player.x, player.y, player.size, player.color); - game_render_vector(buffer, &game_state->thrust_vector01, player.x, player.y, 5, 0x0000FF); - game_render_vector(buffer, &game_state->thrust_vector02, player.x, player.y, 5, 0xFF0000); - game_render_vector(buffer, &game_state->thrust_vector_sum, player.x, player.y, 5, 0xFF00FF); + game_render_circle(buffer, player.position.x, player.position.y, player.size, player.color); + game_render_vector(buffer, &game_state->thrust_vector01, player.position.x, player.position.y, 5, 0x0000FF); + game_render_vector(buffer, &game_state->thrust_vector02, player.position.x, player.position.y, 5, 0xFF0000); + game_render_vector(buffer, &game_state->thrust_vector_sum, player.position.x, player.position.y, 5, 0xFF00FF); + struct Vec2d player_vec = vec2d_scale(player.velocity.x, player.velocity.y, 100); game_render_circle(buffer, - player.x + 100 * (player.speed * cosf(player.angle)), - player.y + 100 * (player.speed * sinf(player.angle)), + player.position.x + player_vec.x, + player.position.y + player_vec.y, 5, 0x00FF00); } @@ -319,10 +320,10 @@ void game_render_line(struct OffscreenBuffer *buffer, float x0, float y0, float void game_render_vector(struct OffscreenBuffer *buffer, struct Vec2d *v, float x0, float y0, float radius, uint32_t color) { - float x1 = x0 + 100 * (v->length * cosf(v->angle)); - float y1 = y0 + 100 * (v->length * sinf(v->angle)); - game_render_line(buffer, x0, y0, x1, y1, color); - game_render_circle(buffer, x1, y1, radius, color); + struct Vec2d vec = vec2d_scale(v->x, v->y, 100); + vec = vec2d_add(x0, y0, vec.x, vec.y); + game_render_line(buffer, x0, y0, vec.x, vec.y, color); + game_render_circle(buffer, vec.x, vec.y, radius, color); } diff --git a/src/platform_sdl.c b/src/platform_sdl.c @@ -77,7 +77,7 @@ void clear_screen(struct SDLOffscreenBuffer *buffer, uint32_t pixel_value) // TODO: make the deadzone circular float sdl_process_controller_axis_value(int16_t value, int16_t deadzone_threshold) { - float result = 0.0; + float result = 0.0f; if (value < -deadzone_threshold) { result = (float)(value + deadzone_threshold) / (32768.0f - deadzone_threshold);