raycast
fun raycast(fromX: Float, fromY: Float, toX: Float, toY: Float, hit: RayHit, targets: QueryTargets = QueryTargets.EVERYTHING): Boolean(source)
Returns whether the ray from (fromX, fromY) to (toX, toY) hits anything of targets, and writes the first hit into hit: what it hit, the point, the surface's normal there and how far along the ray.
Rigid bodies are hit on their shapes, soft bodies on their outlines, and the solids on their open faces; sensors are not hit. Something the ray starts inside is not hit: a ray from a surface going out does not hit that surface, so a reflected ray can start where the last one hit. Allocates nothing.
Samples
val world = PhysicsWorld()
val wall = world.rigidBody(Shape.box(10f, 100f), x = 100f, y = 0f, kind = BodyKind.STATIC)
// Make one RayHit and keep it: a raycast then allocates nothing.
val hit = RayHit()
val found = world.raycast(fromX = 0f, fromY = 0f, toX = 200f, toY = 0f, hit = hit, targets = QueryTargets.RIGID_BODIES)
assertPrints(found, "true")
assertPrints(hit.kind, "RIGID_BODY")
assertPrints(hit.rigidBodyOrNull == wall, "true")
// It hit the wall's left face, at x = 95, facing back towards the ray's start.
assertPrints(hit.x, "95.0")
assertPrints(hit.normalX, "-1.0")
assertPrints(world.raycast(0f, 0f, 50f, 0f, hit), "false")
assertPrints(hit.kind == HitKind.NOTHING, "true")Content copied to clipboard