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.gzbefore trying to rotate collission..
game.c | 108 ++++++++++++++++- human.c | 11 +- input.c | 14 ++- input.h | 1 + mesh.c | 418 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mesh.h | 3 + object.c | 192 +++++++++++++++++++---------- object.h | 6 +- 8 files changed, 678 insertions(+), 75 deletions(-) diff --git a/game.c b/game.c index f7a8898..ef6ba44 100644 --- a/game.c +++ b/game.c @@ -6,6 +6,10 @@ #include <stdio.h> #include <float.h> + +#define M_PI 3.14159265358979323846 +#define DEG2RAD(d) ((d) * (M_PI / 180.0f)) + bool Game_init(Game* game) { //game->time = Time_init(3600.0f); game->time = Time_init(3.0f); @@ -17,9 +21,42 @@ bool Game_init(Game* game) { 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); - Object* ground = Object_create(OBJ_GROUND, CUBE_VERTICES_GROUND, vec3(0,0,0), vec3(30, 1, 30), 0); - Object* tree = Object_create(OBJ_TREE, CUBE_VERTICES_WARM, vec3(3,0,2), vec3(1, 1, 1), 0); - Object* cube = Object_create(OBJ_CUBE, CUBE_VERTICES_WARM, vec3(-0.2f,0,1.5f), vec3(2,1,2), 20); + Object* ground = Object_create(OBJ_GROUND, CUBE_VERTICES_GROUND, vec3(0,-1,0), vec3(130, 1, 130), 0); + Object* ground_1 = Object_create(OBJ_GROUND, CUBE_VERTICES_GROUND, vec3(0,0,0), vec3(1, 1, 1), 0); + Object* tree = Object_create(OBJ_TREE, CUBE_VERTICES_WARM, vec3(3,0,2), vec3(0.5f, 10, 0.5f), 0); + + + + + Object* cylinder = Object_create(OBJ_CYLINDER, CUBE_VERTICES_WARM, vec3(-3,0,1), vec3(1,3,1), 100); + Object* cylinder_1 = Object_create(OBJ_CYLINDER, CUBE_VERTICES_MODERN, vec3(-3,0,3), vec3(1,3,1), 100); + cylinder->rotation.x = DEG2RAD(90); + World_addObject(&game->world, cylinder); + World_addObject(&game->world, cylinder_1); + + + /* + Object* cube = Object_create(OBJ_WALL, CUBE_VERTICES_MODERN, vec3(1,0,8), vec3(1,1,1), 20); + Object* cube_1 = Object_create(OBJ_WALL, CUBE_VERTICES_MODERN, vec3(3,0,8), vec3(1,1,1), 20); + Object* cube_2 = Object_create(OBJ_WALL, CUBE_VERTICES_MODERN, vec3(5,0,8), vec3(1,1,1), 20); + Object* cube_3 = Object_create(OBJ_WALL, CUBE_VERTICES_MODERN, vec3(7,0,8), vec3(1,1,1), 20); + Object* cube_4 = Object_create(OBJ_WALL, CUBE_VERTICES_MODERN, vec3(0,0,8), vec3(1,1,1), 20); + cube_1->rotation.y = DEG2RAD(90); + cube_2->rotation.y = DEG2RAD(180); + cube_3->rotation.y = DEG2RAD(270); + cube_4->rotation.y = DEG2RAD(270); + World_addObject(&game->world, cube); + World_addObject(&game->world, cube_1); + World_addObject(&game->world, cube_2); + World_addObject(&game->world, cube_3); + World_addObject(&game->world, cube_4); + */ + + Object* cube = Object_create(OBJ_CUBE, CUBE_VERTICES_MODERN, vec3(2,2,8), vec3(1,3,1), 20); + cube->rotation.x = DEG2RAD(90); + Object* cuber = Object_create(OBJ_CYLINDER, CUBE_VERTICES_MODERN, vec3(1,0,8), vec3(1,1,1), 20); + World_addObject(&game->world, cube); + //World_addObject(&game->world, cuber); // 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)); @@ -41,14 +78,77 @@ bool Game_init(Game* game) { World_addObject(&game->world, cube); */ + Object* house_main = Object_create(OBJ_HOUSE_MAIN, CUBE_VERTICES_WARM, vec3(10,0,20), vec3(18.0f,11.0f,12.0f), 20); + house_main->rotation.y = DEG2RAD(180); + World_addObject(&game->world, house_main); + + Object* house_main_1 = Object_create(OBJ_HOUSE_MAIN, CUBE_VERTICES_WARM, vec3(-5,0,23), vec3(18.0f,11.0f,12.0f), 20); + house_main_1->rotation.y = DEG2RAD(90); + World_addObject(&game->world, house_main_1); + + Object* barn = Object_create(OBJ_BARN, CUBE_VERTICES_WARM, vec3(2,0,-20), vec3(24.0f,11.0f,12.0f), 20); + barn->rotation.y = DEG2RAD(180); + World_addObject(&game->world, barn); + + Object* house_side_wall_back = Object_create(OBJ_WALL, CUBE_VERTICES_WARM, vec3(27,0,0), vec3(0.5,5,28), 20); + Object* house_side_wall_side_1 = Object_create(OBJ_WALL, CUBE_VERTICES_WARM, vec3(23.125,0,14.25f), vec3(8.25,5,0.5), 20); + Object* house_side_wall_side_2 = Object_create(OBJ_WALL, CUBE_VERTICES_WARM, vec3(23.125,0,9), vec3(8.25,5,0.5), 20); + Object* house_side_wall_side_3 = Object_create(OBJ_WALL, CUBE_VERTICES_WARM, vec3(23.125,0,-4), vec3(8.25,5,0.5), 20); + Object* house_side_wall_side_4 = Object_create(OBJ_WALL, CUBE_VERTICES_WARM, vec3(23.125,0,-14), vec3(8.25,5,0.5), 20); + + Object* house_side_wall_front = Object_create(OBJ_WALL, CUBE_VERTICES_WARM, vec3(19.25f,3,0), vec3(0.5,2,28), 20); + Object* house_side_wall_front_1 = Object_create(OBJ_WALL, CUBE_VERTICES_WARM, vec3(19.25f,0,13.5f), vec3(0.5,3,2), 20); + Object* house_side_wall_front_2 = Object_create(OBJ_WALL, CUBE_VERTICES_WARM, vec3(19.25f,0,7), vec3(0.5,3,8), 20); + Object* house_side_wall_front_3 = Object_create(OBJ_WALL, CUBE_VERTICES_WARM, vec3(19.25f,0,-5), vec3(0.5,3,8), 20); + + Object* house_side_wall_roof = Object_create(OBJ_WALL, CUBE_VERTICES_WARM, vec3(23.125,5,0), vec3(8.25,0.5,28.5), 20); + + World_addObject(&game->world, house_side_wall_back); + World_addObject(&game->world, house_side_wall_side_1); + World_addObject(&game->world, house_side_wall_side_2); + World_addObject(&game->world, house_side_wall_side_3); + World_addObject(&game->world, house_side_wall_side_4); + World_addObject(&game->world, house_side_wall_front); + World_addObject(&game->world, house_side_wall_front_1); + World_addObject(&game->world, house_side_wall_front_2); + World_addObject(&game->world, house_side_wall_front_3); + World_addObject(&game->world, house_side_wall_roof); + + + + + + Object* hof_bar_pillar_1 = Object_create(OBJ_CYLINDER_STATIC, CUBE_VERTICES_WARM, vec3(-10.2,0,-13.8), vec3(0.4f,3,0.4f), 100); + Object* hof_bar_pillar_2 = Object_create(OBJ_CYLINDER_STATIC, CUBE_VERTICES_WARM, vec3(-10.2,0, -5), vec3(0.4f,3,0.4f), 100); + Object* hof_bar_pillar_3 = Object_create(OBJ_CYLINDER_STATIC, CUBE_VERTICES_WARM, vec3(-16.2,0,-13.8), vec3(0.4f,3,0.4f), 100); + Object* hof_bar_pillar_4 = Object_create(OBJ_CYLINDER_STATIC, CUBE_VERTICES_WARM, vec3(-16.2,0, -5), vec3(0.4f,3,0.4f), 100); + World_addObject(&game->world, hof_bar_pillar_1); + World_addObject(&game->world, hof_bar_pillar_2); + World_addObject(&game->world, hof_bar_pillar_3); + World_addObject(&game->world, hof_bar_pillar_4); + Object* hof_bar_pillar_top_f = Object_create(OBJ_CYLINDER_STATIC, CUBE_VERTICES_WARM, vec3(-10.2,-1.4, -9.4), vec3(0.4f,8.8,0.4f), 100); + Object* hof_bar_pillar_top_b = Object_create(OBJ_CYLINDER_STATIC, CUBE_VERTICES_WARM, vec3(-16.2,-1.4, -9.4), vec3(0.4f,8.8,0.4f), 100); + Object* hof_bar_pillar_top_r = Object_create(OBJ_CYLINDER_STATIC, CUBE_VERTICES_WARM, vec3(-13.2,0, -13.8), vec3(0.4f,6.0,0.4f), 100); + Object* hof_bar_pillar_top_l = Object_create(OBJ_CYLINDER_STATIC, CUBE_VERTICES_WARM, vec3(-13.2,0, -5), vec3(0.4f,6.0,0.4f), 100); + hof_bar_pillar_top_f->rotation.x = DEG2RAD(90); + hof_bar_pillar_top_b->rotation.x = DEG2RAD(90); + hof_bar_pillar_top_r->rotation.x = DEG2RAD(90); + hof_bar_pillar_top_r->rotation.z = DEG2RAD(90); + hof_bar_pillar_top_l->rotation.x = DEG2RAD(90); + hof_bar_pillar_top_l->rotation.z = DEG2RAD(90); + World_addObject(&game->world, hof_bar_pillar_top_f); + World_addObject(&game->world, hof_bar_pillar_top_b); + World_addObject(&game->world, hof_bar_pillar_top_r); + World_addObject(&game->world, hof_bar_pillar_top_l); World_addHuman(&game->world, player); World_addHuman(&game->world, human); World_addHuman(&game->world, human_1); World_addObject(&game->world, ground); + World_addObject(&game->world, ground_1); World_addObject(&game->world, tree); - World_addObject(&game->world, cube); + //World_addObject(&game->world, cube_1); //World_addObject(&game->world, Object_create(OBJ_CUBE, CUBE_VERTICES_WARM, vec3(-0.2f,0,2.5f), vec3(1,1,1), 20)); //World_addObject(&game->world, axes); diff --git a/human.c b/human.c index a3d2398..8acd728 100644 --- a/human.c +++ b/human.c @@ -38,15 +38,20 @@ void Player_update(Human *player, Userinput *input, Camera *cam) { // Compute right vector from camera Vec3 right = vec3_normalize(vec3_cross(flatFront, cam->up)); - // Movement vector + // Base speed + float baseSpeed = 5.0f; + float sprintSpeed = 10.0f; + float moveSpeed = input->sprint ? sprintSpeed : baseSpeed; + + // Build movement vector Vec3 move = vec3_add( vec3_scale(flatFront, input->move_forward), vec3_scale(right, input->move_sideway) ); - // Move player + // Apply speed if (vec3_length(move) > 0.0f) { - move = vec3_scale(vec3_normalize(move), 5.0f); + move = vec3_scale(vec3_normalize(move), moveSpeed); player->object->movement = move; } else { player->object->movement = (Vec3){0,0,0}; diff --git a/input.c b/input.c index effa683..d34eddf 100644 --- a/input.c +++ b/input.c @@ -24,23 +24,25 @@ void Input_process(Userinput* userinput) { } const bool *keys = SDL_GetKeyboardState(NULL); + // Input: only directions 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; + + if (keys[SDL_SCANCODE_LSHIFT]) { + if (keys[SDL_SCANCODE_SPACE]) userinput->jump = 2; + } + + // sprint intent + userinput->sprint = keys[SDL_SCANCODE_LSHIFT]; } float dx, dy; diff --git a/input.h b/input.h index 2e65975..9cdcce0 100644 --- a/input.h +++ b/input.h @@ -10,6 +10,7 @@ typedef struct { float yaw_delta; float pitch_delta; char jump; + char sprint; } Userinput; void Input_reset(Userinput* input); diff --git a/mesh.c b/mesh.c index b3f4bfa..72b55c3 100644 --- a/mesh.c +++ b/mesh.c @@ -427,6 +427,150 @@ Mesh Mesh_createTree(float trunkHeight, float trunkRadius, int leafLayers, float return m; } +Mesh Mesh_createCylinder(float diameter, float height, int segments, VertexPreset preset) { + float radius = diameter*0.5f; + float hy = height * 0.5f; + + // vertex layout: pos(3), normal(3), color(3) = 9 floats + int side_vertices = segments * 2; // top + bottom ring + int cap_ring_vertices = segments * 2; // cap ring (top + bottom) + int cap_center_vertices = 2; // top + bottom centers + int vertex_count = side_vertices + cap_ring_vertices + cap_center_vertices; + + float *vertices = malloc(vertex_count * 9 * sizeof(float)); + + // color setup + float color[3] = {1,1,1}; + switch (preset) { + case CUBE_VERTICES_STANDARD: color[0]=color[1]=color[2]=1; break; + case CUBE_VERTICES_MODERN: color[0]=1; color[1]=0; color[2]=0; break; + case CUBE_VERTICES_GROUND: color[0]=0.55f; color[1]=0.27f; color[2]=0.07f; break; + case CUBE_VERTICES_WARM: color[0]=1; color[1]=0.5f; color[2]=0; break; + } + + // precompute sin/cos + float *cos_vals = malloc(segments * sizeof(float)); + float *sin_vals = malloc(segments * sizeof(float)); + for (int i=0; i<segments; i++) { + float theta = (2.0f * M_PI * i) / segments; + cos_vals[i] = cosf(theta); + sin_vals[i] = sinf(theta); + } + + int vi = 0; + + // side vertices + for (int i=0; i<segments; i++) { + float x = cos_vals[i] * radius; + float z = sin_vals[i] * radius; + + // bottom + vertices[vi++] = x; vertices[vi++] = -hy; vertices[vi++] = z; + vertices[vi++] = cos_vals[i]; vertices[vi++] = 0; vertices[vi++] = sin_vals[i]; + vertices[vi++] = color[0]; vertices[vi++] = color[1]; vertices[vi++] = color[2]; + + // top + vertices[vi++] = x; vertices[vi++] = hy; vertices[vi++] = z; + vertices[vi++] = cos_vals[i]; vertices[vi++] = 0; vertices[vi++] = sin_vals[i]; + vertices[vi++] = color[0]; vertices[vi++] = color[1]; vertices[vi++] = color[2]; + } + + // cap ring vertices + for (int i=0; i<segments; i++) { + float x = cos_vals[i] * radius; + float z = sin_vals[i] * radius; + + // bottom cap ring + vertices[vi++] = x; vertices[vi++] = -hy; vertices[vi++] = z; + vertices[vi++] = 0; vertices[vi++] = -1; vertices[vi++] = 0; + vertices[vi++] = color[0]; vertices[vi++] = color[1]; vertices[vi++] = color[2]; + + // top cap ring + vertices[vi++] = x; vertices[vi++] = hy; vertices[vi++] = z; + vertices[vi++] = 0; vertices[vi++] = 1; vertices[vi++] = 0; + vertices[vi++] = color[0]; vertices[vi++] = color[1]; vertices[vi++] = color[2]; + } + + // cap centers + // bottom center + vertices[vi++] = 0; vertices[vi++] = -hy; vertices[vi++] = 0; + vertices[vi++] = 0; vertices[vi++] = -1; vertices[vi++] = 0; + vertices[vi++] = color[0]; vertices[vi++] = color[1]; vertices[vi++] = color[2]; + + // top center + vertices[vi++] = 0; vertices[vi++] = hy; vertices[vi++] = 0; + vertices[vi++] = 0; vertices[vi++] = 1; vertices[vi++] = 0; + vertices[vi++] = color[0]; vertices[vi++] = color[1]; vertices[vi++] = color[2]; + + // indices + int side_indices = segments * 6; + int cap_indices = segments * 6; + int index_count = side_indices + cap_indices; + unsigned int *indices = malloc(index_count * sizeof(unsigned int)); + + int ii = 0; + // sides + for (int i=0; i<segments; i++) { + int i0 = i*2; + int i1 = i0 + 1; + int i2 = (i0 + 2) % (segments*2); + int i3 = (i0 + 3) % (segments*2); + + indices[ii++] = i0; indices[ii++] = i2; indices[ii++] = i1; + indices[ii++] = i1; indices[ii++] = i2; indices[ii++] = i3; + } + + int cap_base = side_vertices; + int center_bottom = side_vertices + cap_ring_vertices; + int center_top = center_bottom + 1; + + for (int i=0; i<segments; i++) { + int next = (i+1) % segments; + + // bottom cap + indices[ii++] = center_bottom; + indices[ii++] = cap_base + i*2; + indices[ii++] = cap_base + next*2; + + // top cap + indices[ii++] = center_top; + indices[ii++] = cap_base + next*2+1; + indices[ii++] = cap_base + i*2+1; + } + + Mesh m = mesh_default; + m.vertexCount = vertex_count; + m.indexCount = index_count; + m.hasIndices = true; + m.drawMode = GL_TRIANGLES; + + glGenVertexArrays(1,&m.vao); + glGenBuffers(1,&m.vbo); + glGenBuffers(1,&m.ebo); + + glBindVertexArray(m.vao); + glBindBuffer(GL_ARRAY_BUFFER,m.vbo); + glBufferData(GL_ARRAY_BUFFER,vertex_count*9*sizeof(float),vertices,GL_STATIC_DRAW); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,m.ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER,index_count*sizeof(unsigned int),indices,GL_STATIC_DRAW); + + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,9*sizeof(float),(void*)0); + glEnableVertexAttribArray(0); + glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,9*sizeof(float),(void*)(3*sizeof(float))); + glEnableVertexAttribArray(1); + glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,9*sizeof(float),(void*)(6*sizeof(float))); + glEnableVertexAttribArray(2); + glBindVertexArray(0); + + free(vertices); + free(indices); + free(cos_vals); + free(sin_vals); + + return m; +} + Mesh Mesh_createAxes(float length) { float vertices[] = { // X axis (red) @@ -576,3 +720,277 @@ Mesh Mesh_createHuman(VertexPreset preset, bool is_player) { glBindVertexArray(0); return m; } + + +Mesh Mesh_createHouse() { + float length = 18.0f; + float width = 12.0f; + float height = 6.0f; + float roofHeight = 5.0f; + + float lx = length / 2.0f; + float wy = height; + float wz = width / 2.0f; + float rh = roofHeight; + + // We’ll store positions, normals, colors + // Estimate: walls (24 verts), roof (12 verts), windows+doors (4 each) + float vertices[5000]; + unsigned int indices[5000]; + int vi = 0, ii = 0, vcount = 0; + + // Helper macro to add a vertex + #define ADDV(px,py,pz,nx,ny,nz,r,g,b) \ + do { \ + vertices[vi++] = (px); \ + vertices[vi++] = (py); \ + vertices[vi++] = (pz); \ + vertices[vi++] = (nx); \ + vertices[vi++] = (ny); \ + vertices[vi++] = (nz); \ + vertices[vi++] = (r); \ + vertices[vi++] = (g); \ + vertices[vi++] = (b); \ + } while(0) + + // Brick color + float br = 0.7f, bg = 0.3f, bb = 0.2f; + // Roof color + float rr = 0.3f, rg = 0.3f, rb = 0.3f; + // Window color + float wr = 0.4f, wg = 0.6f, wb = 0.9f; + // Door color + float dr = 0.4f, dg = 0.2f, db = 0.05f; + + // --- Walls (like cube, but long/short dimensions) + Vec3 faceNormals[6] = { + { 0, 0, -1}, // back + { 0, 0, 1}, // front + {-1, 0, 0}, // left + { 1, 0, 0}, // right + { 0, -1, 0}, // bottom + { 0, 1, 0} // top + }; + + Vec3 wallOffsets[6][4] = { + {{-lx,0,-wz},{lx,0,-wz},{lx,wy,-wz},{-lx,wy,-wz}}, // back + {{-lx,0,wz},{lx,0,wz},{lx,wy,wz},{-lx,wy,wz}}, // front + {{-lx,0,-wz},{-lx,wy,-wz},{-lx,wy,wz},{-lx,0,wz}}, // left + {{lx,0,-wz},{lx,wy,-wz},{lx,wy,wz},{lx,0,wz}}, // right + {{-lx,0,-wz},{lx,0,-wz},{lx,0,wz},{-lx,0,wz}}, // bottom + {{-lx,wy,-wz},{lx,wy,-wz},{lx,wy,wz},{-lx,wy,wz}}, // top + }; + + for (int f=0; f<6; f++) { + for (int v=0; v<4; v++) { + ADDV(wallOffsets[f][v].x, wallOffsets[f][v].y, wallOffsets[f][v].z, + faceNormals[f].x, faceNormals[f].y, faceNormals[f].z, + br,bg,bb); + } + indices[ii++] = vcount; indices[ii++] = vcount+1; indices[ii++] = vcount+2; + indices[ii++] = vcount+2; indices[ii++] = vcount+3; indices[ii++] = vcount; + vcount += 4; + } + + // --- Roof (triangular prism) + // ridge runs along x-axis (length), centered + // base at y=wy, height = rh + ADDV(-lx,wy,-wz, 0,1,0, rr,rg,rb); // left-back + ADDV(-lx,wy, wz, 0,1,0, rr,rg,rb); // left-front + ADDV(-lx,wy+rh, 0, 0,1,0, rr,rg,rb); // left-top + ADDV( lx,wy,-wz, 0,1,0, rr,rg,rb); // right-back + ADDV( lx,wy, wz, 0,1,0, rr,rg,rb); // right-front + ADDV( lx,wy+rh, 0, 0,1,0, rr,rg,rb); // right-top + + // Left triangle + indices[ii++] = vcount; indices[ii++] = vcount+1; indices[ii++] = vcount+2; + // Right triangle + indices[ii++] = vcount+3; indices[ii++] = vcount+4; indices[ii++] = vcount+5; + // Back roof quad + indices[ii++] = vcount; indices[ii++] = vcount+3; indices[ii++] = vcount+5; + indices[ii++] = vcount+5; indices[ii++] = vcount+2; indices[ii++] = vcount; + // Front roof quad + indices[ii++] = vcount+1; indices[ii++] = vcount+4; indices[ii++] = vcount+5; + indices[ii++] = vcount+5; indices[ii++] = vcount+2; indices[ii++] = vcount+1; + vcount += 6; + + // --- Windows (3 along front wall at z=+wz) + float winSize = 1.5f; + float winY = 3.0f; + for (int i=0;i<3;i++) { + float cx = -lx + (i+1)*length/4.0f; // evenly spaced + float cy = winY; + float cz = wz+0.01f; + ADDV(cx-winSize,cy-winSize,cz, 0,0,1, wr,wg,wb); + ADDV(cx+winSize,cy-winSize,cz, 0,0,1, wr,wg,wb); + ADDV(cx+winSize,cy+winSize,cz, 0,0,1, wr,wg,wb); + ADDV(cx-winSize,cy+winSize,cz, 0,0,1, wr,wg,wb); + indices[ii++] = vcount; indices[ii++] = vcount+1; indices[ii++] = vcount+2; + indices[ii++] = vcount+2; indices[ii++] = vcount+3; indices[ii++] = vcount; + vcount += 4; + } + + // --- Doors (front and back) + float dw = 1.0f, dh = 2.0f; + // Front door + ADDV(-lx+1,0,wz+0.01f, 0,0,1, dr,dg,db); + ADDV(-lx+1+dw,0,wz+0.01f, 0,0,1, dr,dg,db); + ADDV(-lx+1+dw,dh,wz+0.01f, 0,0,1, dr,dg,db); + ADDV(-lx+1,dh,wz+0.01f, 0,0,1, dr,dg,db); + indices[ii++] = vcount; indices[ii++] = vcount+1; indices[ii++] = vcount+2; + indices[ii++] = vcount+2; indices[ii++] = vcount+3; indices[ii++] = vcount; + vcount += 4; + // Back door + ADDV(lx-1-dw,0,-wz-0.01f, 0,0,-1, dr,dg,db); + ADDV(lx-1,0,-wz-0.01f, 0,0,-1, dr,dg,db); + ADDV(lx-1,dh,-wz-0.01f, 0,0,-1, dr,dg,db); + ADDV(lx-1-dw,dh,-wz-0.01f, 0,0,-1, dr,dg,db); + indices[ii++] = vcount; indices[ii++] = vcount+1; indices[ii++] = vcount+2; + indices[ii++] = vcount+2; indices[ii++] = vcount+3; indices[ii++] = vcount; + vcount += 4; + + // Upload to GPU + Mesh m = mesh_default; + m.vertexCount = vcount; + m.indexCount = ii; + m.hasIndices = true; + m.drawMode = GL_TRIANGLES; + + glGenVertexArrays(1,&m.vao); + glGenBuffers(1,&m.vbo); + glGenBuffers(1,&m.ebo); + + glBindVertexArray(m.vao); + glBindBuffer(GL_ARRAY_BUFFER,m.vbo); + glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,m.ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW); + + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,9*sizeof(float),(void*)0); + glEnableVertexAttribArray(0); + glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,9*sizeof(float),(void*)(3*sizeof(float))); + glEnableVertexAttribArray(1); + glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,9*sizeof(float),(void*)(6*sizeof(float))); + glEnableVertexAttribArray(2); + glBindVertexArray(0); + + return m; +} + +Mesh Mesh_createBarn() { + float length = 24.0f; + float width = 12.0f; + float height = 6.0f; + float roofHeight = 5.0f; + + float lx = length / 2.0f; + float wy = height; + float wz = width / 2.0f; + float rh = roofHeight; + + // We’ll store positions, normals, colors + // Estimate: walls (24 verts), roof (12 verts), windows+doors (4 each) + float vertices[5000]; + unsigned int indices[5000]; + int vi = 0, ii = 0, vcount = 0; + + // Helper macro to add a vertex + #define ADDV(px,py,pz,nx,ny,nz,r,g,b) \ + do { \ + vertices[vi++] = (px); \ + vertices[vi++] = (py); \ + vertices[vi++] = (pz); \ + vertices[vi++] = (nx); \ + vertices[vi++] = (ny); \ + vertices[vi++] = (nz); \ + vertices[vi++] = (r); \ + vertices[vi++] = (g); \ + vertices[vi++] = (b); \ + } while(0) + + // Brick color + float br = 0.7f, bg = 0.3f, bb = 0.2f; + // Roof color + float rr = 0.3f, rg = 0.3f, rb = 0.3f; + + // --- Walls (like cube, but long/short dimensions) + Vec3 faceNormals[6] = { + { 0, 0, -1}, // back + { 0, 0, 1}, // front + {-1, 0, 0}, // left + { 1, 0, 0}, // right + { 0, -1, 0}, // bottom + { 0, 1, 0} // top + }; + + Vec3 wallOffsets[6][4] = { + {{-lx,0,-wz},{lx,0,-wz},{lx,wy,-wz},{-lx,wy,-wz}}, // back + {{-lx,0,wz},{lx,0,wz},{lx,wy,wz},{-lx,wy,wz}}, // front + {{-lx,0,-wz},{-lx,wy,-wz},{-lx,wy,wz},{-lx,0,wz}}, // left + {{lx,0,-wz},{lx,wy,-wz},{lx,wy,wz},{lx,0,wz}}, // right + {{-lx,0,-wz},{lx,0,-wz},{lx,0,wz},{-lx,0,wz}}, // bottom + {{-lx,wy,-wz},{lx,wy,-wz},{lx,wy,wz},{-lx,wy,wz}}, // top + }; + + for (int f=0; f<6; f++) { + for (int v=0; v<4; v++) { + ADDV(wallOffsets[f][v].x, wallOffsets[f][v].y, wallOffsets[f][v].z, + faceNormals[f].x, faceNormals[f].y, faceNormals[f].z, + br,bg,bb); + } + indices[ii++] = vcount; indices[ii++] = vcount+1; indices[ii++] = vcount+2; + indices[ii++] = vcount+2; indices[ii++] = vcount+3; indices[ii++] = vcount; + vcount += 4; + } + + // --- Roof (triangular prism) + // ridge runs along x-axis (length), centered + // base at y=wy, height = rh + ADDV(-lx,wy,-wz, 0,1,0, rr,rg,rb); // left-back + ADDV(-lx,wy, wz, 0,1,0, rr,rg,rb); // left-front + ADDV(-lx,wy+rh, 0, 0,1,0, rr,rg,rb); // left-top + ADDV( lx,wy,-wz, 0,1,0, rr,rg,rb); // right-back + ADDV( lx,wy, wz, 0,1,0, rr,rg,rb); // right-front + ADDV( lx,wy+rh, 0, 0,1,0, rr,rg,rb); // right-top + + // Left triangle + indices[ii++] = vcount; indices[ii++] = vcount+1; indices[ii++] = vcount+2; + // Right triangle + indices[ii++] = vcount+3; indices[ii++] = vcount+4; indices[ii++] = vcount+5; + // Back roof quad + indices[ii++] = vcount; indices[ii++] = vcount+3; indices[ii++] = vcount+5; + indices[ii++] = vcount+5; indices[ii++] = vcount+2; indices[ii++] = vcount; + // Front roof quad + indices[ii++] = vcount+1; indices[ii++] = vcount+4; indices[ii++] = vcount+5; + indices[ii++] = vcount+5; indices[ii++] = vcount+2; indices[ii++] = vcount+1; + vcount += 6; + + // Upload to GPU + Mesh m = mesh_default; + m.vertexCount = vcount; + m.indexCount = ii; + m.hasIndices = true; + m.drawMode = GL_TRIANGLES; + + glGenVertexArrays(1,&m.vao); + glGenBuffers(1,&m.vbo); + glGenBuffers(1,&m.ebo); + + glBindVertexArray(m.vao); + glBindBuffer(GL_ARRAY_BUFFER,m.vbo); + glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,m.ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW); + + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,9*sizeof(float),(void*)0); + glEnableVertexAttribArray(0); + glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,9*sizeof(float),(void*)(3*sizeof(float))); + glEnableVertexAttribArray(1); + glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,9*sizeof(float),(void*)(6*sizeof(float))); + glEnableVertexAttribArray(2); + glBindVertexArray(0); + + return m; +} diff --git a/mesh.h b/mesh.h index 0628123..a2d9501 100644 --- a/mesh.h +++ b/mesh.h @@ -40,8 +40,11 @@ Mesh Mesh_createTriangle(); Mesh Mesh_createPyramid(float size, float height); //Mesh Mesh_createCube(float size, float height); Mesh Mesh_createCube(Vec3 size, VertexPreset preset); +Mesh Mesh_createCylinder(float diameter, float height, int segments, 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); +Mesh Mesh_createHouse(); +Mesh Mesh_createBarn(); #endif diff --git a/object.c b/object.c index 644e498..d1f5ab1 100644 --- a/object.c +++ b/object.c @@ -46,7 +46,21 @@ Object *Object_create(ObjectType type, VertexPreset vertex, Vec3 position, Vec3 obj->collider.size = size; obj->collider.isStatic = false; break; - + case OBJ_CYLINDER: + obj->mesh = Mesh_createCylinder(size.x, size.y, 30, vertex); + obj->collider.size = size; + obj->collider.isStatic = false; + break; + case OBJ_CYLINDER_STATIC: + obj->mesh = Mesh_createCylinder(size.x, size.y, 30, vertex); + obj->collider.size = size; + obj->collider.isStatic = true; + break; + case OBJ_WALL: + obj->mesh = Mesh_createCube(size, vertex); + obj->collider.size = size; + obj->collider.isStatic = true; + break; case OBJ_HUMAN: obj->mesh = Mesh_createHuman(vertex, false); obj->collider.size = size; @@ -63,10 +77,29 @@ Object *Object_create(ObjectType type, VertexPreset vertex, Vec3 position, Vec3 obj->collider.isStatic = true; break; case OBJ_TREE: - obj->mesh = Mesh_createTree(3.0f, 0.3f, 8, 1.5f);; + obj->mesh = Mesh_createTree(3.0f, 0.3f, 8, 1.5f); obj->collider.size = size; obj->collider.isStatic = true; - break; + obj->position_center = (Vec3){0, obj->collider.size.y/2.0f, 0}; + obj->position.y = -size.y/2.0f; + obj->collider.position = obj->position_center; + return obj; + case OBJ_HOUSE_MAIN: + obj->mesh = Mesh_createHouse(); + obj->collider.size = size; + obj->collider.isStatic = true; + obj->position_center = (Vec3){0, obj->collider.size.y/2.0f, 0}; + obj->position.y = -size.y/2.0f; + obj->collider.position = obj->position_center; + return obj; + case OBJ_BARN: + obj->mesh = Mesh_createBarn(); + obj->collider.size = size; + obj->collider.isStatic = true; + obj->position_center = (Vec3){0, obj->collider.size.y/2.0f, 0}; + obj->position.y = -size.y/2.0f; + obj->collider.position = obj->position_center; + return obj; default: @@ -127,25 +160,19 @@ void Object_render(Object *obj, Renderer *renderer, Camera *camera, Time* time) -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)); +Vec3 colliderCenterWorld(Object *o) { + return vec3_add(o->position, o->collider.position); } -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; +bool AABB_overlap(Object* o_a, Object* o_b) { + Vec3 n_a = colliderCenterWorld(o_a); + Vec3 n_b = colliderCenterWorld(o_b); + Vec3 a = o_a->collider.size; + Vec3 b = o_b->collider.size; + + return (fabs(n_a.x - n_b.x) * 2 < (a.x + b.x)) && + (fabs(n_a.y - n_b.y) * 2 < (a.y + b.y)) && + (fabs(n_a.z - n_b.z) * 2 < (a.z + b.z)); } @@ -155,25 +182,68 @@ void Object_applyGravity(Object *obj, float dt) { obj->velocity.y -= 9.81f * dt; } +void Collider_positionGround(Object* obj, Object* ground) { + float ground_top = ground->position.y + ground->collider.size.y; + + float obj_bottom = obj->position.y; + + // maximum step height: 1/3 of object height + float max_step = 100000; + if (obj->type == OBJ_HUMAN || obj->type == OBJ_HUMAN_PLAYER) + max_step = obj->collider.size.y / 3.0f; + + // distance from feet to ground top + float height_diff = ground_top - obj_bottom; + + if (height_diff > 0 && height_diff <= max_step) { + // within step height → snap onto surface + obj->position.y += height_diff; + obj->velocity.y = 0; + obj->is_grounded = true; + } else if (height_diff <= 0) { + // object is already intersecting / below → push out + obj->position.y += height_diff; + obj->velocity.y = 0; + obj->is_grounded = true; + } else { + // too high → treat as wall, don't snap + obj->is_grounded = false; + } +} +void Collider_positionGround_(Object* obj, Object* ground) { + Vec3 gpos = colliderCenterWorld(ground); + float target_y = gpos.y + ground->collider.size.y / 2.0f; + Vec3 opos = colliderCenterWorld(obj); + float bottom_y = opos.y - obj->collider.size.y / 2.0f; + if (bottom_y < target_y) { float correction = target_y - bottom_y; + obj->position.y += correction; // shift object upward + obj->velocity.y = 0; + } obj->is_grounded = true; +} + void Object_resolveDynamicCollision(Object *obj, Object *other) { - if (obj->collider.isStatic || other->collider.isStatic) return; if (!AABB_overlap(obj, other)) return; + Vec3 posA = colliderCenterWorld(obj); + Vec3 posB = colliderCenterWorld(other); Vec3 delta = vec3_sub(obj->position, other->position); - float verticalOverlap = (obj->collider.size.y + other->collider.size.y)/2 - fabs(delta.y); - if (delta.y > 0 && verticalOverlap > 0.0f) { - Collider_positionGround(obj, other); - obj->velocity.x = 0; - obj->velocity.z = 0; - return; - } delta.y = 0.0f; if (vec3_length(delta) == 0) { delta.x = 0.01f; delta.z = 0.01f; } - Vec3 normal = vec3_normalize(delta); + + // 3. Slight separation + float overlapX = (obj->collider.size.x + other->collider.size.x) / 2 - fabs(posA.x - posB.x); + float overlapZ = (obj->collider.size.z + other->collider.size.z) / 2 - fabs(posA.z - posB.z); + + Vec3 normal = {0,0,0}; + if (overlapX < overlapZ) { + normal.x = (posA.x > posB.x) ? 1.0f : -1.0f; // push left or right + } else { + normal.z = (posA.z > posB.z) ? 1.0f : -1.0f; // push forward or back + } // 2. Weight-based impulse float totalWeight = obj->weight + other->weight; @@ -182,24 +252,10 @@ void Object_resolveDynamicCollision(Object *obj, Object *other) { Vec3 impulseObj = vec3_scale(normal, other->weight / totalWeight * pushStrength); Vec3 impulseOther = vec3_scale(normal, obj->weight / totalWeight * pushStrength); - obj->velocity = vec3_add(obj->velocity, impulseObj); - other->velocity = vec3_add(other->velocity, vec3_scale(impulseOther, -1.0f)); - - // 3. Slight separation - 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); - } // 4. Compute offset from obj center to other center in XZ plane - Vec3 offset = vec3_sub(other->position, obj->position); + Vec3 offset = vec3_sub(posB, posA); offset.y = 0.0f; // 5. Transform offset into obj's local space using rotation.y (yaw) @@ -215,15 +271,44 @@ void Object_resolveDynamicCollision(Object *obj, Object *other) { normalizedTorque = 0.0f; // Print left/right + /* if (normalizedTorque > 0.0f) printf("Object collided on the right side\n"); else if (normalizedTorque < 0.0f) printf("Object collided on the left side\n"); + */ // 7. Apply angular velocity float angularFactor = 1.0f; - obj->angular_velocity.y += normalizedTorque * vec3_length(impulseObj) / obj->weight * angularFactor; - other->angular_velocity.y += normalizedTorque * vec3_length(impulseOther) / other->weight * angularFactor; + + if (!obj->collider.isStatic) { + obj->velocity = vec3_add(obj->velocity, impulseObj); + if (overlapX > 0) + obj->position.x += normal.x * (overlapX * 0.5f); + if (overlapZ > 0) + obj->position.z += normal.z * (overlapZ * 0.5f); + obj->angular_velocity.y += normalizedTorque * vec3_length(impulseObj) / obj->weight * angularFactor; + } + if (!other->collider.isStatic) { + other->velocity = vec3_add(other->velocity, vec3_scale(impulseOther, -1.0f)); + if (overlapX > 0) + other->position.x -= normal.x * (overlapX * 0.5f); + if (overlapZ > 0) + other->position.z -= normal.z * (overlapZ * 0.5f); + other->angular_velocity.y += normalizedTorque * vec3_length(impulseOther) / other->weight * angularFactor; + } +} + +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 (AABB_overlap(obj, other)) { + Collider_positionGround(obj, other); + } + Object_resolveDynamicCollision(obj, other); + } } void Object_applyFriction(Object *obj, float dt) { @@ -244,21 +329,6 @@ void Object_applyFriction(Object *obj, float dt) { 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; diff --git a/object.h b/object.h index bfad1bc..14db597 100644 --- a/object.h +++ b/object.h @@ -17,12 +17,16 @@ typedef enum { OBJ_PYRAMID, OBJ_AXES, OBJ_CUBE, + OBJ_CYLINDER, + OBJ_CYLINDER_STATIC, OBJ_SPHERE, OBJ_HUMAN, OBJ_HUMAN_PLAYER, OBJ_GROUND, OBJ_TREE, - // later: cylinder, plane, etc. + OBJ_HOUSE_MAIN, + OBJ_BARN, + OBJ_WALL, } ObjectType; typedef struct {