use of com.almasb.fxgl.physics.box2d.common.Rotation in project FXGL by AlmasB.
the class CircleShape method raycast.
// Collision Detection in Interactive 3D Environments by Gino van den Bergen
// From Section 3.1.2
// x = s + a * r
// norm(x) = radius
@Override
public boolean raycast(RayCastOutput output, RayCastInput input, Transform transform, int childIndex) {
final Vec2 inputp1 = input.p1;
final Vec2 inputp2 = input.p2;
final Rotation tq = transform.q;
final Vec2 tp = transform.p;
// Rot.mulToOutUnsafe(transform.q, m_p, position);
// position.addLocal(transform.p);
final float positionx = tq.c * center.x - tq.s * center.y + tp.x;
final float positiony = tq.s * center.x + tq.c * center.y + tp.y;
final float sx = inputp1.x - positionx;
final float sy = inputp1.y - positiony;
// final float b = Vec2.dot(s, s) - radius * radius;
final float b = sx * sx + sy * sy - getRadius() * getRadius();
// Solve quadratic equation.
final float rx = inputp2.x - inputp1.x;
final float ry = inputp2.y - inputp1.y;
// final float c = Vec2.dot(s, r);
// final float rr = Vec2.dot(r, r);
final float c = sx * rx + sy * ry;
final float rr = rx * rx + ry * ry;
final float sigma = c * c - rr * b;
// Check for negative discriminant and short segment.
if (sigma < 0.0f || rr < JBoxSettings.EPSILON) {
return false;
}
// Find the point of intersection of the line with the circle.
float a = -(c + FXGLMath.sqrtF(sigma));
// Is the intersection point on the segment?
if (0.0f <= a && a <= input.maxFraction * rr) {
a /= rr;
output.fraction = a;
output.normal.x = rx * a + sx;
output.normal.y = ry * a + sy;
output.normal.getLengthAndNormalize();
return true;
}
return false;
}
use of com.almasb.fxgl.physics.box2d.common.Rotation in project FXGL by AlmasB.
the class CircleShape method containsPoint.
@Override
public boolean containsPoint(final Transform transform, final Vec2 point) {
// Rot.mulToOutUnsafe(transform.q, m_p, center);
// center.addLocal(transform.p);
//
// final Vec2 d = center.subLocal(p).negateLocal();
// return Vec2.dot(d, d) <= radius * radius;
final Rotation q = transform.q;
final Vec2 tp = transform.p;
float centerx = -(q.c * center.x - q.s * center.y + tp.x - point.x);
float centery = -(q.s * center.x + q.c * center.y + tp.y - point.y);
return centerx * centerx + centery * centery <= getRadius() * getRadius();
}
Aggregations