Added sliding along walls and other obstacles when facing them head on

dev
Shariq Shah 6 years ago
parent b427932c34
commit 1514f3990f
  1. 2
      src/common/version.h
  2. 105
      src/game/player.c
  3. 2
      src/game/scene.c
  4. 2
      src/system/config_vars.c

@ -4,7 +4,7 @@
/* Auto generated version file. DO NOT MODIFY */ /* Auto generated version file. DO NOT MODIFY */
#define SYMMETRY_VERSION_MAJOR 0 #define SYMMETRY_VERSION_MAJOR 0
#define SYMMETRY_VERSION_MINOR 1 #define SYMMETRY_VERSION_MINOR 1
#define SYMMETRY_VERSION_REVISION 309 #define SYMMETRY_VERSION_REVISION 310
#define SYMMETRY_VERSION_BRANCH "dev" #define SYMMETRY_VERSION_BRANCH "dev"
#endif #endif

@ -118,63 +118,41 @@ void player_update(struct Player* player, struct Scene* scene, float dt)
transform_rotate(player->camera_node, &rot_axis_pitch, pitch, TS_LOCAL); transform_rotate(player->camera_node, &rot_axis_pitch, pitch, TS_LOCAL);
/* Movement */ /* Movement */
float gravity = 0.1f; float gravity = -0.25f;
float dampening_x = 0.08f; float jump_velocity = 50.f;
float dampening_z = 0.08f;
float jump_velocity = 20.f;
float move_speed = player->move_speed; float move_speed = player->move_speed;
static vec3 velocity = { 0.f, 0.f, 0.f }; vec3 move_direction = { 0.f };
vec3 max_velocity = { player->move_speed * player->move_speed_multiplier, jump_velocity, player->move_speed * player->move_speed_multiplier }; vec3 max_velocity = { player->move_speed * player->move_speed_multiplier, jump_velocity, player->move_speed * player->move_speed_multiplier };
static bool jumping = false; static bool jumping = false;
static float move_speed_vertical = 0.f;
// If we started jumping last frame, set jumpig to false // If we started jumping last frame, set jumpig to false
if(jumping) jumping = false; if(jumping) jumping = false;
if(input_map_state_get("Sprint", KS_PRESSED)) move_speed *= player->move_speed_multiplier; if(input_map_state_get("Sprint", KS_PRESSED)) move_speed *= player->move_speed_multiplier;
if(input_map_state_get("Move_Forward", KS_PRESSED)) velocity.z -= move_speed; if(input_map_state_get("Move_Forward", KS_PRESSED)) move_direction.z -= 1.f;
if(input_map_state_get("Move_Backward", KS_PRESSED)) velocity.z += move_speed; if(input_map_state_get("Move_Backward", KS_PRESSED)) move_direction.z += 1.f;
if(input_map_state_get("Move_Left", KS_PRESSED)) velocity.x -= move_speed; if(input_map_state_get("Move_Left", KS_PRESSED)) move_direction.x -= 1.f;
if(input_map_state_get("Move_Right", KS_PRESSED)) velocity.x += move_speed; if(input_map_state_get("Move_Right", KS_PRESSED)) move_direction.x += 1.f;
if(input_map_state_get("Jump", KS_PRESSED)) if(input_map_state_get("Jump", KS_PRESSED))
{ {
if(player->grounded) if(player->grounded)
{ {
velocity.y += jump_velocity; //velocity.y += jump_velocity;
move_speed_vertical += jump_velocity;
jumping = true; jumping = true;
player->grounded = false; player->grounded = false;
} }
} }
// Dampen Velocity vec3_norm(&move_direction, &move_direction);
if(velocity.x > 0.f) if(move_direction.x != 0 || move_direction.z != 0)
velocity.x -= dampening_x; {
else if(velocity.x < 0.f) //quat_mul_vec3(&move_direction, &player->camera_node->base.transform.rotation, &move_direction);
velocity.x += dampening_x; quat_mul_vec3(&move_direction, &player->base.transform.rotation, &move_direction);
//offset.y = 0.f;
//if(velocity.y >= 0.f) }
velocity.y -= gravity;
if(velocity.z > 0.f)
velocity.z -= dampening_z;
else if(velocity.z < 0.f)
velocity.z += dampening_z;
// Clamp velocity to min/max
if(velocity.x > max_velocity.x)
velocity.x = max_velocity.x;
else if(velocity.x < -max_velocity.x)
velocity.x = -max_velocity.x;
if(velocity.y > max_velocity.y)
velocity.y = max_velocity.y;
if(velocity.y < -max_velocity.y)
velocity.y = -max_velocity.y;
if(velocity.z > max_velocity.z)
velocity.z = max_velocity.z;
if(velocity.z < -max_velocity.z)
velocity.z = -max_velocity.z;
/* Check for collisions ahead */ /* Check for collisions ahead */
int mouse_x = 0, mouse_y = 0; int mouse_x = 0, mouse_y = 0;
@ -211,17 +189,19 @@ void player_update(struct Player* player, struct Scene* scene, float dt)
normal_ray.direction = normal; normal_ray.direction = normal;
im_ray(&normal_ray, 5.f, (vec4) { 1.f, 0.f, 0.f, 1.f }, 3); im_ray(&normal_ray, 5.f, (vec4) { 1.f, 0.f, 0.f, 1.f }, 3);
float dot = (vec3_dot(&velocity, &normal)); float dot = (vec3_dot(&move_direction, &normal));
vec3 norm_scaled = { 0.f }; vec3 norm_scaled = { 0.f };
vec3_scale(&norm_scaled, &normal, dot); vec3_scale(&normal, &normal, dot);
vec3_sub(&velocity, &velocity, &norm_scaled); vec3_sub(&move_direction, &move_direction, &normal);
debug_vars_show_vec3("Normal", &normal); debug_vars_show_vec3("Normal", &normal);
debug_vars_show_vec3("Dir", &move_direction);
debug_vars_show_float("Dot", dot); debug_vars_show_float("Dot", dot);
} }
} }
} }
/* Check for collisions below */ /* Check for collisions below */
move_speed_vertical += gravity;
struct Ray downward_ray; struct Ray downward_ray;
transform_get_absolute_position(player->mesh, &downward_ray.origin); transform_get_absolute_position(player->mesh, &downward_ray.origin);
vec3_fill(&downward_ray.direction, 0.f, -1.f, 0.f); vec3_fill(&downward_ray.direction, 0.f, -1.f, 0.f);
@ -239,37 +219,30 @@ void player_update(struct Player* player, struct Scene* scene, float dt)
debug_vars_show_float("Collision below", distance); debug_vars_show_float("Collision below", distance);
if(distance > 0.f && distance <= min_downward_distance && !jumping) if(distance > 0.f && distance <= min_downward_distance && !jumping)
{ {
velocity.y = 0.f; //velocity.y = 0.f;
move_speed_vertical = 0.f;
player->grounded = true; player->grounded = true;
} }
} }
} }
float min_velocity = 0.0001f;
float fract_part = 0.f;
double int_part = 0.f;
double int_part2 = 0.f;
fract_part = modf(velocity.x, &int_part);
if(fabsf(fract_part) < min_velocity) velocity.x = 0.f;
fract_part = modf(velocity.z, &int_part2);
if(fabsf(fract_part) < min_velocity) velocity.z = 0.f;
debug_vars_show_vec3("velocity", &velocity);
debug_vars_show_bool("Grounded", player->grounded); debug_vars_show_bool("Grounded", player->grounded);
vec3 offset = {0.f, 0.f, 0.f}; vec3 offset = {0.f, 0.f, 0.f};
vec3_assign(&offset, &velocity); vec3_assign(&offset, &move_direction);
vec3_scale(&offset, &offset, dt); //if(offset.x != 0 || offset.z != 0)
if(offset.x != 0 || offset.z != 0) //{
{ // quat_mul_vec3(&offset, &player->camera_node->base.transform.rotation, &offset);
quat_mul_vec3(&offset, &player->camera_node->base.transform.rotation, &offset); // offset.y = 0.f;
offset.y = 0.f; //}
}
if(velocity.y != 0.f) // Apply speed to direction then translate
offset.y = velocity.y * dt; offset.x *= move_speed * dt;
offset.z *= move_speed * dt;
offset.y = move_speed_vertical * dt;
debug_vars_show_vec3("Translation", &offset);
transform_translate(player, &offset, TS_LOCAL); transform_translate(player, &offset, TS_WORLD);
/* Aiming and Projectiles*/ /* Aiming and Projectiles*/
if(input_mousebutton_state_get(MSB_RIGHT, KS_PRESSED)) if(input_mousebutton_state_get(MSB_RIGHT, KS_PRESSED))
@ -295,11 +268,9 @@ void player_update(struct Player* player, struct Scene* scene, float dt)
vec3_scale(&collision_point, &collision_point, distance); vec3_scale(&collision_point, &collision_point, distance);
vec3_add(&collision_point, &collision_point, &bullet_ray.origin); vec3_add(&collision_point, &collision_point, &bullet_ray.origin);
struct Static_Mesh* bullet = scene_static_mesh_create(game_state_get()->scene, "bullet", NULL, "cube.symbres", MAT_UNSHADED); struct Static_Mesh* bullet = scene_static_mesh_create(game_state_get()->scene, "bullet", NULL, "cube.symbres", MAT_UNSHADED);
transform_set_position(bullet, &collision_point); if(bullet) transform_set_position(bullet, &collision_point);
} }
} }
} }
} }
debug_vars_show_float("Frame Time", dt * 100000.f);
} }

