Giter VIP home page Giter VIP logo

cub3d's Introduction

cub3D

A “realistic” 3D graphical representation of the inside of a maze from a first-person perspective using the Ray-Casting principles.

Built in miniLibX, inspired on Wolfenstein 3D which is the ancestor of games like Doom (Id Software, 1993), Doom II (Id Software, 1994), Duke Nukem 3D (3D Realm, 1996) and Quake (Id Software, 1996), that are additional eternal milestones in the world of video games.

How to build cub3D ? 🏗️

Note

Requirements

  • Debian or Ubuntu base distro
  • gcc
  • make
  • X11 include files (package xorg)
  • XShm extension must be present (package libxext-dev)
  • Utility functions from BSD systems - development files (package libbsd-dev)
Setup the miniLibX

Install required packages:

sudo apt-get install gcc make xorg libxext-dev libbsd-dev

After running make, the miniLibX will have generated some files that you must install:

  • libmlx.a and/or libmlx_$(HOSTTYPE).a in /usr/X11/lib or /usr/local/lib
  • mlx.h in /usr/X11/include or /usr/local/include
  • man/man3/mlx*.1 in /usr/X11/man/man3 or /usr/local/man/man3
Compile cub3DJust run make and let the magic happen 🪄

make

How to run cub3D ? 🕹️

Configuration files

There are 3 types of elements to be configured inside a configuration file:

Where the map element must always be the last element declared on the configuration file.

The wall textures and the floor and ceiling colors configuration:

  • Can be set in any order.
  • Can be separated by one or more empty lines.
  • Can have it's second argument separated by one or more spaces.

Here's an example of a minimalist configuration file.

sample.cub:

NO ./path_to_the_north_texture
SO ./path_to_the_south_texture
WE ./path_to_the_west_texture
EA ./path_to_the_east_texture

F 220,100,0
C 225,30,0

        1111111111111111111111111
        1000000000110000000000001
        1011000001110000000000001
        1001000000000000000000001
111111111011000001110000000000001
100000000011000001110111111111111
11110111111111011100000010001
11110111111111011101010010001
11000000110101011100000010001
10000000000000001100000010001
10000000000000001101010010001
11000001110101011111011110N0111
11110111 1110101 101111010001
11111111 1111111 111111111111
Wall textures 🧱

Wall textures vary depending on which side the wall is facing (North, South, East, West).

Wall syntax

The texture elements must be declared in a specific order inside the configuration files.

<id> <./path_to_the_texture>

There are only 4 possible identifiers in the lexic of the texture elements:

ID Description
NO North texture.
SO South texture.
WE West texture.
EA East texture.

Here is a valid example:

WE ./path_to_the_west_texture
Floor and ceiling colors 🎨

You can set the floor and ceiling colors inside the configuration file.

Floor and ceiling syntax

The texture elements must be declared in a specific order inside the configuration files.

<id> <[0,255],[0,255],[0,255]>

There are only 2 possible identifiers in the lexic of the floor and ceiling elements:

ID Description
F Floor.
C Ceiling.

Here is a valid example:

C 225,30,0
Maps 🗺️

The program cub3D needs a map (with the .cub extension) as the first argument to generate the 3D world. You can choose, create or modify an existent map.

Map syntax

There are only 6 possible characters in the lexic of the map:

Char Description
0 Empty space.
1 Wall.
N Player’s start position facing North.
S Player’s start position facing South.
E Player’s start position facing East.
W Player’s start position facing West.
  • The map must be closed/surrounded by walls, if not, the program returns an error.
  • The map must be parsed as it looks in the file.
  • Spaces are a valid part of the map. Here is a valid example of a simple map:
111111
100101
101001
1100N1
 11111

Controls 🎮

⎋ Esc Close the window and quit the program cleanly. M Toggle mini map.

Camera

Look left inside the maze.
Look right inside the maze.

Movement

W Move up the point of view through the maze.
A Move left the point of view through the maze.
S Move down the point of view through the maze.
D Move right the point of view through the maze.

Raycasting 🌄

Raycasting is a rendering technique used in computer graphics to simulate the path of light rays from a virtual camera into a scene.

RUSS

Requirements

The first step in Raycasting to cast one ray to determine the distance between the player and a wall.

We must have some information first:

  • Starting position

  • Direction's unit vector

