Home Java javaTutorial How can I modify the raycasting algorithm in Java to create walls of different heights in a 'windows' maze'?

How can I modify the raycasting algorithm in Java to create walls of different heights in a 'windows' maze'?

Nov 06, 2024 pm 04:19 PM

How can I modify the raycasting algorithm in Java to create walls of different heights in a

Raycasting with different height size

I have a java project who makes the "windows' maze" and use the ray casting algorithm. Here's a screenshot:

as you can see all the walls have the same height size. I would like to do the same but with different height size

private void castRay(int xOnScreen,double angle,double direction) {

R rx = castRayInX(angle,direction);
R ry = castRayInY(angle,direction);
// In case of out-of-space rays
if (rx.getDistance()==Double.MAX_VALUE && ry.getDistance()==Double.MAX_VALUE) {
    graphics.setColor(BACKGROUND);
    graphics.drawLine(xOnScreen,0,xOnScreen,this.image.getHeight());
    return;
}
double distance = rx.getDistance();
double normal = rx.getNormal();
Color c = rx.getColor();
double coef = Math.cos((angle+direction+Math.PI)-normal);
Plot collision = rx.getPlot();

if (ry.getDistance()<rx.getDistance()) {
    distance = ry.getDistance();
    normal = ry.getNormal();
    c = ry.getColor();
    coef = Math.cos((angle+direction+Math.PI)-normal);
    collision = ry.getPlot();
}

coef = Math.abs(coef);
int factor = map.length*SQUARE_SIZE;
double d = (double)(distance+factor)/factor;
coef *= 1/(d*d);
Color c2 = new Color((int)(c.getRed()*coef),(int)(c.getGreen()*coef),(int)(c.getBlue()*coef));
graphics.setColor(c2);
Copy after login

// graphics.setColor(c); // no illumination

distance *= Math.cos(angle); // lens correction
int h = (int)(this.screenDistance/distance*WALL_HEIGHT); // perspective height
int vh = this.image.getHeight();
graphics.drawLine(xOnScreen,(vh-h)/2,xOnScreen,(vh+h)/2);
drawEye(direction,collision);
Copy after login

}

private R castRayInX(double angleRay,double direction) {

double angle = angleRay+direction;
double x1 = eye.getX()+SQUARE_SIZE*Math.cos(angle);
double y1 = eye.getY()+SQUARE_SIZE*Math.sin(angle);
double slope = (y1-eye.getY())/(x1-eye.getX());
if (Math.cos(angle)==0) {
    if (Math.sin(angle)>0)
        return new R(Double.MAX_VALUE,3*Math.PI/2,BACKGROUND,null);
    else
        return new R(Double.MAX_VALUE,Math.PI/2,BACKGROUND,null);
}
if (Math.cos(angle)>0) {
    int firstX = ((eye.getX()/SQUARE_SIZE)+1)*SQUARE_SIZE;
    R r = new R(Double.MAX_VALUE,angle+Math.PI,BACKGROUND,null);
    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);
        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();
            return new R(Math.sqrt(DX*DX+DY*DY),Math.PI,c,new Plot((int)x,(int)y, WALL_HEIGHT));
        }
    }
    return r;
} else {
    int firstX = ((eye.getX()/SQUARE_SIZE))*SQUARE_SIZE;
    R r = new R(Double.MAX_VALUE,angle+Math.PI,BACKGROUND,null);
    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);
        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();
            return new R(Math.sqrt(DX*DX+DY*DY),0,c,new Plot((int)x,(int)y, WALL_HEIGHT));
        }
    }
    return r;           
}
Copy after login

}
private R castRayInY(double angleRay,double direction) {
// System.out.println("cast ray 2 Y " angleRay " " direction);

double angle = angleRay+direction;
double x1 = eye.getX()+SQUARE_SIZE*Math.cos(angle);
double y1 = eye.getY()+SQUARE_SIZE*Math.sin(angle);
Copy after login

// System.out.println(eye " " x1 " " y1);

double slope = (y1-eye.getY())/(x1-eye.getX());
if (Math.sin(angle)==0) {
    if (Math.cos(angle)>0)
        return new R(Double.MAX_VALUE,Math.PI,BACKGROUND,null);
    else
        return new R(Double.MAX_VALUE,0,BACKGROUND,null);
}
if (Math.sin(angle)>0) {
    int firstY = ((eye.getY()/SQUARE_SIZE)+1)*SQUARE_SIZE;
    R r = new R(Double.MAX_VALUE,angle+Math.PI,BACKGROUND,null);
    for (int y = firstY; y<map.length*SQUARE_SIZE; y += SQUARE_SIZE) {
        int x = (int)((y-eye.getY())/slope)+eye.getX();
        if (isOutside(x,y,Color.CYAN,this.showRayCastingY)) break;
        Color c = colorAt(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) {
            double DX = x-eye.getX();
            int DY = y-eye.getY();
            return new R(Math.sqrt(DX*DX+DY*DY),3*Math.PI/2,c,new Plot((int)x,(int)y, WALL_HEIGHT));
            }
        }
        return r;
    } else {
        int firstY = ((eye.getY()/SQUARE_SIZE))*SQUARE_SIZE;
        R r = new R(Double.MAX_VALUE,angle+Math.PI,BACKGROUND,null);
        for (int y = firstY; y>=0; y -= SQUARE_SIZE) {
            int x = (int)((y-eye.getY())/slope)+eye.getX();
            if (isOutside(x,y,Color.CYAN,this.showRayCastingY)) break;
            Color c = colorAt(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) {
                double DX = x-eye.getX();
                int DY = y-eye.getY();
                return new R(Math.sqrt(DX*DX+DY*DY),Math.PI/2,c,new Plot((int)x,(int)y, WALL_HEIGHT));
            }
        }
        return r;           
    }
}
Copy after login

My Rclass has a Plot (x, y, z) for now I use WALL_HEIGHT a color, a distance and a normal for the light. For now this works but I would like to add a new

The above is the detailed content of How can I modify the raycasting algorithm in Java to create walls of different heights in a 'windows' maze'?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

See all articles