@ -870,7 +870,7 @@ void scene_ray_intersect(struct Scene* scene, struct Ray* ray, struct Raycast_Re
out_results->num_entities_intersected++; out_results->num_entities_intersected++;
if(out_results->num_entities_intersected >= MAX_RAYCAST_ENTITIES_INTERSECT) if(out_results->num_entities_intersected >= MAX_RAYCAST_ENTITIES_INTERSECT)
{ {
log_warning("Reached Max raycast limit"); //log_warning("Reached Max raycast limit");
return; return;
} }
} }

@ -29,7 +29,7 @@ void config_vars_init(struct Hashmap* cvars)
hashmap_int_set(cvars, "video_driver_linux", VD_WAYLAND); hashmap_int_set(cvars, "video_driver_linux", VD_WAYLAND);
hashmap_int_set(cvars, "debug_draw_mode", 0); hashmap_int_set(cvars, "debug_draw_mode", 0);
hashmap_vec4_setf(cvars, "debug_draw_color", 0.8f, 0.4f, 0.1f, 1.f); hashmap_vec4_setf(cvars, "debug_draw_color", 0.8f, 0.4f, 0.1f, 1.f);
hashmap_float_set(cvars, "player_move_speed", 10.f); hashmap_float_set(cvars, "player_move_speed", 20.f);
hashmap_float_set(cvars, "player_move_speed_multiplier", 2.f); hashmap_float_set(cvars, "player_move_speed_multiplier", 2.f);
hashmap_float_set(cvars, "player_turn_speed", 45.f); hashmap_float_set(cvars, "player_turn_speed", 45.f);
} }

Loading…
Cancel
Save