]> git.mcshandy.xyz Git - barrow_crawler/commitdiff
Needs heavy cleanup but we've got basic collision via raycast
authorRandy McShandy <randy@mcshandy.xyz>
Thu, 16 May 2024 03:49:34 +0000 (22:49 -0500)
committerRandy McShandy <randy@mcshandy.xyz>
Thu, 16 May 2024 03:49:34 +0000 (22:49 -0500)
src/render_raylib.c

index cde9047bf6ab8e02d5ee36c52ff286cd6e9c983c..41750d49dbb87b41c346d49fe310c4a771acf7c7 100644 (file)
@@ -15,6 +15,7 @@ Vector3 barrow_position;
 Vector3 barrow_scale;
 Vector3 barrow_rotation_axis;
 float barrow_rotation;
+Matrix barrow_mat;
 
 Image floor_image;
 Texture floor_texture;
@@ -38,6 +39,9 @@ int cam_position_shader_loc;
 int screen_dims_shader_loc;
 int map_center_shader_loc;
 
+Vector3 player_collide_point;
+Ray player_collide_ray;
+RayCollision player_collision;
 Vector3 player_velocity;
 Vector3 player_rotation;
 Vector3 map_center;
@@ -49,32 +53,33 @@ float rotate_speed_decay = 0.10f;
 
 Vector3 vec3_unit = {1.0f, 1.0f, 1.0f};
 Vector3 vec3_010       = {0.0f, 1.0f, 0.0f};
+Vector3 vec3_up        = {0.0f, 1.0f, 0.0f};
+Vector3 vec3_down      = {0.0f, -1.0f, 0.0f};
 
 void process_inputs()
 {
-       /* I guess GetRayCollisionMesh is the best solution here
-                but cameras don't know what they're touching so I'll
-                have to roll my own movement */
+       Vector3 new_player_velocity = player_velocity;
+       Vector3 new_player_rotation = player_rotation;
 
        KeyboardKey key = GetKeyPressed();
        do
        {
                if (IsKeyDown(KEY_W) || IsKeyDown(KEY_K))
                {
-                       player_velocity.x = forward_speed;
+                       new_player_velocity.x = forward_speed;
                }
                else if (IsKeyDown(KEY_S) || IsKeyDown(KEY_J))
                {
-                       player_velocity.x = -forward_speed;
+                       new_player_velocity.x = -forward_speed;
                }
 
                if (IsKeyDown(KEY_A) || IsKeyDown(KEY_H))
                {
-                       player_rotation.x = -rotate_speed;
+                       new_player_rotation.x = -rotate_speed;
                }
                else if (IsKeyDown(KEY_D) || IsKeyDown(KEY_L))
                {
-                       player_rotation.x = rotate_speed;
+                       new_player_rotation.x = rotate_speed;
                }
 
                if (IsKeyDown(KEY_T))
@@ -88,16 +93,30 @@ void process_inputs()
 
                if (IsKeyDown(KEY_R))
                {
-                       player_velocity.z += 0.005f;
+                       new_player_velocity.z += 0.005f;
                }
                else if (IsKeyDown(KEY_F))
                {
-                       player_velocity.z -= 0.005f;
+                       new_player_velocity.z -= 0.005f;
                }
 
                if (IsKeyDown(KEY_SPACE) || IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT))
                {
-                       //player_velocity.x *= 2.0f;
+                       //new_player_velocity.x *= 2.0f;
+               }
+
+               /* Collision test */
+               /* Cast straight up */
+               player_collide_ray = (Ray){cam.position, vec3_up};
+               player_collision = GetRayCollisionMesh(player_collide_ray, barrow_model.meshes[0U], barrow_mat);
+               player_rotation = new_player_rotation;
+               if (player_collision.distance >= 0.3f)
+               {
+                       player_velocity = new_player_velocity;
+               }
+               else
+               {
+                       player_velocity.x *= -1.05;
                }
 
                switch(key)
@@ -172,8 +191,6 @@ void initialize_renderer()
        coffin_rotation = 0.0f;
        coffin_model = LoadModel("./assets/coffin_1.glb");
        while(!IsModelReady(coffin_model)){}
-       //coffin_model.materials[0] = *LoadMaterials("./assets/coffin_1.mtl", &coffin_material_count);
-       //while(!IsMaterialReady(coffin_model.materials[0])){}
        coffin_model.materials[0].shader = shader;
 
        cam                                             = (Camera3D){0};
@@ -202,8 +219,16 @@ void start_render_loop()
        SetTargetFPS(target_fps);
        while (!WindowShouldClose())
        {
+               player_collide_point = cam.position;
+               player_collide_point.y -= 1.0f;
                process_inputs();
 
+               Matrix mat_trans = MatrixTranslate(barrow_position.x, barrow_position.y, barrow_position.z);
+               Matrix mat_rot = MatrixRotate(barrow_rotation_axis, DEG2RAD * barrow_rotation);
+               Matrix mat_scale = MatrixScale(barrow_scale.x, barrow_scale.y, barrow_scale.z);
+               barrow_mat = MatrixMultiply(MatrixMultiply(mat_trans, mat_scale), mat_rot);
+               barrow_model.transform = barrow_mat;
+
                SetShaderValue(shader, cam_position_shader_loc, &cam.position, SHADER_UNIFORM_VEC3);
                SetShaderValue(shader, screen_dims_shader_loc, &fscreen_dims, SHADER_UNIFORM_VEC2);
                SetShaderValue(shader, map_center_shader_loc, &map_center, SHADER_UNIFORM_VEC3);
@@ -215,15 +240,17 @@ void start_render_loop()
                        BeginMode3D(cam);
                        BeginShaderMode(shader);
                                DrawModelEx(coffin_model, coffin_position, coffin_rotation_axis, coffin_rotation, coffin_scale, DARKGRAY);
-                               DrawModelEx(barrow_model, barrow_position, barrow_rotation_axis, barrow_rotation, barrow_scale, DARKGRAY);
+                               //DrawModelEx(barrow_model, barrow_position, barrow_rotation_axis, barrow_rotation, barrow_scale, DARKGRAY);
                                DrawModelEx(floor_model, floor_position, floor_rotation_axis, floor_rotation, floor_scale, DARKGRAY);
-                               //DrawSphere(map_center, 1.0f, RED);
+                               DrawMesh(barrow_model.meshes[0], barrow_model.materials[0], barrow_mat);
+                               DrawRay(player_collide_ray, WHITE);
                        EndShaderMode();
                        EndMode3D();
 
                        DrawTexturePro(barrow_texture, minimap_src, minimap_dest, (Vector2){0.0f, 0.0f}, 0.0f, RAYWHITE);
                        DrawFPS(fscreen_dims.x - 80, 10);
                        DrawText(TextFormat("cam x %f\ncam y %f\ncam z %f", cam.position.x, cam.position.y, cam.position.z), 0, 128, 16, GREEN);
+                       DrawText(TextFormat("ray hit %d\nlength %f", player_collision.hit, player_collision.distance), 0, 256, 16, GREEN);
                EndDrawing();
 
                /* Decay forward velocity and rotation */