Skip to reading
GDScript Programming & Godot

45. Physics, areas and ray queries

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page
Code example
func make_blocker() -> StaticBody3D:
    var body := StaticBody3D.new()
    var collision := CollisionShape3D.new()
    var box := BoxShape3D.new()
    box.size = Vector3.ONE
    collision.shape = box
    body.add_child(collision)
    return body
Code example
func run() -> String:
    var body: StaticBody3D = make_blocker()
    var result: String = body.get_class()
    body.free()
    return result

Godot physics needs CollisionObject/PhysicsBody/Area nodes, shapes and matching collision layers/masks. A visible mesh alone is not a collision body. Perform physics-space queries at the appropriate physics stage, and defer collision changes when a physics callback requires it.

A ray query uses PhysicsRayQueryParameters3D and the active World3D’s direct_space_state.intersect_ray. Camera screen coordinates must belong to that camera’s viewport. PCA can render a room into an internal viewport; raw window coordinates are not automatically correct.

PCA navigation obstacles, hotspot picking and event volumes are not automatically identical to Godot physics bodies. Use PCA trigger events for normal adventure actors unless your extension has supplied/tested real physics bodies for that interaction.

Page 54 of 378Permalink
Go to page