Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added checks to prevent box2d crashing #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/box2dLight/ChainLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ protected void updateMesh() {
my[i] = tmpEnd.y;
tmpStart.x = startX[i];
tmpStart.y = startY[i];
if (rayHandler.world != null && !xray) {
if (rayHandler.world != null && !xray && !tmpStart.equals(tmpEnd)) {
rayHandler.world.rayCast(ray, tmpStart, tmpEnd);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/box2dLight/DirectionalLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void update () {
mx[i] = end[i].x = steppedX + xAxelOffSet;
my[i] = end[i].y = steppedY + yAxelOffSet;

if (rayHandler.world != null && !xray) {
if (rayHandler.world != null && !xray && !start[i].equals(end[i])) {
rayHandler.world.rayCast(ray, start[i], end[i]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/box2dLight/PositionalLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ protected void updateMesh() {
mx[i] = tmpEnd.x;
tmpEnd.y = endY[i] + start.y;
my[i] = tmpEnd.y;
if (rayHandler.world != null && !xray) {
if (rayHandler.world != null && !xray && !start.equals(tmpEnd)) {
rayHandler.world.rayCast(ray, start, tmpEnd);
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/box2dLight/SmoothConeLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ protected void updateMesh() {
// shoot check each ray
for (int i = 0; i < currentRayNum; i++) {
// rayCast is not async, so we can do that
rayHandler.world.rayCast(perfectRay, start, current = rays[i]);
if (!rays[i].equals(start)) {
current = rays[i];
rayHandler.world.rayCast(perfectRay, start, current);
}
}
if (currentRayNum >= baseRayNum) {
// rotate all the rays so we always can be sorter properly
Expand Down
4 changes: 3 additions & 1 deletion src/box2dLight/SmoothLineLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,9 @@ private void updateMesh () {
for (int i = 0; i < currentRayNum; i++) {
currentId = i;
LineRay ray = lineRays[i];
rayHandler.world.rayCast(rayCB, ray.start, ray.end);
if (!ray.start.equals(ray.end)) {
rayHandler.world.rayCast(rayCB, ray.start, ray.end);
}
}

// we need to sort if stuff was added to set the mesh properly
Expand Down
5 changes: 4 additions & 1 deletion src/box2dLight/SmoothPointLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ protected void updateMesh() {
// shoot check each ray
for (int i = 0; i < currentRayNum; i++) {
// rayCast is not async, so we can do that
rayHandler.world.rayCast(perfectRay, start, current = rays[i]);
if (!rays[i].equals(start)) {
current = rays[i];
rayHandler.world.rayCast(perfectRay, start, current);
}
}

// we need to sort if stuff was added to set the mesh properly
Expand Down
14 changes: 14 additions & 0 deletions src/box2dLight/SmoothPositionalLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.NumberUtils;

import java.util.Comparator;

Expand Down Expand Up @@ -450,6 +451,19 @@ public void reset () {
fraction = 1;
}

@Override
public boolean equals (Object obj) {
// no class checking here

if (this == obj) return true;
if (obj == null) return false;

Vector2 other = (Vector2)obj;
if (NumberUtils.floatToIntBits(x) != NumberUtils.floatToIntBits(other.x)) return false;
if (NumberUtils.floatToIntBits(y) != NumberUtils.floatToIntBits(other.y)) return false;
return true;
}

@Override public String toString () {
return String.format("Ray([%.2f, %.2f] a=%.2f, f=%.2f)", x, y, angle * MathUtils.radDeg, fraction);
}
Expand Down