Giter Site home page Giter Site logo

cub3d's Introduction

cub3d

The problematic was : re-create a Wolfenstein-3D like, using the MLX library. Only C was allowed.

Install

git clone [email protected]:Bima42/cub3d.git
make
./cub3d map.cub

Note : Use W, A, S, D key to move. Use left and right arrow to rotate camera. Esc quit the game.

Resume

At the beginning, we decided to treat parsing part before talk about raycasting etc Then, we split works in two parts : initialisation part (graphics, mlx ...) and raycasting part

Parsing

There is some important point to note :

  • Map file is .cub only
  • This map file is strict :
    • Begin by texture path (N, S, E, W)
    • Then some rgb data for ceiling and floor
    • Empty lines can be found between those datas
    • Map architecture
  • Map :
    • Must be surrounded by '1'
    • Only one player spawn (N, S, E or W indicate player orientation)

Strategy use for map checking : for every rows and colums (x and y)

  • Start with skip white spaces
  • Then we have to hit a wall
  • We skip all char except wall (1) until white space or \0
  • If the char in pos - 1 is not a wall, it's an error
  • The fact is if we hit a wall we have to found another one, walls works by pair. If the second wall was not hit, row (or column) is false

During this check, we extract the higher x and y to reformat_map, using white_spaces to reformat and put every lines ar the same length.

Last line is filled with \0.

Raycasting

Transforming a limited form of data into a three-dimensional projection with the help of tracing rays from the view point into the viewing volume. Main principle : rays can be cast and traced in groups based on certain geometric constraints. A ray from the pixel through the camera is obtained and the intersection of all objects in the picture is computed.

We have to create a maze world that has the following geometric constraints:

  • Walls are always at 90° angle with the floor.
  • Walls are made of cubes that have the same size.
  • Floor is always flat. Need to define some attributes :
  • Player/viewer’s height, player’s field of view (FOV), and player’s position.
  • Projection plane’s dimension.
  • Relationship between player and projection plane. FOV of 60 is generally a good choice. Player height reprensent half of window height.

The following illustrates steps:

  • Based on the viewing angle, subtract 30 degrees (half of the FOV).
  • Starting from column 0:
  • Cast a ray.
  • Trace the ray until it hits a wall.
  • Record the distance to the wall (the distance = length of the ray).
  • Add the angle increment so that the ray moves to the right
  • Repeat step 2 and 3 for each subsequent column until all rays are cast.

This figure show a really good method to understand how checking horizontal and vertical intersections.

Horizontal_check

Vertical_check

When horizontal and vertical intersections are merged, you have to check the lowest distance between player and wall using Pythagore theorem. Now you know which side of the wall you have to drawn.

This way will show distortion called "fish-eye". To remove the viewing distortion : correct_dist = distorted_dist * cos(angle);

Sources

cub3d's People

Contributors

bima42 avatar yvanoffp avatar

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.