Skip to content

Commit

Permalink
Draft ChainShape support for dynamic shadows
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 353ca3f commit 4248c63
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/box2dLight/PointLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.ChainShape;
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;
import com.badlogic.gdx.physics.box2d.Shape.Type;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.IntArray;

Expand Down Expand Up @@ -106,20 +108,32 @@ protected void updateDynamicShadowMeshes() {
if (data == null) continue;

Shape fixtureShape = fixture.getShape();
Type type = fixtureShape.getType();
center.set(fixture.getBody().getWorldCenter());
float l = 0f;
float f = 1f / data.shadowsDropped;
if (fixtureShape instanceof PolygonShape) {
PolygonShape shape = (PolygonShape)fixtureShape;
if (type == Type.Polygon || type == Type.Chain) {
boolean isPolygon = (type == Type.Polygon);
ChainShape cShape = isPolygon ?
null : (ChainShape)fixtureShape;
PolygonShape pShape = isPolygon ?
(PolygonShape)fixtureShape : null;
int vertexCount = isPolygon ?
pShape.getVertexCount() :
cShape.getVertexCount();
int size = 0;
int minN = -1;
int maxN = -1;
int minDstN = -1;
float minDst = Float.POSITIVE_INFINITY;
boolean hasGasp = false;
tmpVerts.clear();
for (int n = 0; n < shape.getVertexCount(); n++) {
shape.getVertex(n, tmpVec);
for (int n = 0; n < vertexCount; n++) {
if (isPolygon) {
pShape.getVertex(n, tmpVec);
} else {
cShape.getVertex(n, tmpVec);
}
tmpVec.set(fixture.getBody().getWorldPoint(tmpVec));
tmpVerts.add(tmpVec.cpy());
tmpEnd.set(tmpVec).sub(start).limit(0.01f).add(tmpVec);
Expand All @@ -138,11 +152,10 @@ protected void updateDynamicShadowMeshes() {

ind.clear();
if (!hasGasp) {
shape.getVertex(minDstN, tmpVec);
tmpVec.set(fixture.getBody().getWorldPoint(tmpVec));
tmpVec.set(tmpVerts.get(minDstN));
boolean correctDirection = Intersector.pointLineSide(
start, center, tmpVec) < 0;
for (int n = minDstN; n < shape.getVertexCount(); n++) {
for (int n = minDstN; n < vertexCount; n++) {
ind.add(n);
}
for (int n = 0; n < minDstN; n++) {
Expand All @@ -158,7 +171,7 @@ protected void updateDynamicShadowMeshes() {
for (int n = minN - 1; n > -1; n--) {
ind.add(n);
}
for (int n = shape.getVertexCount() - 1; n > maxN ; n--) {
for (int n = vertexCount - 1; n > maxN ; n--) {
ind.add(n);
}
}
Expand Down Expand Up @@ -200,7 +213,7 @@ protected void updateDynamicShadowMeshes() {
new VertexAttribute(Usage.Generic, 1, "s"));
mesh.setVertices(dynamicSegments, 0, size);
dynamicShadowMeshes.add(mesh);
} else if (fixtureShape instanceof CircleShape) {
} else if (type == Type.Circle) {
CircleShape shape = (CircleShape)fixtureShape;
int size = 0;
float r = shape.getRadius();
Expand Down

0 comments on commit 4248c63

Please sign in to comment.