Lighting Engine - UQdeco2800/2021-studio-6 GitHub Wiki

Quick intro

The lighting engine works in a very similar way to the Renderer or Physics engine, for you to interact or use the lighting engine you attach a component to an entity. Currently the only component to attach is the PointLightComponent which will add a point light to the center of the entity, there are more types of lights check out [1] to see whats available (currently these are only written descriptions not visual demos).

The PointLightComponent and any further components are essentially just wrappers around the box2dLight library. There are more lighting options to explore and also shaders if we get to that.

[1]http://javadox.com/com.badlogicgames.box2dlights/box2dlights/1.4/box2dLight/package-summary.html

https://github.com/libgdx/box2dlights

Examples:

Point Light

Creates a simple point light which emits light from a point

Point Light Example

Code:

(on your desired Entity, obviously customise the variables to your liking)
.addComponent(new PointLightComponent(Colors.get("RED"), 10f, 0, 0));

Cone Light

Creates a cone of light, it has a direction, and coneDegree

Cone Light Example

Code

(on your desired Entity)
.addComponent(new ConeLightComponent(Colors.get("RED"), 10f, 0, 0, 0, 45f) );

Chain Light

This creates a chain of light all pointing in a direction this one is the most complicated because you have to give the component a float[] which represent (x, y) points. for example [x1, y1, x2, y2, ...], The float array has to have at least 4 numbers because to have a chain your must have two points. I made two examples for this:

Circle Chain

Chain Light Circle Example

Code for generating circle chain:

Circle algorithm go here for an explanation https://www.mathopenref.com/coordcirclealgorithm.html

// use an ArrayList because its very easy to append to an array list
ArrayList<Float> prepwave =  new ArrayList<>();
float h = 0.5f;
float r = 2;
float k = 0.5f;
//generate (x, y) points for the circle
for(float theta=0;  theta < 2*Math.PI + 0.1;  theta+=0.05){ 
    prepwave.add((float) (h + r*Math.cos(theta)));
    prepwave.add((float) (k - r*Math.sin(theta))); 
}

//convert ArrayList to float[], because ChainLight expects a float[] not an ArrayList
float[] wave = new float[prepwave.size()];
int i = 0;
for (Float f : prepwave) {
    wave[i++] = f; // Or whatever default you want.
}

(On desired entity)
.addComponent(new ChainLightComponent(Colors.get("ORANGE"), 10f, 1, wave) );

Sine Wave Chain

Create a weird sine wave wall

Chain Sine Wave Example

Code

ArrayList<Float> prepwave =  new ArrayList<>();
for (float i = -10; i < 10; i += 0.1) {
  prepwave.add(MathUtils.sin(i));
  prepwave.add(i);
}

float[] wave = new float[prepwave.size()];
int i = 0;

for (Float f : prepwave) {
  wave[i++] = f; // Or whatever default you want.
}

(On desired entity)
.addComponent(new ChainLightComponent(Colors.get("ORANGE"), 10f, 1, wave) );

I hope these examples highlight what is possible with the lighting engine

⚠️ **GitHub.com Fallback** ⚠️