Home  >  Article  >  Java  >  How can I add different height sizes to walls in a Java "windows' maze" project using the ray casting algorithm?

How can I add different height sizes to walls in a Java "windows' maze" project using the ray casting algorithm?

Barbara Streisand
Barbara StreisandOriginal
2024-11-06 01:32:02134browse

How can I add different height sizes to walls in a Java

Ray Casting with Different Height Size

Problem

In a Java "windows' maze" project using the ray casting algorithm, all walls have the same height size. The goal is to create a version with different height sizes.

Solution

1. Add Height Information to the Map:

Add a third value to each cell in the map to represent the height of the wall in that cell. For example, pmap[y][x] = (color, distance, height).

2. Update Ray-Casting Algorithm:

  • Check Z Value for Hit Detection: When checking for hits, include the height values to determine whether the ray has hit a wall of the correct height.
  • Compute Projected Height: Calculate the projected height of the wall based on the distance from the ray to the hit point and the viewing angle.
  • Adjust Rendering:

    • Variable Height Rendering: Use the projected height to adjust the scanline rendering for the wall, creating a 3D effect.
    • Topside Rendering: Check for hits on the back faces of walls and render the topside color from the last rendered y coordinate to the current one.

3. Consider Mouse Wheel Input:

Allow users to adjust the height of walls (in map editor mode) using the mouse wheel.

Detailed Code Snippets: Here is a snippet from the revised castRayInX function:

    // Check for hits on front or back wall based on direction
    boolean hit = false;
    Color c = null;
    int z = 0;
    if (slope > 0) {
        int firstX = ((eye.getX() / SQUARE_SIZE) + 1) * SQUARE_SIZE;
        for (int x = firstX; x < map[0].length * SQUARE_SIZE; x += SQUARE_SIZE) {
            int y = (int) (slope * (x - eye.getX()) + eye.getY());
            if (isOutside(x, y, Color.MAGENTA, this.showRayCastingX))
                break;
            c = colorAt(x, y);
            z = heightAt(x, y);
            if (c == null)
                c = colorAt(x, y - 1);
            if (c == null)
                c = colorAt(x - 1, y);
            if (c == null)
                c = colorAt(x - 1, y - 1);
            if (c != null) {
                int DX = x - eye.getX();
                double DY = y - eye.getY();
                hit = true;
                break;
            }
        }
        if (!hit && slope != Double.POSITIVE_INFINITY) // check back wall
            for (int x = firstX; x >= 0; x -= SQUARE_SIZE) {
                int y = (int) (slope * (x - eye.getX()) + eye.getY());
                if (isOutside(x, y, Color.MAGENTA, this.showRayCastingX))
                    break;
                c = colorAt(x, y);
                z = heightAt(x, y);
                if (c == null)
                    c = colorAt(x, y - 1);
                if (c == null)
                    c = colorAt(x - 1, y);
                if (c == null)
                    c = colorAt(x - 1, y - 1);
                if (c != null) {
                    int DX = x - eye.getX();
                    double DY = y - eye.getY();
                    hit = true;
                    break;
                }
            }
    } else {
        int firstX = ((eye.getX() / SQUARE_SIZE)) * SQUARE_SIZE;
        for (int x = firstX; x >= 0; x -= SQUARE_SIZE) {
            int y = (int) (slope * (x - eye.getX()) + eye.getY());
            if (isOutside(x, y, Color.MAGENTA, this.showRayCastingX))
                break;
            Color c = colorAt(x, y);
            int z = heightAt(x, y);
            if (c == null)
                c = colorAt(x, y - 1);
            if (c == null)
                c = colorAt(x - 1, y);
            if (c == null)
                c = colorAt(x - 1, y - 1);
            if (c != null) {
                int DX = x - eye.getX();
                double DY = y - eye.getY();
                hit = true;
                break;
            }
        }
        if (!hit && slope != Double.POSITIVE_INFINITY) // check back wall
            for (int x = firstX; x < map[0].length * SQUARE_SIZE; x += SQUARE_SIZE) {
                int y = (int) (slope * (x - eye.getX()) + eye.getY());
                if (isOutside(x, y, Color.MAGENTA, this.showRayCastingX))
                    break;
                Color c = colorAt(x, y);
                int z = heightAt(x, y);
                if (c == null)
                    c = colorAt(x, y - 1);
                if (c == null)
                    c = colorAt(x - 1, y);
                if (c == null)
                    c = colorAt(x - 1, y - 1);
                if (c != null) {
                    int DX = x - eye.getX();
                    double DY = y - eye.getY();
                    hit = true;
                    break;
                }
            }
    }

    // If hit, compute projected height and adjust rendering
    if (hit) {
        h = (int) (this.screenDistance / distance * z);
        int hw = (int) (this.screenDistance / distance * WALL_HEIGHT); // WALL_HEIGHT value is 300px at default
        int y0 = (hw + vh) / 2;
        int y1 = (vh - h) / 2;
        graphics.drawLine(xOnScreen, y0, xOnScreen, y1);
    }

Additional Notes:

  • Update the castRayInY function in a similar manner.
  • To detect back faces of walls, simply add another slope condition:

    } else if (slope > 0 && slope < Double.POSITIVE_INFINITY) {...} // back face

    The above is the detailed content of How can I add different height sizes to walls in a Java "windows' maze" project using the ray casting algorithm?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn