Skip to content

Commit

Permalink
Draft CircleShape support for dynamic shadow
Browse files Browse the repository at this point in the history
Working on libgdx#40
  • Loading branch information
rinold committed Feb 5, 2015
1 parent 3173078 commit 2378081
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions src/box2dLight/PointLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.Shape;
Expand Down Expand Up @@ -103,11 +104,11 @@ protected void updateDynamicShadowMeshes() {
for (Fixture fixture : affectedFixtures) {
LightData data = (LightData)fixture.getUserData();
Shape fixtureShape = fixture.getShape();
center.set(fixture.getBody().getWorldCenter());
if (fixtureShape instanceof PolygonShape) {
PolygonShape shape = (PolygonShape)fixtureShape;
int size = 0;
float l;
center.set(fixture.getBody().getWorldCenter());
float f = 1f / data.shadowsDropped;
int minN = -1;
int maxN = -1;
Expand Down Expand Up @@ -162,7 +163,6 @@ protected void updateDynamicShadowMeshes() {

for (int k = 0; k < ind.size; k++) {
int n = ind.get(k);

tmpVec.set(tmpVerts.get(n));

float dst = tmpVec.dst(start);
Expand Down Expand Up @@ -198,6 +198,57 @@ protected void updateDynamicShadowMeshes() {
new VertexAttribute(Usage.Generic, 1, "s"));
mesh.setVertices(dynamicSegments, 0, size);
dynamicShadowMeshes.add(mesh);
} else if (fixtureShape instanceof CircleShape) {
CircleShape shape = (CircleShape)fixtureShape;
int size = 0;
float r = shape.getRadius();
float dst = tmpVec.set(center).dst(start);
float a = (float)Math.acos(r/dst) * MathUtils.radDeg;
float l;
if (height > data.height) {
l = dst * data.height / (height - data.height);
float diff = distance - dst;
if (l > diff) l = diff;
} else if (height == 0f) {
l = distance;
} else {
l = distance - dst;
}
if (l < 0) l = 0f;

tmpVec.set(start).sub(center).clamp(r, r).rotate(a);
tmpStart.set(center).add(tmpVec);
dynamicSegments[size++] = tmpStart.x;
dynamicSegments[size++] = tmpStart.y;
dynamicSegments[size++] = colBits;
dynamicSegments[size++] = 1f;

tmpEnd.set(tmpStart).sub(start).limit(l).add(tmpStart);
dynamicSegments[size++] = tmpEnd.x;
dynamicSegments[size++] = tmpEnd.y;
dynamicSegments[size++] = colBits;
dynamicSegments[size++] = 1f;

tmpVec.rotate(-2f*a);
tmpStart.set(center).add(tmpVec);
dynamicSegments[size++] = tmpStart.x;
dynamicSegments[size++] = tmpStart.y;
dynamicSegments[size++] = colBits;
dynamicSegments[size++] = 1f;

tmpEnd.set(tmpStart).sub(start).limit(l).add(tmpStart);
dynamicSegments[size++] = tmpEnd.x;
dynamicSegments[size++] = tmpEnd.y;
dynamicSegments[size++] = colBits;
dynamicSegments[size++] = 1f;

Mesh mesh = new Mesh(
VertexDataType.VertexArray, staticLight, size / 4, 0,
new VertexAttribute(Usage.Position, 2, "vertex_positions"),
new VertexAttribute(Usage.ColorPacked, 4, "quad_colors"),
new VertexAttribute(Usage.Generic, 1, "s"));
mesh.setVertices(dynamicSegments, 0, size);
dynamicShadowMeshes.add(mesh);
}
}
}
Expand Down

0 comments on commit 2378081

Please sign in to comment.