Hi, i'm new to this forum. First of all thanks to all the developers for this great physics engine. 🙂
Anyway, I was having some problems with raycasting and circle shapes: if you try to raycast from a point inside a circle bounding box that actually misses the circle, you get an immediate collide.
I was already updated to the last SVN version and also searched through most of the forum to find similar problems.
The piece of code I changed to get things working was in org.jbox2d.collision.shapes.CircleShape testSegment method:
I obviously changed the last line in:
Hope not to have put up an already known bug.
Happy New Year to everyone!
Francesco
Anyway, I was having some problems with raycasting and circle shapes: if you try to raycast from a point inside a circle bounding box that actually misses the circle, you get an immediate collide.
I was already updated to the last SVN version and also searched through most of the forum to find similar problems.
The piece of code I changed to get things working was in org.jbox2d.collision.shapes.CircleShape testSegment method:
Code:
// Does the segment start inside the circle?
if (b < 0.0f) {
return SegmentCollide.STARTS_INSIDE_COLLIDE;
}
Vec2 r = tlR.get();
// Solve quadratic equation.
r.set(segment.p2).subLocal(segment.p1);
final float c = Vec2.dot(s, r);
final float rr = Vec2.dot(r, r);
final float sigma = c * c - rr * b;
// Check for negative discriminant and short segment.
if (sigma <= 0.0f || rr < Settings.EPSILON) {
return SegmentCollide.MISS_COLLIDE;
}
// Find the point of intersection of the line with the circle.
float a = -(c + MathUtils.sqrt(sigma));
// System.out.println(a + "; " + maxLambda + "; " + rr + " ; " + (a <= maxLambda * rr));
// Is the intersection point on the segment?
if (0.0f <= a && a <= maxLambda * rr) {
// System.out.println("Got here");
a /= rr;
out.lambda = a;
out.normal.set(r).mulLocal(a).addLocal(s);
out.normal.normalize();
return SegmentCollide.HIT_COLLIDE;
}
return SegmentCollide.HIT_COLLIDE;
if (b < 0.0f) {
return SegmentCollide.STARTS_INSIDE_COLLIDE;
}
Vec2 r = tlR.get();
// Solve quadratic equation.
r.set(segment.p2).subLocal(segment.p1);
final float c = Vec2.dot(s, r);
final float rr = Vec2.dot(r, r);
final float sigma = c * c - rr * b;
// Check for negative discriminant and short segment.
if (sigma <= 0.0f || rr < Settings.EPSILON) {
return SegmentCollide.MISS_COLLIDE;
}
// Find the point of intersection of the line with the circle.
float a = -(c + MathUtils.sqrt(sigma));
// System.out.println(a + "; " + maxLambda + "; " + rr + " ; " + (a <= maxLambda * rr));
// Is the intersection point on the segment?
if (0.0f <= a && a <= maxLambda * rr) {
// System.out.println("Got here");
a /= rr;
out.lambda = a;
out.normal.set(r).mulLocal(a).addLocal(s);
out.normal.normalize();
return SegmentCollide.HIT_COLLIDE;
}
return SegmentCollide.HIT_COLLIDE;
I obviously changed the last line in:
Code:
return SegmentCollide.MISS_COLLIDE;
.Hope not to have put up an already known bug.
Happy New Year to everyone!
Francesco