RayHit

class RayHit(source)

Where a ray first hit something, filled in by PhysicsWorld.raycast. Make one and keep it, so a raycast allocates nothing.

toString gives what it hit and where, for example RayHit(RIGID_BODY, RigidBody(2), at=(120.0, 300.0), normal=(0.0, -1.0), fraction=0.4).

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")

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard

Distance along the ray where it hit: 0 at its start, 1 at its end.

Link copied to clipboard

Whether it hit anything.

Link copied to clipboard

What it hit, or HitKind.NOTHING.

Link copied to clipboard

Normal of the surface where it hit, across: a unit long with normalY, pointing back towards the ray's start.

Link copied to clipboard

Normal of the surface where it hit, down: see normalX.

Link copied to clipboard

The rigid body it hit, or null when it hit something else or nothing.

Link copied to clipboard

The shape of the rigid body it hit, or null when it hit something else or nothing.

Link copied to clipboard

The soft body it hit, or null when it hit something else or nothing.

Link copied to clipboard

The solid it hit, or null when it hit something else or nothing.

Link copied to clipboard
var x: Float

Position across where it hit.

Link copied to clipboard
var y: Float

Position down where it hit.

Functions

Link copied to clipboard
open override fun toString(): String