Java 3DS!

Avi Gupta
hackerLog
Published in
6 min readSep 21, 2019

“Never dig straight down.”

-The first rule of Minecraft

Minecraft is a game that was made using Java. A special kind of Java: Java 3DS. Java 3DS is what lets you create 3D objects in Java. Seems pretty obvious, right?

Courtesy of KnowYourMeme.

It is simple at first glance, yes, but as you go deeper into it, it gets harder and harder.

In fact, it was used to create Minecraft.

Courtesy of Minecraft on Gamepedia.

The BASICs

First, we’ll start with a simple box.

We set the program up first:

void setup() {
size(600, 600, P3D); // P3D means that the program adds a Z axis, making it 3D.
}

Then, we make the box.

void draw() {
background(20);
pushMatrix();
translate(width/2, height/2);// sets the "position" for the box
box(30,20,100);// makes a rectangular prism (box)
popMatrix();// Makes the grid go back to its original place.
}

The end result looks like this:

A rectangle.

It looks like a rectangle.

So let’s change:

translate(width/2, height/2);

to this:

translate(50, 50);

moving it to a corner.

If we play that with the edited line…

Now it’s a box!

So the so-called “camera” is at the center of the canvas. The more to the edges we move the boxes, the more 3D it seems.

We can even make spheres.

The void draw() code should look like this for a sphere:

void draw() {
background(20);
pushMatrix();
translate(width/2, height/2);
sphere(100);//makes a sphere
popMatrix();
}

The end result should look like this:

A sphere.

(The lines are to mark the mesh that the sphere is made out of.)

We can also rotate objects along the X, Y, and Z axises.

To rotate along the X axis, change your void draw() code to this:

void draw() {
background(20);
pushMatrix();
translate(width/2, height/2);
rotateX(20);// rotates along the x axis by 20 degrees. you MUST make sure that this command is before the box command.
box(50,50,50);
popMatrix();
}

It should look like this:

A cube rotated along the X axis by 20 degrees.

Y axis is also similar to the X axis code.

void draw() {
background(20);
pushMatrix();
translate(width/2, height/2);
rotateY(20);
box(50,50,50);
popMatrix();
}

It should look like this:

A cube rotated along the Y axis by 20 degrees.

And for along the Z axis…

void draw() {
background(20);
pushMatrix();
translate(width/2, height/2);
rotateZ(20);
box(50,50,50);
popMatrix();
}

The output looks like this:

A cube rotated along the Z axis by 20 degrees.

Cool, right?

Now, what if we added a texture? As in, an image?

The code for this is different. Instead of having a void setup{} and a void draw{} function, we put everything inside the void setup{} function. We also need to code separate vertexes instead of using the box() function.

We now introduce a new variable type: PImage, which probably stands for “Processing Image.” This is used to upload an Image to processing. But how do we do that?

You see, each Processing project is created as a folder, not a single program, so you can upload stuff to each individual project (pictures, videos, sounds, ect.) The program is included in that folder.

So, here’s what you do for the mac:

  1. Open Finder.
  2. Search up your project name in the search bar.
  3. Click the folder that your project is in.
  4. Search up the texture that you want.
  5. Upload the texture to the folder.

It’s a similar process for Windows, too.

For my texture, I used this image:

Courtesy of Planet Minecraft.

This is the dirt block texture in Minecraft. Link to the texture here. I called it “DirtBlock.jpg”.

Now, this is what we do:

First, we declare the void setup(){ part and also the size .

void setup() {
size(600, 600, P3D);
noStroke();//there to prevent any strokes

Next, we upload the image to the program.

 PImage img = loadImage("DirtBlock.jpg");

After that, we don’t use regular polygons. Instead, we use vertexes.

beginShape();
texture(img)

Now, normally, a vertex has three variables: x, y, and z. But for our case, it has 4. Why? You see, this is to scale the image to our proportions. the last two variables, v and u, describe where the relative point on the image should go on the shape.

It’s also important that you get the vertexes in a circular order for making a square — otherwise, the shape appears like an hourglass.

vertex(200,200,0,0);
vertex(400,200,512,0);
vertex(400,400,512,512);
vertex(200,400,0,512);
endShape();//ends the shape.
}

This is the output:

The texture on a shape.

Can we make it 3d? Yes, we can!

replace all of the vertex lines with this:

 vertex(200,200,0,0);
vertex(400,200,100,512,0);
vertex(400,400,100,512,512);
vertex(200,400,0,512);

This is the output:

The texture on a more 3D shape.

It seems as if we have found out a way to recreate a Minecraft block.

But we’ll get to that later.

Lights! Camera! Not that much Action!

In J3DS, you can recreate lights. Huh? Huh? HUUUUUH?

It is pretty cool when you think about it, because that involves ray tracing. Actually it doesn’t — it uses ray marching. (I don’t wanna get super detailed into those — so I would probably recommend you to search it up.)
You can create different kinds of lights: pointLight(), ambientLight() , directionalLight() , spotLight(), or just plain lights() . We will go into the different lights, and how they work. Also, this means that if you use a light in you code, then the not illuminated parts will be black. For the best results, we will use a sphere.

void setup() {
size(600, 600, P3D);
noStroke();
background(20);
lights();
sphereDetail(10); //this specifies how many faces the sphere has.
// the higher the detail value, the more spherical it appears.
}

The lights(); command lets you add lights to the program.

Then, we start drawing.

void draw() {

after that, we add a pointLight();. A point light is a light that spreads in all directions. We use mouseX and mouseY to move the light along with the mouse.

 pointLight(0, 100, 255, mouseX, mouseY, 50);

Then, we use pushMatrix(); and popMatrix(); to shift the matrix and put it back so that we can place the sphere. (The sphere(); function doesn’t have an x and y variable in its parameters.)

  pushMatrix();
translate(width/2, height/2, -150);
fill(255);
sphere(100);
popMatrix();
}

The output should look like this:

A sphere with a light shining on it.

Pretty simple, right?

Let’s try out some other lights. For example, ambientLight(). This function lights up the whole stage, so everything will appear to be lit up.

This was the code for the light:

ambientLight(0, 100, 255, mouseX, mouseY, -155);

This was the output:

A sphere with ambient light.

spotLight(); is more complex. It has an angle, rotations along each axis, and a concentration. Because it directs its light in a certain spot, I had to use a fixed position.

this is the code for the light:

spotLight(0,100,255, width/2, height/2, 400, 0, 0, -1, PI, 100);

this is the output:

sphere with spotlight on it.

But what happens if I change the last parameter, the intensity? If we increase the intensity, the light becomes fixed on a smaller space.

This is the code:

spotLight(0,100,255, width/2, height/2, 400, 0, 0, -1, PI, 1000);

This is the output:

The spotlight with a higher intensity.

See?

This is pretty much all you need to know about J3DS for now.

Back to Minecraft

All of these principles are used in Minecraft. Textures, cubes, and lighting are all part of it. The spheres? Nah, probably not. Minecraft is famous for its blockiness and grass blocks.

Speaking of …
Minecraft has many different types of grass block textures, based on the biome that it is in. To see them all, click here.

Happy coding and crafting!

--

--

Avi Gupta
hackerLog

Named after the first 2 games I got on my xBox, Forza and Minecraft. Also, i have a blog. Real name is Avi.