Skip to content

Commit

Permalink
Radial-smoothed dynamic shadows for CircleShapes
Browse files Browse the repository at this point in the history
Working on libgdx#40
  • Loading branch information
rinold committed Feb 6, 2015
1 parent 6b748a8 commit f188dd5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
43 changes: 19 additions & 24 deletions src/box2dLight/PointLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,34 +200,29 @@ protected void updateDynamicShadowMeshes() {
CircleShape shape = (CircleShape)fixtureShape;
float r = shape.getRadius();
float dst = tmpVec.set(center).dst(start);
float a = (float)Math.acos(r/dst) * MathUtils.radDeg;
float a = (float) Math.acos(r/dst);
l = data.getLimit(dst, height, distance);

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

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

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

tmpEnd.set(tmpStart).sub(start).limit(l).add(tmpStart);
dynamicSegments[size++] = tmpEnd.x;
dynamicSegments[size++] = tmpEnd.y;
dynamicSegments[size++] = colBits;
dynamicSegments[size++] = f;
float angle = (MathUtils.PI2 - 2f * a) /
RayHandler.CIRCLE_APPROX_POINTS;
for (int k = 0; k < RayHandler.CIRCLE_APPROX_POINTS; k++) {
tmpStart.set(center).add(tmpVec);
dynamicSegments[size++] = tmpStart.x;
dynamicSegments[size++] = tmpStart.y;
dynamicSegments[size++] = colBits;
dynamicSegments[size++] = f;

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

tmpVec.rotateRad(angle);
}
} else if (type == Type.Edge) {
EdgeShape shape = (EdgeShape)fixtureShape;

Expand Down
5 changes: 5 additions & 0 deletions src/box2dLight/RayHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public class RayHandler implements Disposable {

static boolean gammaCorrection = false;
static float gammaCorrectionParameter = 1f;

/** TODO: This could be made adaptive to ratio of camera sizes * zoom
* vs the CircleShape radius - thus will provide smooth radial shadows
* while resizing and zooming in and out */
static int CIRCLE_APPROX_POINTS = 32;

/** if this is public why we have a setter?
* TODO: remove public modifier and add getter
Expand Down

0 comments on commit f188dd5

Please sign in to comment.