struct Godot::PhysicsHit2D

Overview

Strongly-typed 2D physics intersection hit result.

Encapsulates collision location, surface normal, and hit object reference with built-in dead-pointer protection.

Usage Example:

if hit = raycast_to(target_pos)
  # Standard Crystal downcasting with dead-pointer safety:
  if enemy = hit.collider.as?(Enemy)
    enemy.take_damage(25)
  end
end

Defined in:

libgodot/extensions/physics.cr

Constructors

Instance Method Summary

Instance methods inherited from class Object

try?(&)
try?
try?

Constructor Detail

def self.new(point : Vector2, normal : Vector2, collider : Godot::Object | Nil = nil, rid : RID = RID.new, shape : Int32 = 0) #

Instance Method Detail

def collider : Godot::Object | Nil #

Returns the hit collider if alive, or nil if already freed or absent.

Rationale & Dead-Pointer Protection:

If the collider node was destroyed by Godot or GDScript (e.g. via queue_free()) during the current physics step, accessing a stale pointer causes a segmentation fault (0xC0000005). #collider dynamically validates #alive? before returning, safely yielding nil if the native instance was deallocated.

Pair with .as?(Type) for idiomatic, type-safe downcasting:

if player = hit.collider.as?(Player)
  player.heal(10)
end

def normal : Vector2 #

The surface normal vector at the intersection point


def point : Vector2 #

The global intersection coordinate


def rid : RID #

The RID of the colliding physics body or shape


def shape : Int32 #

The shape index within the colliding body