{"id":25543883,"url":"https://github.com/mathyscogne/42_cub3d","last_synced_at":"2025-10-08T12:12:22.176Z","repository":{"id":274879604,"uuid":"917265196","full_name":"MathysCogne/42_Cub3D","owner":"MathysCogne","description":"Cub3D is a project inspired by Wolfenstein 3D, one of the first first-person shooter games. It allows you to explore ray-casting techniques to create a 3D maze representation. The goal is to navigate through this environment while respecting the constraints of the graphic engine.","archived":false,"fork":false,"pushed_at":"2025-02-03T16:40:59.000Z","size":75171,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-15T20:06:47.860Z","etag":null,"topics":["3d-game","42","cub3d","minilibx","raycasting"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MathysCogne.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-01-15T17:02:23.000Z","updated_at":"2025-02-18T11:54:55.000Z","dependencies_parsed_at":"2025-05-23T07:24:53.511Z","dependency_job_id":null,"html_url":"https://github.com/MathysCogne/42_Cub3D","commit_stats":null,"previous_names":["mathyscogne/42_cub3d"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/MathysCogne/42_Cub3D","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MathysCogne%2F42_Cub3D","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MathysCogne%2F42_Cub3D/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MathysCogne%2F42_Cub3D/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MathysCogne%2F42_Cub3D/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MathysCogne","download_url":"https://codeload.github.com/MathysCogne/42_Cub3D/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MathysCogne%2F42_Cub3D/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278940576,"owners_count":26072547,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["3d-game","42","cub3d","minilibx","raycasting"],"created_at":"2025-02-20T07:38:40.001Z","updated_at":"2025-10-08T12:12:22.154Z","avatar_url":"https://github.com/MathysCogne.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n\u003cstrong\u003eCub3D 🎮\u003c/strong\u003e\n\n\u003cp\u003eCub3D is a project inspired by Wolfenstein 3D, one of the first first-person shooter games. It allows you to explore ray-casting techniques to create a 3D maze representation. The goal is to navigate through this environment while respecting the constraints of the graphic engine.\u003c/p\u003e\n\n\u003c/br\u003e\n\n\u003cimg src=\"subject/output_compressed.gif\" alt=\"GIF Cub3d\" width=\"650\" /\u003e\n\n\u003cp\u003e\u003ca href=\"https://github.com/MathysCogne/42_Cub3D/blob/main/subject/fr.subject.pdf\"\u003e\u003cstrong\u003eSubject\u003c/strong\u003e\u003c/a\u003e\u003c/p\u003e\n\n\u003c/div\u003e\n\n## Features\n\n- 3D rendering using ray-casting\n\n- Custom textures and configurable map\n\n- Player movement and interactions\n\n- Minimap display (bonus)\n\n- Animated sprites (bonus)\n\n- Door system (bonus)\n\n- 360-degree full viewpoint management  (bonus)\n\n- Mouse interaction for precise aiming and camera control (bonus)\n\n- Weapon, and stats player (Heath, stamina..) (bonus)\n\n\u003c/br\u003e\n\n\n## Controls\n\n| Key     | Description       |\n|:--------|:-------------------\n| ESC        | Quit the game  |\n| W/A/S/D     | Move         |\n| ⬆️ / ⬇️     | Tilt the camera vertically      |\n| ⬅️ / ➡️      | Rotate the camera horizontally      |\n| Mouse movement     | Adjust camera viewpoint     |\n| Left Mouse Click     | Shoot weapon     |\n| Right Mouse Click     | Interaction weapon     |\n\n\n\u003c/br\u003e\n\n## Map  `(.cub format)`\n\n```\nNO ./assets/texture/NO.xpm\nSO ./assets/texture/SO.xpm\nWE ./assets/texture/WE.xpm\nEA ./assets/texture/EA.xpm\n\nF ./assets/texture/BOT.xpm\nC ./assets/texture/TOP.xpm\n\n11111111111111111111111111\n1E000000020000000000900001\n19111111191111111110111191\n10000000001000000010001001\n11121110111011119111001021\n10001000000010000000201001\n10191101110101111191111221\n10002001000100000000200001\n11111101111101111111111111\n11111111111191111111111111\n     10000000000001\n     10000200020001\n     10000003000001\n     10000000000001\n11111111111111111111111111\n```\n\n\n## Compilation and Usage\n\n\u003c/br\u003e\n\n```bash\n# Compile:\nmake\n\n# Start game:\n./cub3d [path_map]\n```\n\n\u003c/br\u003e\n\n\n⊹ ࣪ ﹏𓊝﹏𓂁﹏⊹ ࣪ ˖\n\n\u003c/br\u003e\n\n## RayCasting\nThis guide explains the logic behind ray casting for rendering a 3D-like environment using a 2D map.\u003c/br\u003e\n\n- 1/ Rays \u0026 Field of View (FOV)\n\nIf the player has a 90° field of view (FOV) and is looking straight north (90°, depending on your implementation), then the rays will cover an angular range from 90° - 45° to 90° + 45°.\n\nIf you cast 90 rays, each one corresponds to a specific angle in this range:\nAngles: 45°, 46°, 47°, ..., 134°, 135°\n\nEach ray is traced independently to detect walls and compute a color.\n- 2/ Calculating Ray Vectors\n\nTo compute the movement vector for each ray, use trigonometry:\n\n    X displacement:\n    dx=cos(angle_h​)\n    Y displacement:\n    dy=sin(angle_h​)\n\nwith angle_h the angle of the ray.\n\nFor example, if the player is looking straight north (90°), then, for the mid ray:\n\n    dx=cos⁡(90°)=0\n    dy=sin⁡(90°)=1\n\nThis means the ray moves vertically upwards on the 2D grid (y -= 1, x += 0).  \n\nProportionality Calculations:\n\nTo find the movement needed to reach the next X or Y intersection, use the cross-multiplication rule:  \nCase 1: Moving x_len on the X-axis, how much do we move in Y?  \ny_len = sin(angle_h) * x_len / cos(angle_h)\nor more classic cross factor view :  \ncos(angle_h) | x_len  \nsin(angle_h) | y_len  \n\n\nCase 2: Moving y_len on the Y-axis, how much do we move in X?  \nx_len = cos(angle_h) * y_len / sin(angle_h)\nor more classic cross factor view :  \ncos(angle_h) | x_len  \nsin(angle_h) | y_len  \n\nThese formulas allow us to step through the grid along the ray's direction.\n- 3/ Wall Detection (Grid Intersection Check)\n\n\u003cimg src=\"subject/raycast_illustration.png\" alt=\"GIF Cub3d\" width=\"650\" /\u003e\n\nRemember, we are looking for a wall (1 on our map).  \nWe start at the player's position, here x(4.5), y(3.5).  \nYou can see the ray angle (something like 30°, though it's not important).  \nThe first two intersections with rounded x and y values are shown in green.  \nThe y-axis intersection is at x(4.0).  \nThe x-axis intersection is at y(3.0).  \n\nWhich one is the closest?  \n```\ndist_x = (4.0 - 4.5) / cos(angle_h)  \ndist_y = (3 - 3.5) / sin(angle_h)  \n```\n\nCompare dist_x and dist_y. Choose the smaller value as the next ray step.  \nif it is dist_x, cross factor the y value.  \nif it is dist_y, cross factor the x value.  \n\nThen, verify if a wall is hit: \n```\ncase1 if the rounded value is x:   \ngrid[(int)y][(int)x−1]==1  \ncase2 if the rounded value is y:   \ngrid[(int)y - 1][(int)x]==1  \n```\n\nRepeat until this condition is met. I demonstrated the algo for the step1, i'm sure you can expend it yourself for each step  \n\nAs a result, we obtain the pixel offset of the image as a percentage. A value x (between 0 and 1) corresponding to the img column offset.  \nOn the map, the offset is something like 0.50. If your img is 200\\*200, so it is the column 100 that you will have to render (200\\*0.5).\n\n- 5/ You still can't understand... look at the code\n\nSo far, you can only get a column offset, you can't get a single one pixel. You have 2 choices. The first one is to create a function to render the column. The other one is to continue on this logic and introduce a z axis. This is our choice, computing each pixel using raycasting.  \nSo, this demonstration is a simplified version of the algo, to make it work, you should introduce the z axis (height). We want to keep this text simple. Please look at the code to understand how to introduce the z axis.  \nHelper: z vector correction: sin(angle_v);\n\n- 6/ I'm still lost\n\nYou should use:  \ncos, sin, ceil, floor, round, pi. You should understand that cos and sin create a vector (https://www.mathsisfun.com/algebra/trig-interactive-unit-circle.html), you should do your (very simple) math. Even if it is very simple. It took me 4 days. Don't panic.  \nCode entry for raycasting : /src/exec/ray_casting/ray_casting.c\n\n## Disclaimer\n\u003e At 42 School, most projects must comply with the [Norm](https://github.com/42School/norminette/blob/master/pdf/en.norm.pdf).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathyscogne%2F42_cub3d","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmathyscogne%2F42_cub3d","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathyscogne%2F42_cub3d/lists"}