// Ray starting position coordinates (player's position)
p->ray.xy[0] = p->mx;
p->ray.xy[1] = p->my;
// Unit vector of direction from the angle in radians.
p->ray.dir[0] = cos(angle);
p->ray.dir[1] = -sin(angle);

Intersection of cells

We also have to calculate the ray unit step size on the player's direction's unit vector.

To know the lenght of each cell intersection happening on the X axis and on the Y axis, through the ray.

DeltaDist is our Ray Unit Step Size.

Note

This value is a percentage, taking a cell as a unit. It can be adapted as needed.

Triangle

Let's say that the side AB = x and AC = y, then we have the hypotenuse step sizes as follow:

$$S_{x} = \sqrt{1^{2}+(\frac{y}{x})^{2}}$$ $$S_{y} = \sqrt{(\frac{x}{y})^{2}+1^{2}}$$

We can implement this functions in C like this:

// dir = {x, y} player direction's unit vector coordinates;
p->ray.russ[0] = sqrt(1 + pow(p->ray.dir[1], 2) / pow(p->ray.dir[0], 2));
p->ray.russ[1] = sqrt(1 + pow(p->ray.dir[0], 2) / pow(p->ray.dir[1], 2));

cub3d's People

Contributors

3lsy avatar andre1yan avatar

Watchers

 avatar

cub3d's Issues

Decide how to handle spaces in a map

The map must be parsed as it looks in the file. Spaces are a valid part of the
map and are up to you to handle. You must be able to parse any kind of map,
as long as it respects the rules of the map.

Leaks and invalid read in parsing

Command: ./cub3d maps/valid/big/mario_world.cub

==62718== Invalid read of size 1
==62718== at 0x113AED: trim_size (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x113974: ft_strtrim (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10FEEC: analyze_line (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10FC5D: parse_config (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10FBD2: load_config (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10F5F7: main (in /home/echavez-/42/cub3d/cub3d)
==62718== Address 0x4d66c8f is 1 bytes before a block of size 1 alloc'd
==62718== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==62718== by 0x114A7D: ft_fstrdup (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x1145BF: ft_get_next_line (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10FC64: parse_config (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10FBD2: load_config (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10F5F7: main (in /home/echavez-/42/cub3d/cub3d)

==62718== HEAP SUMMARY:
==62718== in use at exit: 1,290 bytes in 50 blocks
==62718== total heap usage: 1,974 allocs, 1,924 frees, 4,441,452 bytes allocated
==62718==
==62718== 30 bytes in 1 blocks are definitely lost in loss record 1 of 4
==62718== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==62718== by 0x114A7D: ft_fstrdup (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x114612: ft_get_next_line (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10FC64: parse_config (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10FBD2: load_config (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10F5F7: main (in /home/echavez-/42/cub3d/cub3d)
==62718==
==62718== 40 bytes in 1 blocks are definitely lost in loss record 2 of 4
==62718== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==62718== by 0x114A7D: ft_fstrdup (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x1145BF: ft_get_next_line (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10FC42: parse_config (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10FBD2: load_config (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10F5F7: main (in /home/echavez-/42/cub3d/cub3d)
==62718==
==62718== 593 bytes in 25 blocks are definitely lost in loss record 3 of 4
==62718== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==62718== by 0x114A7D: ft_fstrdup (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x1145BF: ft_get_next_line (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10FC64: parse_config (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10FBD2: load_config (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10F5F7: main (in /home/echavez-/42/cub3d/cub3d)
==62718==
==62718== 627 bytes in 23 blocks are definitely lost in loss record 4 of 4
==62718== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==62718== by 0x113984: ft_strtrim (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10FEEC: analyze_line (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10FC5D: parse_config (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10FBD2: load_config (in /home/echavez-/42/cub3d/cub3d)
==62718== by 0x10F5F7: main (in /home/echavez-/42/cub3d/cub3d)
==62718==
==62718== LEAK SUMMARY:
==62718== definitely lost: 1,290 bytes in 50 blocks
==62718== indirectly lost: 0 bytes in 0 blocks
==62718== possibly lost: 0 bytes in 0 blocks
==62718== still reachable: 0 bytes in 0 blocks
==62718== suppressed: 0 bytes in 0 blocks
==62718==
==62718== For lists of detected and suppressed errors, rerun with: -s
==62718== ERROR SUMMARY: 8 errors from 5 contexts (suppressed: 1 from 1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.