Implement raycaster.

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2, Sig, Sigo 3 years ago
parent c17b05c786
commit 420fdb81a3
  1. 0
      old/Panel.java
  2. 0
      old/Pixel.java
  3. 0
      old/SigRenderer.java
  4. 0
      old/Triangle.java
  5. 83
      src/sig/Panel.java
  6. 94
      src/sig/SigRenderer.java

@ -14,7 +14,8 @@ public class Panel extends JPanel{
startTime = System.nanoTime(); startTime = System.nanoTime();
} }
public void paintComponent(Graphics g) { public void paintComponent(Graphics g) {
g.clearRect(0,0,SigRenderer.SCREEN_WIDTH,SigRenderer.SCREEN_HEIGHT); g.setColor(Color.BLACK);
g.fillRect(0,0,SigRenderer.SCREEN_WIDTH,SigRenderer.SCREEN_HEIGHT);
//Vector3f origin = new Vector3f(Math.cos(SigRenderer.rot)*10,0,10+Math.sin(SigRenderer.rot)*10); //Vector3f origin = new Vector3f(Math.cos(SigRenderer.rot)*10,0,10+Math.sin(SigRenderer.rot)*10);
/*for (int x=0;x<SigRenderer.SCREEN_WIDTH/RESOLUTION;x++) { /*for (int x=0;x<SigRenderer.SCREEN_WIDTH/RESOLUTION;x++) {
for (int y=0;y<SigRenderer.SCREEN_HEIGHT/RESOLUTION;y++) { for (int y=0;y<SigRenderer.SCREEN_HEIGHT/RESOLUTION;y++) {
@ -31,6 +32,86 @@ public class Panel extends JPanel{
} }
} }
}*/ }*/
final int h=64;
for (int x=0;x<SigRenderer.SCREEN_WIDTH;x++) {
double cameraX = 2*x/(double)SigRenderer.SCREEN_WIDTH-1;
double rayDirX = SigRenderer.dirX+SigRenderer.planeX*cameraX;
double rayDirY = SigRenderer.dirY+SigRenderer.planeY*cameraX;
int mapX = (int)SigRenderer.x;
int mapY = (int)SigRenderer.y;
double sideDistX;
double sideDistY;
double deltaDistX=Math.abs(1/rayDirX);
double deltaDistY=Math.abs(1/rayDirY);
double perpWallDist=0;
int stepX;
int stepY;
int hit=0;
int side=0;
if (rayDirX<0) {
stepX=-1;
sideDistX=(SigRenderer.x-mapX)*deltaDistX;
} else {
stepX=1;
sideDistX=(mapX+1d-SigRenderer.x)*deltaDistX;
}
if (rayDirY<0) {
stepY=-1;
sideDistY=(SigRenderer.y-mapY)*deltaDistY;
} else {
stepY=1;
sideDistY=(mapY+1d-SigRenderer.y)*deltaDistY;
}
while (hit==0) {
if (sideDistX<sideDistY) {
sideDistX+=deltaDistX;
mapX+=stepX;
side=0;
} else {
sideDistY+=deltaDistY;
mapY+=stepY;
side=1;
}
if (SigRenderer.worldMap[mapX][mapY]>0) {hit=1;}
if (side==0) {
perpWallDist=sideDistX-deltaDistX;
} else {
perpWallDist=sideDistY-deltaDistY;
}
}
int lineHeight = (int)(h/perpWallDist);
int drawStart=-lineHeight/2+h/2;
if (drawStart<0) {
drawStart=0;
}
int drawEnd=lineHeight/2+h/2;
if (drawEnd>=h) {drawEnd=h-1;}
Color col;
switch(SigRenderer.worldMap[mapX][mapY]) {
case 1:{col=Color.RED;}break;
case 2:{col=Color.GREEN;}break;
case 3:{col=Color.BLUE;}break;
case 4:{col=Color.WHITE;}break;
default:{col=Color.YELLOW;}break;
}
if (side==1) {col=col.darker();}
g.setColor(col);
g.drawLine(x,drawStart,x,drawEnd);
}
endTime=System.nanoTime(); endTime=System.nanoTime();
SigRenderer.DRAWLOOPTIME = (endTime-startTime)/1000000f; SigRenderer.DRAWLOOPTIME = (endTime-startTime)/1000000f;
} }

