This site requires JavaScript, please enable it in your browser!
Greenfoot back
Triangluminati
Triangluminati wrote ...

2021/12/15

I have been trying to figure out how to do something like ray casting but I ran into a problem

Triangluminati Triangluminati

2021/12/15

#
For starters I just started learning to code about 4 or 5 months ago for my school so I'm sorry if I get terminology or something wrong. I have been trying to learn how to code ray casting in Greenfoot so I can apply it to the project I have been working on. I have been following a tutorial somewhat (https://sinepost.wordpress.com/2012/06/16/raycasting/) but I have been trying to come up with my own solutions in areas. I have gotten a useable, but not very playable, version of ray casting to work but the rays have to travel far enough that once they reach an object, the distance between them is so large and the result comes out very blocky/chunky. My main question is if there is a way to shoot out more rays to make the objects appear more smooth or if there is a different approach to reach the same effect. I will add my code here for my player:
import greenfoot.*;
import java.util.*;
import java.util.ArrayList;
public class refPlr extends Actor
{
    private boolean CANMOVEFORWARD = true;
    private boolean CANMOVEBACKWARDS = true;
    public void act()
    {
        checkForPoints();
        collisions();
        if(CANMOVEFORWARD)
        if(Greenfoot.isKeyDown("w"))
        {
            move(1);
        }
        if(CANMOVEBACKWARDS)
        if(Greenfoot.isKeyDown("s"))
        {
            move(-1);
        }
        if(Greenfoot.isKeyDown("d"))
        {
            setRotation(getRotation()-1);
        }
        if(Greenfoot.isKeyDown("a"))
        {
            setRotation(getRotation()+1);
        }
    }
    private boolean rotationCycle = true;
    private boolean check = true;
    ArrayList<Integer> DISTANCEX = new ArrayList<Integer>();
    ArrayList<Integer> DISTANCEY = new ArrayList<Integer>();
    ArrayList<Integer> ROTATIONPOINT = new ArrayList<Integer>();
    ArrayList<Integer> HITX = new ArrayList<Integer>();
    ArrayList<Integer> HITY = new ArrayList<Integer>();
    ArrayList<Integer> COLOR = new ArrayList<Integer>();
    int x;
    int y;
    int CHECKEDROTATION;
    int ORIGINALROTATION;
    final int FOV = 50;
    final int IMGWIDTH = 5;
    public void checkForPoints()
    {
        CHECKEDROTATION = getRotation()-FOV;
        ORIGINALROTATION = getRotation()-FOV;
        while(rotationCycle)
        {
            while(check)
            {
                if(getWorld().getObjectsAt(getX()+x, getY()+y, refObj.class).size() == 0 && x < 500 && y < 500 && x > -500 && y > -500)
                {
                    double rot = Math.toRadians(CHECKEDROTATION);
                    int cx =(int)Math.round(Math.cos(rot)*3);
                    int cy =(int)Math.round(Math.sin(rot)*3);
                    x += cx;
                    y += cy;
                }
                else if(getWorld().getObjectsAt(getX()+x, getY()+y, refObj.class).size() > 0)
                {
                    refObj r = getWorld().getObjectsAt(getX()+x, getY()+y, refObj.class).get(0);
                    DISTANCEX.add(x);
                    DISTANCEY.add(y);
                    HITX.add(getX()+x);
                    HITY.add(getY()+y);
                    COLOR.add(r.color);
                    ROTATIONPOINT.add(CHECKEDROTATION-ORIGINALROTATION);
                    x = 0;
                    y = 0;
                    check = false;
                }
                else
                {   
                    x = 0;
                    y = 0;
                    break;
                }
            }
            if(CHECKEDROTATION >= getRotation() + FOV)
                rotationCycle = false;
            CHECKEDROTATION++;
            check = true;
        }
        if(DISTANCEX.size() > 0)
        {
            GreenfootImage img;
            for(int REMOVED = (FOV*2)-1; REMOVED >= 0; REMOVED--)
            {
                pasteImg PASTEDIMAGE = getWorld().getObjects(pasteImg.class).get(REMOVED);
                img = PASTEDIMAGE.getImage();

                img.setColor(Color.WHITE);
                img.fillRect(0, 0, IMGWIDTH, 500);
                PASTEDIMAGE.setImage(img);
            }
            int ROTATIONREFERENCE = ROTATIONPOINT.size();
            for(int ADDED = ROTATIONPOINT.size()-2; ADDED >= 0; ADDED--)
            {
                pasteImg p = getWorld().getObjects(pasteImg.class).get(ROTATIONPOINT.get(ADDED));
                double across = (8 / (double)IMGWIDTH - 0.5);
                double distX = HITX.get(ADDED) - getX();
                double distY = HITY.get(ADDED) - getY();
                double rayLength = Math.sqrt(distX * distX + distY * distY);
                double planeDist = Math.sqrt(0.5 * 0.5 + across * across);
                int height = (int)(((0.5 * IMGWIDTH) / rayLength) * 5000);
                img = new GreenfootImage(IMGWIDTH, height);
                if(COLOR.get(ADDED) == 0)
                    img.setColor(Color.GREEN);
                else
                {
                    img.setColor(Color.BLACK);
                }
                img.fillRect(0, 0, IMGWIDTH, height);
                p.setImage(img);
            }
        }
        if(DISTANCEX.size() == 0 && getWorld().getObjects(pasteImg.class).size() > 0)
        {
            for(int REMOVED = getWorld().getObjects(pasteImg.class).size()-1; REMOVED >= 0; REMOVED--)
            {
                pasteImg PASTEDIMAGE = getWorld().getObjects(pasteImg.class).get(REMOVED);
                GreenfootImage img = PASTEDIMAGE.getImage();
                img.setColor(Color.WHITE);
                img.fillRect(0, 0, IMGWIDTH, 500);
                PASTEDIMAGE.setImage(img);
            }
            pasteImg PASTEDIMAGE = getWorld().getObjects(pasteImg.class).get(0);
        }
        DISTANCEX.clear();
        DISTANCEY.clear();
        HITX.clear();
        HITY.clear();
        COLOR.clear();
        ROTATIONPOINT.clear();
        rotationCycle = true;
        check = true;
    }
    
