怎样用Java制作基本的3D引擎

步骤1:主类
首先要做的是被造是一个主要的阶级。主类将处理向用户显示图像,调用其他类以重新计算应向播放器显示的内容以及更新相机的位置。
对于该类,导入将是:
import java.awt.color;
import java.awt.graphics;
import java.awt.image.bufferstrategy;
import java.awt.image.bufferedimage;
import java.awt.image.databufferint;
import java.util.arraylist;
import javax.swing.jframe;
该类及其变量将如下所示:
public class game extends jframe implements runnable{
private static final long serialversionuid = 1l;
public int mapwidth = 15;
public int mapheight = 15;
private thread thread;
private boolean running;
private bufferedimage image;
public int[] pixels;
public static int[][] map =
{
{1,1,1,1,1,1,1,1,2,2,2,2,2,2,2},
{1,0,0,0,0,0,0,0,2,0,0,0,0,0,2},
{1,0,3,3,3,3,3,0,0,0,0,0,0,0,2},
{1,0,3,0,0,0,3,0,2,0,0,0,0,0,2},
{1,0,3,0,0,0,3,0,2,2,2,0,2,2,2},
{1,0,3,0,0,0,3,0,2,0,0,0,0,0,2},
{1,0,3,3,0,3,3,0,2,0,0,0,0,0,2},
{1,0,0,0,0,0,0,0,2,0,0,0,0,0,2},
{1,1,1,1,1,1,1,1,4,4,4,0,4,4,4},
{1,0,0,0,0,0,1,4,0,0,0,0,0,0,4},
{1,0,0,0,0,0,1,4,0,0,0,0,0,0,4},
{1,0,0,2,0,0,1,4,0,3,3,3,3,0,4},
{1,0,0,0,0,0,1,4,0,3,3,3,3,0,4},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,4},
{1,1,1,1,1,1,1,4,4,4,4,4,4,4,4}
};
请注意,可以将地图重新配置为所需的内容,我在这里只是一个样品。地图上的数字表示该位置的墙壁类型。 0代表空白空间,而其他任何数字则代表实心墙和随之而来的纹理。 bufferedimage是显示给用户的,像素是图像中所有像素的数组。其他变量实际上不会再次出现,它们只是用来使图形和程序正常工作。
构造函数现在看起来像这样:
public game() {
thread = new thread(this);
image = new bufferedimage(640, 480, bufferedimage.type_int_rgb);
pixels = ((databufferint)image.getraster().getdatabuffer()).getdata();
setsize(640, 480);
setresizable(false);
settitle(“3d engine”);
setdefaultcloseoperation(jframe.exit_on_close);
setbackground(color.black);
setlocationrelativeto(null);
setvisible(true);
start();
}
大多数只是类变量和框架的初始化。 “ pixels =“之后的代码连接像素和图像,以便每当更改像素中的数据值时,向用户显示图像时就会在图像上显示相应的更改。
start和stop方法是简单并用于确保程序安全地开始和结束。
private synchronized void start() {
running = true;
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch(interruptedexception e) {
e.printstacktrace();
}
}
game类中需要的最后两个方法是render和run方法。渲染方法将如下所示:
public void render() {
bufferstrategy bs = getbufferstrategy();
if(bs == null) {
createbufferstrategy(3);
return;
}
graphics g = bs.getdrawgraphics();
g.drawimage(image, 0, 0, image.getwidth(), image.getheight(), null);
bs.show();
}
渲染时使用缓冲策略,以使屏幕更新更加流畅。总体而言,使用缓冲策略只会帮助游戏在运行时看起来更好。为了将图像实际绘制到屏幕上,需要从缓冲策略中获取图形对象并用于绘制图像。
run方法非常重要,因为它可以处理程序不同部分的更新频率。为此,它使用一些代码来跟踪何时经过了1/60秒,以及何时更新了屏幕和摄像机。这样可以提高程序运行的流畅度。 run方法如下所示:
public void run() {
long lasttime = system.nanotime();
final double ns = 1000000000.0 / 60.0;//60 times per second
double delta = 0;
requestfocus();
while(running) {
long now = system.nanotime();
delta = delta + ((now-lasttime) / ns);
lasttime = now;
while (delta 》= 1)//make sure update is only happening 60 times a second
{
//handles all of the logic restricted time
delta--;
}
render();//displays to the screen unrestricted time
}
}
一旦所有这些方法,构造函数和变量都在其中,那么当前在game类中剩下要做的唯一事情就是添加一个main方法。主要方法非常简单,您要做的就是:
public static void main(string [] args) {
game game = new game();
}
现在,主类已完成!如果您现在运行该程序,则将弹出黑屏。
步骤2:纹理类
在进入查找屏幕外观的计算之前,我将绕行并设置texture类。纹理将应用于环境中的各种墙壁,并将来自保存在项目文件夹中的图像。在图像中,我包含了在网上找到的4个纹理,将在该项目中使用。您可以使用任何想要的纹理。要使用这些纹理,我建议将它们放在项目文件中的文件夹中。为此,请转到项目文件夹(在eclipse中,它位于工作区文件夹中)。转到项目文件夹后,创建一个名为“ res”或其他名称的新文件夹。将纹理放在此文件夹中。您可以将纹理放置在其他地方,这就是我存储纹理的地方。完成此操作后,我们就可以开始编写代码以使纹理可用。
该类的导入为:
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.ioexception;
import javax.imageio.imageio;
该类头及其变量将看起来像这样:
public class texture {
public int[] pixels;
private string loc;
public final int size;
数组像素用于保存纹理图像中所有像素的数据。 loc用于向计算机指示可以找到纹理的图像文件的位置。 size是一侧的纹理大小(64x64图像的大小为64),并且所有纹理将完全为正方形。
构造函数将初始化loc和size变量并调用a方法来将图像数据加载到像素中。看起来像这样:
public texture(string location, int size) {
loc = location;
size = size;
pixels = new int[size * size];
load();
}
现在,texture类剩下的就是添加一个load方法来从图像中获取数据并将它们存储在像素数据数组中。此方法将如下所示:
private void load() {
try {
bufferedimage image = imageio.read(new file(loc));
int w = image.getwidth();
int h = image.getheight();
image.getrgb(0, 0, w, h, pixels, 0, w);
} catch (ioexception e) {
e.printstacktrace();
}
}
load方法的工作原理是从loc指向的文件中读取数据并将该数据写入缓冲的图像。然后,从缓冲的图像中获取每个像素的数据,并将其存储在像素中。
此时,texture类已完成,因此我将继续定义一些将要使用的纹理在最终程序中。为此,请将此
public static texture wood = new texture(“res/wood.png”, 64);
public static texture brick = new texture(“res/redbrick.png”, 64);
public static texture bluestone = new texture(“res/bluestone.png”, 64);
public static texture stone = new texture(“res/greystone.png”, 64);
放在“公共类texture”行和“ public int []像素”之间。
使其余部分可以访问这些纹理该程序让我们继续前进,并将其交给game类。为此,我们将需要一个arraylist来容纳所有纹理,并且需要将纹理添加到此arraylist中。要创建arraylist,请将以下代码行和变量放在类的顶部附近:
public arraylist textures;
此arraylist必须在构造函数中初始化,并且还应添加纹理在构造函数中。在构造函数中添加以下代码:
textures = new arraylist();
textures.add(texture.wood);
textures.add(texture.brick);
textures.add(texture.bluestone);
textures.add(texture.stone);
现在可以使用纹理了!
第3步:相机类
现在让我们绕道而行并设置camera类。 camera类跟踪玩家在2d地图中的位置,并负责更新玩家的位置。为此,该类将实现keylistener,因此将需要导入keyevent和keylistener。
import java.awt.event.keyevent;
import java.awt.event.keylistener;
需要许多变量来跟踪摄像机的位置及其所见。因此,该类的第一个块如下所示:
public class camera implements keylistener {
public double xpos, ypos, xdir, ydir, xplane, yplane;
public boolean left, right, forward, back;
public final double move_speed = .08;
public final double rotation_speed = .045;
xpos和ypos是玩家在game类中创建的2d地图上的位置。 xdir和ydir是指向玩家所面对方向的向量的x和y分量。 xplane和yplane也是向量的x和y分量。 xplane和yplane定义的向量始终垂直于方向向量,并且在一侧指向相机视场的最远边缘。另一边最远的边缘就是负平面向量。方向矢量和平面矢量的组合定义了相机视场中的内容。布尔值用于跟踪用户按下了哪些键,以便用户可以移动相机。 move_speed和rotation_speed指示在用户按下相应键时相机移动和旋转的速度。
接下来是构造函数。构造函数接受告诉类的位置的值,并将相机分配给相应的变量(xpos,ypos 。..)。
public camera(double x, double y, double xd, double yd, double xp, double yp)
{
xpos = x;
ypos = y;
xdir = xd;
ydir = yd;
xplane = xp;
yplane = yp;
}
相机对象在最终程序中将需要它,所以让我们继续添加一个。在具有所有其他变量声明的game类中,添加
public camera camera;
,并在构造函数中添加
camera = new camera(4.5, 4.5, 1, 0, 0, -.66);
addkeylistener(camera);
,此摄像机将与地图一起使用我正在使用,如果您使用的是其他地图,或者想从其他位置开始,请调整xpos和ypos的值(在我的示例中为4和6)。使用.66可以提供良好的视野,但是您可以调整该值以获得不同的fov。
现在,camera类具有构造函数,我们可以开始添加方法来跟踪用户的输入并更新相机的位置/方向。因为camera类实现了keyboardlistener,所以它必须具有其实现的所有方法。 eclipse应该自动提示您添加这些方法。您可以将keytyped方法保留为空白,但将使用其他两种方法。当按下相应的键时,keypressed会将布尔值设置为true,而释放键时,keyreleased会将其更改为false。方法看起来像这样:
public void keypressed(keyevent key) {
if((key.getkeycode() == keyevent.vk_left))
left = true;
if((key.getkeycode() == keyevent.vk_right))
right = true;
if((key.getkeycode() == keyevent.vk_up))
forward = true;
if((key.getkeycode() == keyevent.vk_down))
back = true;
}

public void keyreleased(keyevent key) {
if((key.getkeycode() == keyevent.vk_left))
left = false;
if((key.getkeycode() == keyevent.vk_right))
right = false;
if((key.getkeycode() == keyevent.vk_up))
forward = false;
if((key.getkeycode() == keyevent.vk_down))
back = false;
}
现在,camera类正在跟踪按下了哪些键,我们可以开始更新播放器的位置。为此,我们将使用在game类的run方法中调用的update方法。在此过程中,我们将继续进行操作,并通过在game类中将地图传递给update方法时,将冲突检测添加到update方法中。更新方法如下所示:
public void update(int[][] map) {
if(forward) {
if(map[(int)(xpos + xdir * move_speed)][(int)ypos] == 0) {
xpos+=xdir*move_speed;
}
if(map[(int)xpos][(int)(ypos + ydir * move_speed)] ==0)
ypos+=ydir*move_speed;
}
if(back) {
if(map[(int)(xpos - xdir * move_speed)][(int)ypos] == 0)
xpos-=xdir*move_speed;
if(map[(int)xpos][(int)(ypos - ydir * move_speed)]==0)
ypos-=ydir*move_speed;
}
if(right) {
double oldxdir=xdir;
xdir=xdir*math.cos(-rotation_speed) - ydir*math.sin(-rotation_speed);
ydir=oldxdir*math.sin(-rotation_speed) + ydir*math.cos(-rotation_speed);
double oldxplane = xplane;
xplane=xplane*math.cos(-rotation_speed) - yplane*math.sin(-rotation_speed);
yplane=oldxplane*math.sin(-rotation_speed) + yplane*math.cos(-rotation_speed);
}
if(left) {
double oldxdir=xdir;
xdir=xdir*math.cos(rotation_speed) - ydir*math.sin(rotation_speed);
ydir=oldxdir*math.sin(rotation_speed) + ydir*math.cos(rotation_speed);
double oldxplane = xplane;
xplane=xplane*math.cos(rotation_speed) - yplane*math.sin(rotation_speed);
yplane=oldxplane*math.sin(rotation_speed) + yplane*math.cos(rotation_speed);
}
}
该方法中控制前进和后退运动的部分通过分别向xpos和ypos添加xdir和ydir来工作。在此动作发生之前,程序会检查该动作是否会将相机放置在墙内,如果可以,则不进行检查。对于旋转,方向矢量和平面矢量都乘以旋转矩阵,即:
[ cos(rotation_speed) -sin(rotation_speed) ]
[ sin(rotation_speed) cos(rotation_speed) ]
以获得其新值。完成update方法后,我们现在可以从game类中调用它。在game类的run方法中,添加以下代码行,在此处显示
add this:
camera.update(map);
in here:
while(running) {
long now = system.nanotime();
delta = delta + ((now-lasttime) / ns);
lasttime = now;
while (delta 》= 1)//make sure update is only happening 60 times a second
{
//handles all of the logic restricted time
camera.update(map);
delta--;
}
render();//displays to the screen unrestricted time
}
现在,完成了,我们终于可以进入最终类并计算屏幕了!
第4步:计算屏幕
在screen类中,大部分的计算都是为了使程序正常工作。要工作,该类需要以下导入:
import java.util.arraylist;
import java.awt.color;
实际的类是这样开始的:
public class screen {
public int[][] map;
public int mapwidth, mapheight, width, height;
public arraylist textures;
该地图与在游戏类。屏幕使用它来确定墙壁在哪里以及与玩家之间的距离。宽度和高度定义屏幕的大小,并且应始终与game类中创建的框架的宽度和高度相同。纹理是所有纹理的列表,以便屏幕可以访问纹理的像素。在声明了这些变量之后,必须像下面这样在构造函数中对其进行初始化:
public screen(int[][] m, arraylist tex, int w, int h) {
map = m;
textures = tex;
width = w;
height = h;
}
现在是时候编写类具有的一个方法了:update方法。更新方法根据用户在地图中的位置重新计算屏幕的外观。该方法被不断调用,并将更新后的像素数组返回给game类。该方法开始于“清除”屏幕。通过将上半部分的所有像素设置为一种颜色,并将下半部分的所有像素设置为另一种颜色来实现此目的。
public int[] update(camera camera, int[] pixels) {
for(int n=0; n if(pixels[n] != color.dark_gray.getrgb()) pixels[n] = color.dark_gray.getrgb();
}
for(int i=pixels.length/2; i if(pixels[i] != color.gray.getrgb()) pixels[i] = color.gray.getrgb();
}
让屏幕的顶部和底部为两个不同颜色也使它看起来好像有地板和天花板。清除像素阵列后,该是进行主要计算的时候了。该程序循环遍历屏幕上的每个垂直条,并投射光线以找出该垂直条上的屏幕上应该有什么墙。循环的开始看起来像这样:
for(int x=0; xdouble camerax = 2 * x / (double)(width) -1;
double raydirx = camera.xdir + camera.xplane * camerax;
double raydiry = camera.ydir + camera.yplane * camerax;
//map position
int mapx = (int)camera.xpos;
int mapy = (int)camera.ypos;
//length of ray from current position to next x or y-side
double sidedistx;
double sidedisty;
//length of ray from one side to next in map
double deltadistx = math.sqrt(1 + (raydiry*raydiry) / (raydirx*raydirx));
double deltadisty = math.sqrt(1 + (raydirx*raydirx) / (raydiry*raydiry));
double perpwalldist;
//direction to go in x and y
int stepx, stepy;
boolean hit = false;//was a wall hit
int side=0;//was the wall vertical or horizontal
这里发生的所有事情都是计算出循环其余部分将要使用的一些变量。 camerax是摄影机平面上当前垂直条纹的x坐标,并且raydir变量为射线创建矢量。计算所有以distx或disty结尾的变量,以便程序仅在可能发生碰撞的位置检查碰撞。 perpwalldist是从播放器到射线与之碰撞的第一堵墙的距离。这将在以后计算。完成此操作后,我们需要根据已经计算出的变量来找出其他一些变量。
//figure out the step direction and initial distance to a side
if (raydirx 《 0)
{
stepx = -1;
sidedistx = (camera.xpos - mapx) * deltadistx;
}
else
{
stepx = 1;
sidedistx = (mapx + 1.0 - camera.xpos) * deltadistx;
}
if (raydiry 《 0)
{
stepy = -1;
sidedisty = (camera.ypos - mapy) * deltadisty;
}
else
{
stepy = 1;
sidedisty = (mapy + 1.0 - camera.ypos) * deltadisty;
}
一旦完成,就该找出射线与何处碰撞了。一堵墙。为此,程序要经过一个循环,在该循环中检查射线是否与墙壁接触,如果没有,则移动到下一个可能的碰撞点,然后再次检查。
//loop to find where the ray hits a wall
while(!hit) {
//jump to next square
if (sidedistx 《 sidedisty)
{
sidedistx += deltadistx;
mapx += stepx;
side = 0;
}
else
{
sidedisty += deltadisty;
mapy += stepy;
side = 1;
}
//check if ray has hit a wall
if(map[mapx][mapy] 》 0) hit = true;
}
现在我们知道射线在何处撞击墙壁,我们可以开始计算墙壁在我们当前所在的垂直条纹中的外观。为此,我们首先计算到墙的距离,然后使用该距离来计算出墙在垂直条中应该有多高。然后,我们根据屏幕上的像素将该高度转换为起点和终点。代码如下所示:
//calculate distance to the point of impact
if(side==0)
perpwalldist = math.abs((mapx - camera.xpos + (1 - stepx) / 2) / raydirx);
else
perpwalldist = math.abs((mapy - camera.ypos + (1 - stepy) / 2) / raydiry);
//now calculate the height of the wall based on the distance from the camera
int lineheight;
if(perpwalldist 》 0) lineheight = math.abs((int)(height / perpwalldist));
else lineheight = height;
//calculate lowest and highest pixel to fill in current stripe
int drawstart = -lineheight/2+ height/2;
if(drawstart 《 0)
drawstart = 0;
int drawend = lineheight/2 + height/2;
if(drawend 》= height)
drawend = height - 1;
计算完之后,就该开始从墙的纹理中找出哪些像素会真正呈现给用户了。为此,我们首先必须确定与刚击中的墙关联的纹理,然后确定将向用户显示的像素的纹理的x坐标。
//add a texture
int texnum = map[mapx][mapy] - 1;
double wallx;//exact position of where wall was hit
if(side==1) {//if its a y-axis wall
wallx = (camera.xpos + ((mapy - camera.ypos + (1 - stepy) / 2) / raydiry) * raydirx);
} else {//x-axis wall
wallx = (camera.ypos + ((mapx - camera.xpos + (1 - stepx) / 2) / raydirx) * raydiry);
}
wallx-=math.floor(wallx);
//x coordinate on the texture
int texx = (int)(wallx * (textures.get(texnum).size));
if(side == 0 && raydirx 》 0) texx = textures.get(texnum).size - texx - 1;
if(side == 1 && raydiry 《 0) texx = textures.get(texnum).size - texx - 1;
通过获取在2d地图上击中墙壁的确切位置并减去整数值(仅保留小数)来计算x坐标。然后将此小数(wallx)乘以墙的纹理大小即可在我们希望绘制的像素的墙上获得确切的x坐标。一旦我们知道剩下要做的就是计算纹理上像素的y坐标并将其绘制在屏幕上。为此,我们遍历垂直条带中屏幕上的所有像素,然后对其进行计算并计算纹理上像素的确切y坐标。然后,使用该程序,程序将纹理中像素的数据写入屏幕上的像素阵列。该程序还使此处的水平墙比垂直墙暗,以提供基本的照明效果。
//calculate y coordinate on texture
for(int y=drawstart; y int texy = (((y*2 - height + lineheight) 《《 6) / lineheight) / 2;
int color;
if(side==0) color = textures.get(texnum).pixels[texx + (texy * textures.get(texnum).size)];
else color = (textures.get(texnum).pixels[texx + (texy * textures.get(texnum).size)]》》1) & 8355711;//make y sides darker
pixels[x + y*(width)] = color;
}
然后,screen类中剩下的就是返回像素数组
return pixels;
该类完成。现在,我们要做的就是在game类中添加几行代码以使屏幕正常运行。在变量顶部添加以下内容:
public screen screen;
,然后在构造函数中,在初始化纹理之后,将其添加到某处。
screen = new screen(map, mapwidth, mapheight, textures, 640, 480);
最后,在在camera.update(map)之前运行运行方法add
screen.update(camera, pixels);
。程序就完成了!
步骤5:最终代码


Linux系统编程--fcntl()读写锁实例
TP-Link11正式发布了自己的第一款802.11ax Wi-Fi路由器
解析丰田L4级自动驾驶计算平台
电子设备智能供应链协同解决方案: 赋能产业数字化升级
压注法封装贴片LED填补空白 达到国际领先水平
怎样用Java制作基本的3D引擎
美国PCB收到了什么的影响
路灯控制器怎么设置开关时间?
肿瘤疾病治疗新方法:借助硅纳米粒子进行光学诊断
看创新SoC网络平台Axxia如何为移动宽带提速
Modbus RTU连接YGWE通讯点表单双机头
2018Q2全球市场手机均价及涨幅,哪些手机品牌涨价最猛?
中国高铁和谐号问世全面实现中国制造业智能化的中心思想
怎样用废微波炉建造一个小型电风扇
三星或将于第三季度推出铰接式可折叠手机
树根互联打造中国的Predix,中国智能制造升级
小米9确定支持无线充电,并且速度还不慢
苹果全新Mac Pro改由中国广达生产
频谱共享是5G时代大趋势
充电桩安装需要什么样的条件