Tag Archives: entities

LWJGL Entities

By the end of this tutorial you will have learned how to leverage the power of object-oriented programming to create a game engine containing game entities.

The story behind LWJGL entities

Instead of looking at the scene as a bunch of shapes, look at the scene as a bunch of objects. Each object has material properties, a bounding box for collision and an amount of transparency for instance. Furthermore, each object can be classified into more abstract groups or categories. For example: a football (or soccer ball), a basketball, and a tennis ball are all balls. They all share the same properties of a ball, being round, but they also differ. Utilising this approach makes managing an object pool straightforward.

LWJGL Entity interface

This is the parent interface for the entity.

public interface Entity2D {
	public float getX();
	public float getY();
        public void setX(float x);
        public void setY(float y);
        public void setLocation(float x, float y);
	public void setUp();
	public void destroy();
	public void draw();
}

It is still very basic and generic. Collision detection would be too specific to add here. It would be better to add a CollidableEntity2D down the line.

Abstract classes – being lazy

When a lazy approach is desired, create an abstract class for the aforementioned interface and implement the position methods and leave the remaining methods abstract.

public abstract class AbstractEntity2D implements Entity2D {
	protected float x;
	protected float y;
	public float getX() { return x; }
	public float getY() { return y; }
        public void setX(float x) { this.x = x; }
        public void setY(float y) { this.y = y; }
        public void setLocation(float x, float y) {
    		this.x = x;
    		this.y = y;
        }
	public abstract void setUp();
	public abstract void destroy();
	public abstract void draw();
}

Implementing the interface fully

It is now possible to easily create a class derived from the ‘Entity2D’ interface.

public class Box2D extends AbstractEntity2D {
	protected float size;
	public Box2D(float x, float y, float size) {
		this.x = x;
		this.y = y;
		this.size = size;
	}
	public Box2D() {
		this.x = this.y = this.size = 0;
	}
	@Override
	public void setUp() {
		// We don't need anything here for a box
	}
	@Override
	public void destroy() {
		// We don't need anything here for a box
	}
	@Override 
	public void draw() {
		glBegin(GL_TRIANGLES);
		glVertex2f(x + size/2, y + size/2);
		glVertex2f(x + size/2, y - size/2);
		glVertex2f(x - size/2, y - size/2);
		glVertex2f(x - size/2, y - size/2);
		glVertex2f(x - size/2, y + size/2);
		glVertex2f(x + size/2, y + size/2);
		glEnd();
	}
}

Using the classes for the LWJGL Entities in code

To create an instance of ‘Entity2D’, write the following code.

// Before game loop
Entity2D box = new Box2D(30.0f, 50.0f, 25.0f);
box.setUp();
// In game loop 
box.draw();
// After game loop
box.destroy();

Note that even though the ‘setUp()’ and ‘destroy()’ methods do not appear necessary, they are still called. The reason is that in the future resource handling might be put there.

Conclusion

This concludes this tutorial. Should you have any questions or remarks, I would love to see them in the comments section below.