Skip to content

#1 Getting started with JPGE

John edited this page Nov 15, 2018 · 10 revisions

In this tutorial you will learn how to create a window and show a 3D cube inside of it.

First of all you need to add the .jar file of the engine to your project. It is located at /target/JPGE.jar in the repository.

If you are using eclipse:

right click you project > Properties > Java Build Path > Libraries > Add external jars

If not just search how add external libraries to your java project on the web.

Now that you have the .jar file in your project we are ready to go.

So let's begin creating a new class called SimpleCube. Then, in the constructor add the following code to create a window:

Engine.getInstance().setSceneWindow(new SceneWindow(320, 240));

Now let's create a cube:

// the engine works with vectors represented by integer arrays.
// that's why 'new int[3]' is used.
Transform cubeTransform = new Transform(new int[3], new int[3], new int[] {1,1,1});
Mesh cubeMesh = null;
try {
	// load mesh from resources using the SOMImporter.
	// the Engine already provides some simple meshes.
	cubeMesh = SOMImporter.load(getClass().getResourceAsStream("/cube.som"));
} catch (ImportExeption e) {
	e.printStackTrace();
}
SceneObject cube = new SceneObject("Cube", cubeTransform, cubeMesh);

To see you cube you will need a camera, to create one just do this:

// If you thinking 'why is the value in the int[] so high?'.
// Its because the engine only uses fixed point/ integer math.
// But precision is needed and the easiest way to get it is by using higher values.
// (8000 is more precise than just 8) 
Transform cameraTransform = new Transform(new int[]{0,0,-8000}, new int[3], new int[3]);
Camera camera = new Camera("Camera", cameraTransform, 0, 0, 320, 240);

Then add the cube and the camera to the scene:

Engine.getInstance().getScene().addSceneObject(cube);
Engine.getInstance().getScene().addCamera(camera);

And to adjust the rendering size of the engine to the window size:

Engine.getInstance().getRenderBuffer().setSize(320, 240);

And now we have this:

import com.johnsproject.jpge.Engine;
import com.johnsproject.jpge.dto.Camera;
import com.johnsproject.jpge.dto.Mesh;
import com.johnsproject.jpge.dto.SceneObject;
import com.johnsproject.jpge.dto.Transform;
import com.johnsproject.jpge.graphics.SceneWindow;
import com.johnsproject.jpge.io.ImportExeption;
import com.johnsproject.jpge.io.SOMImporter;

public class SimpleCube {
	
	public static void main(String[] args) {
		new SimpleCube();
	}

	public SimpleCube() {
		Engine.getInstance().setSceneWindow(new SceneWindow(320, 240));
		// the engine works with vectors represented by integer arrays.
		// that's why 'new int[3]' is used.
		Transform cubeTransform = new Transform(new int[3], new int[3], new int[] {1,1,1});
		Mesh cubeMesh = null;
		try {
			// load mesh from resources using the SOMImporter.
			// the Engine already provides some simple meshes.
			cubeMesh = SOMImporter.load(getClass().getResourceAsStream("/cube.som"));
		} catch (ImportExeption e) {
			e.printStackTrace();
		}
		SceneObject cube = new SceneObject("Cube", cubeTransform, cubeMesh);
		// If you thinking 'why is the value in the int[] so high?'.
		// Its because the engine only uses fixed point/ integer math.
		// But precision is needed and the easiest way to get it is by using higher values.
		// (8000 is more precise than just 8) 
		Transform cameraTransform = new Transform(new int[]{0,0,-8000}, new int[3], new int[3]);
		Camera camera = new Camera("Camera", cameraTransform, 0, 0, 320, 240);
		Engine.getInstance().getScene().addSceneObject(cube);
		Engine.getInstance().getScene().addCamera(camera);
		Engine.getInstance().getRenderBuffer().setSize(320, 240);
	}
}

Oh I nearly forgot about it! the cube will probably fall because of physics.

If you want to stop the cube:

cube.getRigidbody().useGravity(false);
Clone this wiki locally