@ -1,17 +1,11 @@
package sig; package sig;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.vecmath.Point2d;
import javax.vecmath.Tuple3d;
import javax.vecmath.Vector3f;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener; import java.awt.event.MouseMotionListener;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.Color;
public class SigRenderer implements KeyListener,MouseListener,MouseMotionListener{ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListener{
public final static int SCREEN_WIDTH=1280; public final static int SCREEN_WIDTH=1280;
@ -20,7 +14,81 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
public static float DRAWTIME=0; public static float DRAWTIME=0;
public static float DRAWLOOPTIME=0; public static float DRAWLOOPTIME=0;
public static double x = 22;
public static double y = 12;
public static double dirX = -1;
public static double dirY = 0;
public static double planeX = 0;
public static double planeY = 0.66;
public static double moveSpeed = 0.01;
public static double rotSpeed = Math.PI/64;
boolean UP_KEY=false;
boolean DOWN_KEY=false;
boolean RIGHT_KEY=false;
boolean LEFT_KEY=false;
public static int[][] worldMap = new int[][]
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,2,2,2,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1},
{1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,3,0,0,0,3,0,0,0,1},
{1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,2,2,0,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,4,0,0,0,0,5,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,4,0,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
public void runGameLoop() { public void runGameLoop() {
if (UP_KEY) {
if (worldMap[(int)(x+dirX*moveSpeed)][(int)y]==0) {
x+=dirX*moveSpeed;
}
if (worldMap[(int)x][(int)(y+dirY*moveSpeed)]==0) {
y+=dirY*moveSpeed;
}
}
if (DOWN_KEY) {
if (worldMap[(int)(x-dirX*moveSpeed)][(int)y]==0) {
x-=dirX*moveSpeed;
}
if (worldMap[(int)x][(int)(y-dirY*moveSpeed)]==0) {
y-=dirY*moveSpeed;
}
}
if (LEFT_KEY) {
double oldDirX=dirX;
dirX=dirX*Math.cos(-rotSpeed)-dirY*Math.sin(-rotSpeed);
dirY=oldDirX*Math.sin(-rotSpeed)+dirY*Math.cos(-rotSpeed);
double oldPlaneX=planeX;
planeX=planeX*Math.cos(-rotSpeed)-planeY*Math.sin(-rotSpeed);
planeY=oldPlaneX*Math.sin(-rotSpeed)+planeY*Math.cos(-rotSpeed);
}
if (RIGHT_KEY) {
double oldDirX=dirX;
dirX=dirX*Math.cos(rotSpeed)-dirY*Math.sin(rotSpeed);
dirY=oldDirX*Math.sin(rotSpeed)+dirY*Math.cos(rotSpeed);
double oldPlaneX=planeX;
planeX=planeX*Math.cos(rotSpeed)-planeY*Math.sin(rotSpeed);
planeY=oldPlaneX*Math.sin(rotSpeed)+planeY*Math.cos(rotSpeed);
}
} }
SigRenderer(JFrame f) { SigRenderer(JFrame f) {
@ -102,11 +170,21 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:{UP_KEY=true;}break;
case KeyEvent.VK_DOWN:{DOWN_KEY=true;}break;
case KeyEvent.VK_LEFT:{LEFT_KEY=true;}break;
case KeyEvent.VK_RIGHT:{RIGHT_KEY=true;}break;
}
} }
@Override @Override
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub switch (e.getKeyCode()) {
case KeyEvent.VK_UP:{UP_KEY=false;}break;
case KeyEvent.VK_DOWN:{DOWN_KEY=false;}break;
case KeyEvent.VK_LEFT:{LEFT_KEY=false;}break;
case KeyEvent.VK_RIGHT:{RIGHT_KEY=false;}break;
}
} }
} }

Loading…
Cancel
Save