    public void collisions()
    {
        double radians = Math.toRadians(getRotation());
        int dx = (int) Math.round(Math.cos(radians) * 7);
        int dy = (int) Math.round(Math.sin(radians) * 7);
        if(getOneObjectAtOffset(dx, dy, refObj.class)!=null)
        {
            CANMOVEFORWARD = false;
        }
        else CANMOVEFORWARD = true;
        if(getOneObjectAtOffset(-dx, -dy, refObj.class)!=null)
        {
            CANMOVEBACKWARDS = false;
        }
        else CANMOVEBACKWARDS = true;
    }
}
and I will add my world code
import greenfoot.*;
import java.util.*;
public class MyWorld extends World
{

    static int X = 500;
    static int Y = 500;
    public MyWorld()
    {    
        super(X, Y, 1); 
        ArrayList<Integer> GRIDX = new ArrayList<Integer>();
        ArrayList<Integer> GRIDY = new ArrayList<Integer>();
        //9X9 placement grid
        for(int x = 25; x < X; x += 50)
        {
            GRIDX.add(x);            
        }
        for(int y = 25; y < Y; y += 50)
        {
            GRIDY.add(y);
        }
        addObject(new refObj(), GRIDX.get(5), GRIDY.get(0));
        addObject(new refObj(), GRIDX.get(5), GRIDY.get(1));
        addObject(new refObj(), GRIDX.get(5), GRIDY.get(2));
        addObject(new refObj(), GRIDX.get(8), GRIDY.get(4));
        addObject(new refObj(), GRIDX.get(8), GRIDY.get(5));
        addObject(new refObj(), GRIDX.get(8), GRIDY.get(6));
        addObject(new refObj(), GRIDX.get(8), GRIDY.get(7));
        addObject(new refObj(), GRIDX.get(6), GRIDY.get(4));
        addObject(new refObj(), GRIDX.get(6), GRIDY.get(5));
        addObject(new refObj(), GRIDX.get(6), GRIDY.get(6));
        addObject(new refObj(), GRIDX.get(6), GRIDY.get(7));
        for(int dirx = 0; dirx<9 ; dirx++)
        addObject(new refObj(1), GRIDX.get(dirx), GRIDY.get(0));
        for(int dirx = 0; dirx<9 ; dirx++)
        addObject(new refObj(1), GRIDX.get(dirx), GRIDY.get(8));
        for(int diry = 1; diry<8 ; diry++)
        addObject(new refObj(1), GRIDX.get(0), GRIDY.get(diry));
        for(int diry = 1; diry<8 ; diry++)
        addObject(new refObj(1), GRIDX.get(8), GRIDY.get(diry));
        addObject(new BackGround(), X/2, Y/2);
        for(int AMOUNT = X; AMOUNT > 0; AMOUNT -= X/100)
        {
            addObject(new pasteImg(), AMOUNT, Y/2);
        }
        addObject(new refPlr(), 100, 100);
        setPaintOrder(pasteImg.class, BackGround.class, refObj.class, refPlr.class);
    }
}
You need to login to post a reply.