Coder Social home page Coder Social logo

mikedotcpp / gbraycaster Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 1.0 110.94 MB

An implementation of the classic raycast rendering algorithm for Cocos2d-x. Used in games like Wolfenstein3D, Rise of the Triad, The Terminator: Rampage, Catacomb3-D, Super 3D Noah's Ark, and more.

License: MIT License

CMake 1.22% C++ 71.10% GLSL 0.36% Python 0.27% Shell 0.12% Batchfile 0.01% Objective-C 14.41% C 6.41% Makefile 0.18% Objective-C++ 2.41% Java 3.37% JavaScript 0.07% Lua 0.07%
game-development raycast renderer

gbraycaster's Introduction

Grid-Based Raycaster

This project adapts the old-school raycast rendering algorithm to be used with Cocos2d-x. Used in games like Wolfenstein3D, Rise of the Triad, The Terminator: Rampage, Catacomb3-D, Super 3D Noah's Ark, and more.

All of the code is freely available from my GitHub page.

What is Raycasting?

Raycasting refers to an algorithm used by games like the original Wolfenstein3D to sell the illusion of a first-person perspective.

The basic idea is to fire a bunch of rays from the player and draw whatever those rays hit. The number of rays to fire is equal to the number of vertical columns on screen (the pixel width of the screen). For each ray that hits an object in the world (like a wall), a scaled vertical strip of that object is drawn.

In this early experiement, vertical strips were created using 1-pixel wide quads:

Raycasting animation

While a promising start, I wanted to have the option of rendering floors/ceilings and be able to easily use some nice features of cocos - like actions, particle effects, etc. I started experimenting with a slightly different approach...

Raycasting, Cubed

Instead of drawing vertical strips per raycast, I decided to draw "blocks" per unique hit from a raycast. That is, draw cubes for each tile that a ray intersects with in projected 3D space.

Each cube is made from a Sprite3D object with 6 Sprite's as children that each represent a different face. All faces are optional and completely independent, which means one block could generate a cube with potentially 6 unique sides to it.

This gives me the flexibility I wanted in developing a unique look for maps. Video below:

Alt text

You can see in the video that walls, floors, billboards and ceilings are rendered and that player can collect doors by walking into them. This was a convenient way to show how one might use the simple Behavior system to attach a pickup behavior to door objects. It also suggests one might allow the player to change maps in realtime, a la a poor man's version of Minecraft.

Maps themselves are generated from a human-readable JSON text file. Much more on the format can be found here.

Just One More Thing...

By using the raycasting algorithm for visibility determination, I am able to leverage the 3D capabilities of Cocos2d-x to display a simple grid-based map in a first-person perspective. What would happen if I could assign a 3D mesh to the Sprite3D container object? Video below:

Alt text

This demo still utilizes the same raycasting technique from CocosWolf3D, but renders the results in a vastly different way. There's a lot going on here:

Instancing

I am using a technique called instancing to draw many similarly textured objects with little cost. The objects themselves are proper 3D meshes (of cubes) which are assigned to the Sprite3D container object at load time. Like the sprite-based version, these cubes can define unique textures for all 6 faces. Note: instancing requires an OpenGL ES extension for the 2.0 API and is included in 3.0 and above, therefore it may not be available on some hardware.

Lighting

The cube meshes rely on a slightly modified version of the stock 3D shader that comes with Cocos2d-x and has support for normal mapping, multiple light sources, and geometry instancing. This demo makes use of ambient light, a spotlight, and a few point lights.

Reflections

You may have noticed the large columns seem to be reflected in the floor of the main chamber. This is a quick and dirty old-school trick at play: render the columns below the marble floor and change the alpha of the marble floor texture so that the columns are visible through it.

Custom Shaders

Some tiles (like the lava tile) can have their own specialized shader that animates the texture sinusoidally.

Variable Height Walls

Each map can define layers (called Plane's) at different heights to give the illusion of walls with varying height.

Particles

The demo uses 3D particles that come with Cocos2d-x for a more dramatic effect. Used as-is, no changes necessary.

I hope you enjoyed reading about my approach to making a first-person perspective game using Cocos2d-x! These projects have been tested for Mac/iOS only. Code can be found:

Keep me in the loop with all your awesome creations, email me at [email protected]

gbraycaster's People

Contributors

mikedotcpp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

sidewinder

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.