strichmaennchen git · main
Entropie festival game; first steps, second try
C 95% GLSL 4.7%git clone https://git.christianimmanuel.de/games/strichmaennchen.gitwget https://git.christianimmanuel.de/games/strichmaennchen/archive/strichmaennchen.tar.gzMoving, jumping, tree.
camera.c | 6 ++ camera.h | 4 ++ game.c | 46 ++++++++++---- game.h | 2 +- human.c | 49 ++++++++++++++- human.h | 6 +- input.c | 10 ++- input.h | 3 +- main.c | 7 ++- mesh.c | 153 +++++++++++++++++++++++++++++++++++++++++----- mesh.h | 9 ++- object.c | 210 +++++++++++++++++++++++++++++++++++++++++++++++++++++---------- object.h | 20 ++++-- world.c | 24 +------- 14 files changed, 452 insertions(+), 97 deletions(-) diff --git a/camera.c b/camera.c index f5042b7..acafd1a 100644 --- a/camera.c +++ b/camera.c @@ -1,7 +1,9 @@ #include "camera.h" +#include "human.h" #include <math.h> #define M_PI 3.14159265358979323846 +#define M_PI_2 6.28318530717958647692 #define DEG2RAD(x) ((x) * (3.14159265358979323846f / 180.0f)) @@ -78,3 +80,7 @@ void Camera_move(Camera* cam, Userinput* input, float dt) { } +void Camera_followPlayer(Camera *cam, Human *player) { + cam->position = vec3_add(player->object->position, (Vec3){0, 1.6f, 0}); // eye height + Camera_update(cam); +} diff --git a/camera.h b/camera.h index fbacd3e..3f0d5ab 100644 --- a/camera.h +++ b/camera.h @@ -4,6 +4,9 @@ #include "input.h" +typedef struct Human Human; + + typedef struct { Vec3 position; Vec3 front; @@ -22,5 +25,6 @@ void Camera_init(Camera* cam, Vec3 pos, float yaw, float pitch, float fov); void Camera_update(Camera* cam); void Camera_rotate(Camera* cam, float yaw_delta, float pitch_delta); void Camera_move(Camera* cam, Userinput* input, float dt); +void Camera_followPlayer(Camera *cam, Human *player); #endif diff --git a/game.c b/game.c index e72fc04..c58e91b 100644 --- a/game.c +++ b/game.c @@ -7,18 +7,29 @@ bool Game_init(Game* game, Renderer* renderer, Camera* camera) { World_init(&game->world); - Human *player = Human_create(vec3(0,2,0), CUBE_VERTICES_STANDARD); - Human *human = Human_create(vec3(1,2,0), CUBE_VERTICES_STANDARD); - - Object* triangle = Object_create(OBJ_TRIANGLE, vec3(2.0f, 4.0f, 1.0f), vec3(0,0,0)); - Object* triangle_1 = Object_create(OBJ_TRIANGLE, vec3(0,0,0), vec3(0,0,0)); - Object* axes = Object_create(OBJ_AXES, vec3(0,0,0), vec3(0,0,0)); - Object* pyramid = Object_create(OBJ_PYRAMID, vec3(0,0,0), vec3(1,0,1)); - Object* cube = Object_create(OBJ_CUBE, vec3(0,0,0), vec3(1,0,1)); - Object* ground = Object_create(OBJ_GROUND, vec3(0,-0.5f,0), vec3(0,0,0)); - - game->ground = cube; - if (!triangle || !triangle_1 || !axes || !pyramid || !cube || !human || !ground) + Human *player = Human_create(vec3(1,0,0), CUBE_VERTICES_MODERN, vec3(0.5f,1.6f,0.25f), true, 60); + player->isPlayer = true; + //Human *human = Human_create(vec3(1,2,0), CUBE_VERTICES_STANDARD, false); + Human *human = Human_create(vec3(0,4,0), CUBE_VERTICES_MODERN, vec3(0.5f,1.6f,0.25f), false, 60); + Human *human_1 = Human_create(vec3(-1,2,1), CUBE_VERTICES_MODERN, vec3(0.5f,1.6f,0.25f), false, 60); + + // for (int j = 0; j<10; j++) for (int i = 0; i<10; i++) World_addHuman(&game->world, Human_create(vec3(i,5,j), CUBE_VERTICES_MODERN, vec3(0.5f,1.6f,0.25f), false, 60)); + + /* + Object* triangle = Object_create(OBJ_TRIANGLE, vec3(2.0f, 4.0f, 1.0f), vec3(0.5f,0.5f,0.0f), vec3(0,0,0)); + Object* axes = Object_create(OBJ_AXES, vec3(0,0,0), vec3(0,0,0), vec3(0,0,0)); + Object* pyramid = Object_create(OBJ_PYRAMID, vec3(0,0,0), vec3(2.0f,2.0f,2.0f), vec3(1,0,1)); + */ + Object* cube = Object_create(OBJ_CUBE, CUBE_VERTICES_WARM, vec3(-0.2f,0,1.5f), vec3(1,1,1), 20); + + Object* ground = Object_create(OBJ_GROUND, CUBE_VERTICES_GROUND, vec3(0,0,0), vec3(10, 1, 10), 0); + Object* ground_1 = Object_create(OBJ_GROUND, CUBE_VERTICES_WARM, vec3(1,0,0), vec3(1, 1, 1), 0); + Object* ground_2 = Object_create(OBJ_GROUND, CUBE_VERTICES_MODERN, vec3(2,1,0), vec3(1, 1, 1), 0); + Object* ground_3 = Object_create(OBJ_GROUND, CUBE_VERTICES_WARM, vec3(3,2,0), vec3(1, 1, 1), 0); + Object* tree = Object_create(OBJ_TREE, CUBE_VERTICES_WARM, vec3(3,0,2), vec3(1, 1, 1), 0); + + game->player = player; + if ( !human || !ground) return false; /* @@ -27,10 +38,19 @@ bool Game_init(Game* game, Renderer* renderer, Camera* camera) { World_addObject(&game->world, pyramid); World_addObject(&game->world, cube); */ + + World_addHuman(&game->world, player); World_addHuman(&game->world, human); - World_addObject(&game->world, axes); + World_addHuman(&game->world, human_1); + //World_addObject(&game->world, axes); + World_addObject(&game->world, cube); + World_addObject(&game->world, cube); World_addObject(&game->world, ground); + World_addObject(&game->world, ground_1); + World_addObject(&game->world, ground_2); + World_addObject(&game->world, ground_3); + World_addObject(&game->world, tree); return true; } diff --git a/game.h b/game.h index 46ecf3b..88043d3 100644 --- a/game.h +++ b/game.h @@ -10,7 +10,7 @@ typedef struct { World world; - Object* ground; + Human* player; // later: score, level state, player refs, etc. } Game; diff --git a/human.c b/human.c index 019b2da..8f47995 100644 --- a/human.c +++ b/human.c @@ -6,13 +6,21 @@ #include "mesh.h" #include "object.h" +float cosf(float x); +float tanf(float x); +float sinf(float x); +float sqrtf(float x); +float atan2f(float y, float x); +#define DEG2RAD(x) ((x) * (3.14159265358979323846f / 180.0f)) +#define M_PI 3.14159265358979323846 +#define M_PI_2 6.28318530717958647692 -Human* Human_create(Vec3 position, VertexPreset preset) { +Human* Human_create(Vec3 position, VertexPreset vertex, Vec3 size, bool is_player, float weight) { Human *human = malloc(sizeof(Human)); if (!human) return NULL; - human->object = Object_create(OBJ_HUMAN, position, (Vec3){0.0f,0.0f,0.0f}); + human->object = Object_create(is_player ? OBJ_HUMAN_PLAYER : OBJ_HUMAN, vertex, position, size, weight); return human; } @@ -20,3 +28,40 @@ void Human_destroy(Human* h) { free(h->object); free(h); } + +void Player_update(Human *player, Userinput *input, float dt, Camera *cam) { + // Flatten the camera's front vector so movement is only in XZ plane + Vec3 flatFront = cam->front; + flatFront.y = 0.0f; + flatFront = vec3_normalize(flatFront); + + // Compute right vector from camera + Vec3 right = vec3_normalize(vec3_cross(flatFront, cam->up)); + + // Movement vector + Vec3 move = vec3_add( + vec3_scale(flatFront, input->move_forward), + vec3_scale(right, input->move_sideway) + ); + + // Move player + if (vec3_length(move) > 0.0f) { + move = vec3_scale(vec3_normalize(move), 5.0f); + player->object->movement = move; + } else { + player->object->movement = (Vec3){0,0,0}; + } + + // Jump + if (input->jump > 0 && player->object->is_grounded) { + float jump_vel = input->jump == 1 ? 5.0 : 8.0; + player->object->velocity.y = jump_vel; // tweak jump strength + player->object->is_grounded = false; + input->jump = 0; + } + + //player->object->position = vec3_add(player->object->position, vec3_scale(move, dt)); + // Rotate player to face camera direction + float yaw = atan2f(flatFront.z, flatFront.x) + M_PI/2; // x = right, z = forward + player->object->rotation.y = yaw; +} diff --git a/human.h b/human.h index 9184fbb..dd21fd8 100644 --- a/human.h +++ b/human.h @@ -3,11 +3,13 @@ #include "object.h" -typedef struct { +typedef struct Human { Object* object; + bool isPlayer; } Human; -Human* Human_create(Vec3 position, VertexPreset preset); +Human* Human_create(Vec3 position, VertexPreset preset, Vec3 size, bool is_player, float weiht); +void Player_update(Human *player, Userinput *input, float dt, Camera *cam); #endif diff --git a/input.c b/input.c index 0b8d57d..effa683 100644 --- a/input.c +++ b/input.c @@ -11,6 +11,7 @@ void Input_reset(Userinput* input) { input->rotate_object_sideway = 0; input->yaw_delta = 0; input->pitch_delta = 0; + input->jump = 0; } @@ -23,16 +24,23 @@ void Input_process(Userinput* userinput) { } const bool *keys = SDL_GetKeyboardState(NULL); - if (keys[SDL_SCANCODE_LSHIFT]) { + if (keys[SDL_SCANCODE_LCTRL]) { if (keys[SDL_SCANCODE_W]) userinput->rotate_object_forward += 1.0f; if (keys[SDL_SCANCODE_S]) userinput->rotate_object_forward -= 1.0f; if (keys[SDL_SCANCODE_A]) userinput->rotate_object_sideway -= 1.0f; if (keys[SDL_SCANCODE_D]) userinput->rotate_object_sideway += 1.0f; + } else if (keys[SDL_SCANCODE_LSHIFT]) { + if (keys[SDL_SCANCODE_SPACE]) userinput->jump = 2; + if (keys[SDL_SCANCODE_W]) userinput->move_forward += 1.0f; + if (keys[SDL_SCANCODE_S]) userinput->move_forward -= 1.0f; + if (keys[SDL_SCANCODE_A]) userinput->move_sideway -= 1.0f; + if (keys[SDL_SCANCODE_D]) userinput->move_sideway += 1.0f; } else { if (keys[SDL_SCANCODE_W]) userinput->move_forward += 1.0f; if (keys[SDL_SCANCODE_S]) userinput->move_forward -= 1.0f; if (keys[SDL_SCANCODE_A]) userinput->move_sideway -= 1.0f; if (keys[SDL_SCANCODE_D]) userinput->move_sideway += 1.0f; + if (keys[SDL_SCANCODE_SPACE]) userinput->jump = 1; } float dx, dy; diff --git a/input.h b/input.h index e32214d..2e65975 100644 --- a/input.h +++ b/input.h @@ -9,11 +9,10 @@ typedef struct { char rotate_object_sideway; float yaw_delta; float pitch_delta; + char jump; } Userinput; void Input_reset(Userinput* input); void Input_process(Userinput* userinput); - - #endif diff --git a/main.c b/main.c index a349898..962f364 100644 --- a/main.c +++ b/main.c @@ -45,6 +45,8 @@ int main(void) { while (userinput.running) { float dt = Time_delta(&prev_ticks); + if (dt > 0.05f) dt = 0.05f; + prev_ticks = SDL_GetTicks(); Renderer_resize(&renderer); @@ -55,9 +57,12 @@ int main(void) { Input_process(&userinput); Camera_rotate(&camera, userinput.yaw_delta, userinput.pitch_delta); - Camera_move(&camera, &userinput, dt); + //Camera_move(&camera, &userinput, dt); + Player_update(game.player, &userinput, dt, &camera); Game_update(&game, &camera, &userinput, dt); + + Camera_followPlayer(&camera, game.player); Game_render(&game, &renderer, &camera); //Renderer_testCube(&renderer, &camera); diff --git a/mesh.c b/mesh.c index 1a6dcbd..6ad5dd0 100644 --- a/mesh.c +++ b/mesh.c @@ -4,6 +4,14 @@ #include "stdio.h" #include "glfuncs.h" +#include <stdlib.h> + +float cosf(float x); +float tanf(float x); +float sinf(float x); + +#define M_PI 3.14159265358979323846 + const Mesh mesh_default = { @@ -15,7 +23,7 @@ const Mesh mesh_default = { .hasIndices = false, }; -void Mesh_draw(Mesh mesh, struct Renderer *renderer, Mat4 mvp, Vec3 color) { +void Mesh_draw(Mesh mesh, struct Renderer *renderer, Mat4 mvp) { glUseProgram(renderer->shaderProgram); // Set MVP uniform @@ -122,7 +130,7 @@ Mesh Mesh_createPyramid(float size, float height) { } -Mesh Mesh_createCube(Vec3 position, Vec3 size, VertexPreset preset) { +Mesh Mesh_createCube(Vec3 size, VertexPreset preset) { float hx = size.x/2.0f; float hy = size.y/2.0f; float hz = size.z/2.0f; @@ -143,6 +151,18 @@ Mesh Mesh_createCube(Vec3 position, Vec3 size, VertexPreset preset) { faceColors[4][0]=1;faceColors[4][1]=0;faceColors[4][2]=1; // bottom faceColors[5][0]=0;faceColors[5][1]=1;faceColors[5][2]=1; // top break; + case CUBE_VERTICES_GROUND: + // back/front/left/right -> brown (dirt) + for (int i=0;i<4;i++){ + faceColors[i][0]=0.55f; + faceColors[i][1]=0.27f; + faceColors[i][2]=0.07f; + } + // bottom -> darker brown + faceColors[4][0]=0.3f; faceColors[4][1]=0.15f; faceColors[4][2]=0.05f; + // top -> green (grass) + faceColors[5][0]=0.1f; faceColors[5][1]=0.8f; faceColors[5][2]=0.1f; + break; case CUBE_VERTICES_WARM: for (int i=0;i<6;i++){ faceColors[i][0]=1; faceColors[i][1]=0.5; faceColors[i][2]=0; } break; @@ -161,9 +181,9 @@ Mesh Mesh_createCube(Vec3 position, Vec3 size, VertexPreset preset) { for (int f=0; f<6; f++) { for (int v=0; v<4; v++) { - vertices[vi++] = position.x + faceOffsets[f][v].x; - vertices[vi++] = position.y + faceOffsets[f][v].y; - vertices[vi++] = position.z + faceOffsets[f][v].z; + vertices[vi++] = faceOffsets[f][v].x; + vertices[vi++] = faceOffsets[f][v].y; + vertices[vi++] = faceOffsets[f][v].z; vertices[vi++] = faceColors[f][0]; vertices[vi++] = faceColors[f][1]; vertices[vi++] = faceColors[f][2]; @@ -208,6 +228,97 @@ Mesh Mesh_createCube(Vec3 position, Vec3 size, VertexPreset preset) { +Mesh Mesh_createTree(float trunkHeight, float trunkRadius, int leafLayers, float leafRadius) { + Mesh m = mesh_default; + + const int segments = 12; // circular segments for trunk and leaves + int vOffset = 0; + + int maxVertices = segments*36 + leafLayers*segments*18; + float* vertices = malloc(sizeof(float) * maxVertices); + if (!vertices) { + fprintf(stderr, "Failed to allocate memory for tree vertices\n"); + return mesh_default; + } + + // Trunk (cylinder) + for (int i=0; i<segments; i++) { + float angle0 = 2*M_PI*i/segments; + float angle1 = 2*M_PI*((i+1)%segments)/segments; + + float x0 = cosf(angle0)*trunkRadius; + float z0 = sinf(angle0)*trunkRadius; + float x1 = cosf(angle1)*trunkRadius; + float z1 = sinf(angle1)*trunkRadius; + + float y0 = 0; + float y1 = trunkHeight; + + // side quad as two triangles + // triangle 1 + vertices[vOffset++] = x0; vertices[vOffset++] = y0; vertices[vOffset++] = z0; // pos + vertices[vOffset++] = 0.55f; vertices[vOffset++] = 0.27f; vertices[vOffset++] = 0.07f; // brown + vertices[vOffset++] = x1; vertices[vOffset++] = y0; vertices[vOffset++] = z1; + vertices[vOffset++] = 0.55f; vertices[vOffset++] = 0.27f; vertices[vOffset++] = 0.07f; + vertices[vOffset++] = x1; vertices[vOffset++] = y1; vertices[vOffset++] = z1; + vertices[vOffset++] = 0.55f; vertices[vOffset++] = 0.27f; vertices[vOffset++] = 0.07f; + + // triangle 2 + vertices[vOffset++] = x0; vertices[vOffset++] = y0; vertices[vOffset++] = z0; + vertices[vOffset++] = 0.55f; vertices[vOffset++] = 0.27f; vertices[vOffset++] = 0.07f; + vertices[vOffset++] = x1; vertices[vOffset++] = y1; vertices[vOffset++] = z1; + vertices[vOffset++] = 0.55f; vertices[vOffset++] = 0.27f; vertices[vOffset++] = 0.07f; + vertices[vOffset++] = x0; vertices[vOffset++] = y1; vertices[vOffset++] = z0; + vertices[vOffset++] = 0.55f; vertices[vOffset++] = 0.27f; vertices[vOffset++] = 0.07f; + } + + // Leaves (cones, layered from top to bottom) + for (int l=0; l<leafLayers; l++) { + float yLayer = trunkHeight + l*0.6f; // staggered layers + float rLayer = leafRadius * (1.0f - (float)l/leafLayers); + + for (int i=0; i<segments; i++) { + float angle0 = 2*M_PI*i/segments; + float angle1 = 2*M_PI*((i+1)%segments)/segments; + + float x0 = cosf(angle0)*rLayer; + float z0 = sinf(angle0)*rLayer; + float x1 = cosf(angle1)*rLayer; + float z1 = sinf(angle1)*rLayer; + + // triangle pointing to top of cone + vertices[vOffset++] = 0; vertices[vOffset++] = yLayer + 0.8f; vertices[vOffset++] = 0; + vertices[vOffset++] = 0.0f; vertices[vOffset++] = 0.8f; vertices[vOffset++] = 0.0f; // green + vertices[vOffset++] = x0; vertices[vOffset++] = yLayer; vertices[vOffset++] = z0; + vertices[vOffset++] = 0.0f; vertices[vOffset++] = 0.8f; vertices[vOffset++] = 0.0f; + vertices[vOffset++] = x1; vertices[vOffset++] = yLayer; vertices[vOffset++] = z1; + vertices[vOffset++] = 0.0f; vertices[vOffset++] = 0.8f; vertices[vOffset++] = 0.0f; + } + } + + // Generate OpenGL buffers + glGenVertexArrays(1, &m.vao); + glGenBuffers(1, &m.vbo); + + glBindVertexArray(m.vao); + glBindBuffer(GL_ARRAY_BUFFER, m.vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vOffset, vertices, GL_STATIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)0); + glEnableVertexAttribArray(0); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)(3*sizeof(float))); + glEnableVertexAttribArray(1); + + glBindVertexArray(0); + + m.vertexCount = vOffset / 6; + m.drawMode = GL_TRIANGLES; + m.ebo = 0; + m.hasIndices = false; + + free(vertices); + return m; +} Mesh Mesh_createAxes(float length) { float vertices[] = { @@ -268,6 +379,18 @@ static void addCube( faceColors[4][0]=1;faceColors[4][1]=0;faceColors[4][2]=1; faceColors[5][0]=0;faceColors[5][1]=1;faceColors[5][2]=1; break; + case CUBE_VERTICES_GROUND: + // back/front/left/right -> brown (dirt) + for (int i=0;i<4;i++){ + faceColors[i][0]=0.55f; + faceColors[i][1]=0.27f; + faceColors[i][2]=0.07f; + } + // bottom -> darker brown + faceColors[4][0]=0.3f; faceColors[4][1]=0.15f; faceColors[4][2]=0.05f; + // top -> green (grass) + faceColors[5][0]=0.1f; faceColors[5][1]=0.8f; faceColors[5][2]=0.1f; + break; case CUBE_VERTICES_WARM: for (int f=0; f<6; f++){ faceColors[f][0]=1; faceColors[f][1]=0.5; faceColors[f][2]=0; } break; @@ -302,7 +425,7 @@ static void addCube( *vertexBase += 24; } -Mesh Mesh_createHuman(Vec3 position, VertexPreset preset) { +Mesh Mesh_createHuman(VertexPreset preset, bool is_player) { Mesh m = mesh_default; // Temporary arrays for vertices and indices @@ -317,16 +440,18 @@ Mesh Mesh_createHuman(Vec3 position, VertexPreset preset) { // Function to append a cube to the arrays // Add all body parts (positions relative to root) - addCube(vertices, indices, &vOffset, &iOffset, &vertexBase, (Vec3){position.x, position.y + 0.5f, position.z}, (Vec3){0.5f,1.0f,0.25f}, preset); // torso - addCube(vertices, indices, &vOffset, &iOffset, &vertexBase, (Vec3){position.x, position.y + 1.25f, position.z}, (Vec3){0.3f,0.3f,0.3f}, preset); // head - addCube(vertices, indices, &vOffset, &iOffset, &vertexBase, (Vec3){position.x - 0.4f, position.y + 0.75f, position.z}, (Vec3){0.2f,0.75f,0.2f}, preset); // left arm - addCube(vertices, indices, &vOffset, &iOffset, &vertexBase, (Vec3){position.x + 0.4f, position.y + 0.75f, position.z}, (Vec3){0.2f,0.75f,0.2f}, preset); // right arm - addCube(vertices, indices, &vOffset, &iOffset, &vertexBase, (Vec3){position.x - 0.15f, position.y - 0.25f, position.z}, (Vec3){0.2f,0.75f,0.2f}, preset); // left leg - addCube(vertices, indices, &vOffset, &iOffset, &vertexBase, (Vec3){position.x + 0.15f, position.y - 0.25f, position.z}, (Vec3){0.2f,0.75f,0.2f}, preset); // right leg + // legs=4, torso=3, arms=4, head=1 -> center of human needs to be y=0 + addCube(vertices, indices, &vOffset, &iOffset, &vertexBase, (Vec3){0.0f, 0.285f, 0.0f}, (Vec3){0.5f,0.57f,0.25f}, preset); // torso + if (!is_player) + addCube(vertices, indices, &vOffset, &iOffset, &vertexBase, (Vec3){0.0f, 0.72f, 0.0f}, (Vec3){0.2f,0.2f,0.2f}, preset); // head + addCube(vertices, indices, &vOffset, &iOffset, &vertexBase, (Vec3){-0.4f, 0.17f, 0.0f}, (Vec3){0.2f,0.76f,0.2f}, preset); // left arm + addCube(vertices, indices, &vOffset, &iOffset, &vertexBase, (Vec3){0.4f, 0.17f, 0.0f}, (Vec3){0.2f,0.76f,0.2f}, preset); // right arm + addCube(vertices, indices, &vOffset, &iOffset, &vertexBase, (Vec3){0.15f, -0.4f, 0.0f}, (Vec3){0.2f,0.76f,0.2f}, preset); // left leg + addCube(vertices, indices, &vOffset, &iOffset, &vertexBase, (Vec3){-0.15f, -0.4f, 0.0f}, (Vec3){0.2f,0.76f,0.2f}, preset); // right leg // Create mesh - m.vertexCount = 6*24; - m.indexCount = 6*36; + m.vertexCount = vertexBase; + m.indexCount = iOffset; m.hasIndices = true; m.drawMode = GL_TRIANGLES; diff --git a/mesh.h b/mesh.h index 56c808c..af5d053 100644 --- a/mesh.h +++ b/mesh.h @@ -1,6 +1,7 @@ #ifndef MESH_H #define MESH_H +#include <SDL3/SDL.h> #include <GL/gl.h> #include "linalg.h" @@ -9,6 +10,7 @@ struct Renderer; typedef enum { CUBE_VERTICES_STANDARD, + CUBE_VERTICES_GROUND, CUBE_VERTICES_MODERN, CUBE_VERTICES_WARM, // add more presets if needed @@ -26,12 +28,13 @@ typedef struct Mesh { extern const Mesh mesh_default; -void Mesh_draw(Mesh mesh, struct Renderer *renderer, Mat4 mvp, Vec3 color); +void Mesh_draw(Mesh mesh, struct Renderer *renderer, Mat4 mvp); Mesh Mesh_createTriangle(); Mesh Mesh_createPyramid(float size, float height); //Mesh Mesh_createCube(float size, float height); -Mesh Mesh_createCube(Vec3 position, Vec3 size, VertexPreset preset); -Mesh Mesh_createHuman(Vec3 position, VertexPreset preset); +Mesh Mesh_createCube(Vec3 size, VertexPreset preset); +Mesh Mesh_createHuman(VertexPreset preset, bool is_player); Mesh Mesh_createAxes(float length); +Mesh Mesh_createTree(float trunkHeight, float trunkRadius, int leafLayers, float leafRadius); #endif diff --git a/object.c b/object.c index 2c37526..a41ad7d 100644 --- a/object.c +++ b/object.c @@ -7,69 +7,78 @@ double fabs(double x); #include "object.h" #include "linalg.h" -bool AABB_overlap(Collider a, Collider b) { - return (fabs(a.position.x - b.position.x) * 2 < (a.size.x + b.size.x)) && - (fabs(a.position.y - b.position.y) * 2 < (a.size.y + b.size.y)) && - (fabs(a.position.z - b.position.z) * 2 < (a.size.z + b.size.z)); -} - -void Collider_positionGround(Object* obj, Object* ground) { - if (AABB_overlap(obj->collider, ground->collider)) { - obj->position.y = ground->position.y + ground->collider.size.y/2 + obj->collider.size.y/2; - obj->velocity.y = 0; - } -} - - -Object *Object_create(ObjectType type, Vec3 position, Vec3 color) { +Object *Object_create(ObjectType type, VertexPreset vertex, Vec3 position, Vec3 size, float weight) { Object *obj = malloc(sizeof(Object)); if (!obj) return NULL; + obj->type = type; obj->position = position; + obj->weight = weight; + + obj->vertex = vertex; obj->rotation = (Vec3){0,0,0}; obj->scale = (Vec3){1,1,1}; - obj->color = color; obj->mesh = mesh_default; obj->velocity = (Vec3){0,0,0}; - obj->type = type; switch (type) { case OBJ_TRIANGLE: obj->mesh = Mesh_createTriangle(); - obj->collider = (Collider){position, (Vec3){0.5f,0.5f,0.0f}, true}; + obj->collider.size = size; + obj->collider.isStatic = true; break; case OBJ_AXES: obj->mesh = Mesh_createAxes(200); - obj->collider = (Collider){position, (Vec3){0,0,0}, true}; // no collision + obj->collider.size = size; + obj->collider.isStatic = true; break; case OBJ_PYRAMID: obj->mesh = Mesh_createPyramid(4.0f, 4.0f); - obj->collider = (Collider){position, (Vec3){2.0f,2.0f,2.0f}, true}; + obj->collider.size = size; + obj->collider.isStatic = true; break; case OBJ_CUBE: - obj->mesh = Mesh_createCube((Vec3){0.0f,0.0f,0.0f}, (Vec3){2.0f,2.0f,2.0f}, CUBE_VERTICES_MODERN); - obj->collider = (Collider){position, (Vec3){1.0f,1.0f,1.0f}, true}; + obj->mesh = Mesh_createCube(size, vertex); + obj->collider.size = size; + obj->collider.isStatic = false; break; case OBJ_HUMAN: - obj->mesh = Mesh_createHuman((Vec3){0.0f,0.0f,0.0f}, CUBE_VERTICES_MODERN); - obj->collider = (Collider){position, (Vec3){0.5f,1.5f,0.25f}, false}; // rough box around human + obj->mesh = Mesh_createHuman(vertex, false); + obj->collider.size = size; + obj->collider.isStatic = false; + break; + case OBJ_HUMAN_PLAYER: + obj->mesh = Mesh_createHuman(vertex, true); + obj->collider.size = size; + obj->collider.isStatic = false; break; case OBJ_GROUND: - obj->mesh = Mesh_createCube(position, (Vec3){10, 1, 10}, CUBE_VERTICES_STANDARD); - obj->collider = (Collider){(Vec3){0, -0.5f, 0}, (Vec3){10, 1, 10}, true}; // rough box around human + obj->mesh = Mesh_createCube(size, vertex); + obj->collider.size = size; + obj->collider.isStatic = true; break; + case OBJ_TREE: + obj->mesh = Mesh_createTree(3.0f, 0.3f, 8, 1.5f);; + obj->collider.size = size; + obj->collider.isStatic = true; + break; + default: obj->mesh = mesh_default; - obj->collider = (Collider){position, (Vec3){0,0,0}, true}; + obj->collider.size = (Vec3){0,0,0}; + obj->collider.isStatic = true; fprintf(stderr, "Warning: unsupported ObjectType (%d)\n", type); break; } + obj->position_center = (Vec3){0, obj->collider.size.y/2.0f, 0}; + obj->collider.position = obj->position_center; + return obj; } @@ -81,22 +90,161 @@ void Object_render(Object *obj, Renderer *renderer, Camera *camera) { if (!obj) return; Mat4 projection = mat4_perspective(camera->fov, - renderer->width / renderer->height, + (float)renderer->width / renderer->height, 0.1f, 100.0f); Mat4 view = mat4_lookAt(camera->position, vec3_add(camera->position, camera->front), camera->up); + // Translate by bottom position + half height to center the mesh + Vec3 renderPos = vec3_add(obj->position, (Vec3){0, obj->collider.size.y / 2.0f, 0}); + Mat4 model = mat4_identity(); - model = mat4_mul(model, mat4_translate(obj->position)); + model = mat4_mul(model, mat4_translate(renderPos)); model = mat4_mul(model, mat4_rotate_x(obj->rotation.x)); model = mat4_mul(model, mat4_rotate_y(obj->rotation.y)); model = mat4_mul(model, mat4_rotate_z(obj->rotation.z)); model = mat4_mul(model, mat4_scale(obj->scale)); - Mat4 mvp = mat4_mul(projection, mat4_mul(view, model)); - Mesh_draw(obj->mesh, renderer, mvp, obj->color); + Mesh_draw(obj->mesh, renderer, mvp); +} + + + + + + + + + + + + + + + + + +bool AABB_overlap(Object* o_a, Object* o_b) { + Collider a = o_a->collider; + Collider b = o_b->collider; + Vec3 n_a = vec3_add(o_a->position,a.position); + Vec3 n_b = vec3_add(o_b->position,b.position); + return (fabs(n_a.x - n_b.x) * 2 < (a.size.x + b.size.x)) && + (fabs(n_a.y - n_b.y) * 2 < (a.size.y + b.size.y)) && + (fabs(n_a.z - n_b.z) * 2 < (a.size.z + b.size.z)); +} + +void Collider_positionGround(Object* obj, Object* ground) { + float target_y = ground->position.y + ground->collider.size.y; + + if (obj->position.y < target_y) { + //printf("%f %f %f %f\n", target_y, ground->position.y, ground->collider.size.y, obj->position.y); + obj->position.y = target_y; // snap to top of ground + obj->velocity.y = 0; + } + obj->is_grounded = true; +} + + +void Object_applyGravity(Object *obj, float dt) { + if (obj->collider.isStatic || obj->is_grounded) return; + + obj->velocity.y -= 9.81f * dt; +} + +void Object_resolveDynamicCollision(Object *obj, Object *other) { + if (obj->collider.isStatic || other->collider.isStatic) return; + + if (!AABB_overlap(obj, other)) return; + + // 1. Compute the push direction in XZ plane (ignore Y for now) + Vec3 delta = vec3_sub(obj->position, other->position); + delta.y = 0.0f; // horizontal push only + if (vec3_length(delta) == 0) { + delta.x = 0.01f; // avoid zero-length vector + delta.z = 0.01f; + } + Vec3 normal = vec3_normalize(delta); + + // 2. Compute weight-based impulse + float totalWeight = obj->weight + other->weight; + float pushStrength = 5.0f; // tweak this to control push force + + Vec3 impulseObj = vec3_scale(normal, other->weight / totalWeight * pushStrength); + Vec3 impulseOther = vec3_scale(normal, obj->weight / totalWeight * pushStrength); + + // 3. Apply impulses to velocity + obj->velocity = vec3_add(obj->velocity, impulseObj); + other->velocity = vec3_add(other->velocity, vec3_scale(impulseOther, -1.0f)); + + // 4. Optional: separate objects slightly to prevent overlap + float overlapX = (obj->collider.size.x + other->collider.size.x)/2 - fabs(obj->position.x - other->position.x); + float overlapZ = (obj->collider.size.z + other->collider.size.z)/2 - fabs(obj->position.z - other->position.z); + + if (overlapX > 0) { + obj->position.x += normal.x * (overlapX * 0.5f); + other->position.x -= normal.x * (overlapX * 0.5f); + } + if (overlapZ > 0) { + obj->position.z += normal.z * (overlapZ * 0.5f); + other->position.z -= normal.z * (overlapZ * 0.5f); + } +} + +void Object_applyFriction(Object *obj, float dt) { + if (obj->collider.isStatic) return; + + // Base friction coefficient (tweak) + float frictionCoeff = 0.5f; + + // Friction proportional to weight (mass) + float friction = frictionCoeff * obj->weight; + + // Apply only to horizontal velocity (XZ) + obj->velocity.x -= obj->velocity.x * friction * dt; + obj->velocity.z -= obj->velocity.z * friction * dt; + + // Stop very small velocities to prevent jitter + if (fabs(obj->velocity.x) < 0.01f) obj->velocity.x = 0; + if (fabs(obj->velocity.z) < 0.01f) obj->velocity.z = 0; +} + +void Object_resolveCollisions(Object *obj, Object **worldObjects, int count) { + for (int j = 0; j < count; j++) { + Object *other = worldObjects[j]; + if (other == obj) continue; + + if (other->collider.isStatic) { + if (AABB_overlap(obj, other)) { + Collider_positionGround(obj, other); + } + } else { + Object_resolveDynamicCollision(obj, other); + } + } +} + +void Object_update(Object *obj, Object **worldObjects, int count, float dt) { + if (obj->collider.isStatic) return; + + Object_applyGravity(obj, dt); + + Vec3 total = vec3_add(obj->movement, obj->velocity); + if (vec3_length(total) > 0.0f) { + obj->is_grounded = false; + obj->position = vec3_add(obj->position, vec3_scale(total, dt)); + Object_resolveCollisions(obj, worldObjects, count); + } + + Object_applyFriction(obj, dt); + + obj->movement = (Vec3){0,0,0}; +} + +void Object_addMovement(Object *obj, Vec3 move) { + obj->velocity = vec3_add(obj->velocity, move); } diff --git a/object.h b/object.h index 83f1c1c..96f85c2 100644 --- a/object.h +++ b/object.h @@ -19,26 +19,38 @@ typedef enum { OBJ_CUBE, OBJ_SPHERE, OBJ_HUMAN, + OBJ_HUMAN_PLAYER, OBJ_GROUND, + OBJ_TREE, // later: cylinder, plane, etc. } ObjectType; typedef struct { ObjectType type; - Vec3 position; + VertexPreset vertex; + Vec3 position; // feet + Vec3 position_center; Vec3 rotation; Vec3 scale; Mesh mesh; Vec3 color; Collider collider; - Vec3 velocity; // for gravity/movement + Vec3 velocity; // for gravity/getting pushed/throwing + Vec3 movement; // for movement + bool is_grounded; + float weight; // in kilogramm } Object; -Object *Object_create(ObjectType type, Vec3 position, Vec3 color); +Object *Object_create(ObjectType type, VertexPreset vertex, Vec3 position, Vec3 size, float weight); void Object_render(Object *obj, Renderer *renderer, Camera *camera); void Object_destroy(Object* obj); -bool AABB_overlap(Collider a, Collider b); +bool AABB_overlap(Object* o_a, Object* o_b); void Collider_positionGround(Object* obj, Object* ground); +void Object_applyGravity(Object *obj, float dt); +void Object_resolveCollisions(Object *obj, Object **worldObjects, int count); +void Object_update(Object *obj, Object **worldObjects, int count, float dt); +void Object_addMovement(Object *obj, Vec3 move); + #endif /* GAMEOBJECT_H */ diff --git a/world.c b/world.c index ac603e2..ec60f97 100644 --- a/world.c +++ b/world.c @@ -48,29 +48,7 @@ void World_removeHuman(World *world, Human *hum) { void World_update(World *world, float dt) { for (int i = 0; i < world->countObjects; i++) { - Object *obj = world->objects[i]; - - if (!obj->collider.isStatic) { - // Apply gravity - obj->velocity.y -= 9.81f * dt; - obj->position.y += obj->velocity.y * dt; - - // Update collider position (so collision checks use new pos) - obj->collider.position = obj->position; - - // Check collisions with static objects (like ground) - for (int j = 0; j < world->countObjects; j++) { - Object *other = world->objects[j]; - if (other == obj) continue; - if (!other->collider.isStatic) continue; - - if (AABB_overlap(obj->collider, other->collider)) { - Collider_positionGround(obj, other); - // Update collider again after resolution - obj->collider.position = obj->position; - } - } - } + Object_update(world->objects[i], world->objects, world->countObjects, dt); } }