# aego/physics

> Aego 0.12.5 · development; docs build `4a32b816933e02930662f6a443cb25abe6050d090c55e237cbff5c44ae9b25f8`. [Build manifest](/docs-build.json).

Queries work only while physics runs, during Play and in `aego run`; outside it every function stops the script with `AES6010`. The `Overlap*` and `RaycastAll` functions fill a slice you pass, never more than its length, and return how many they wrote. `Layer` and `LayerMask` take a layer declared in Project Settings → Physics and stop the script for an unknown name; a mask of `-1` means every layer. The documentation sandbox has no physics world, so the example keeps every query in `OnCollisionEnter`, which runs once the node has a Rigid Body 2D and a collider and touches another body. See [Physics 2D](/physics.md).

## Example

```aes run
package demo

import (
    "aego/log"
    "aego/physics"
)

script Grenade {
    @export var Radius float = 2

    var hits []NodeRef
    var exploded bool

    func Ready() {
        for i := 0; i < 16; i++ {
            hits = append(hits, nil)
        }
        log.Info("waiting for a collision; this node needs a Rigid Body 2D and a collider")
    }

    func OnCollisionEnter(other NodeRef, contact physics.Contact) {
        if exploded {
            return
        }
        exploded = true
        log.Info("hit " + other.Name() + " with vertical speed " + string(contact.RelativeVelocity.Y))
        n := physics.OverlapCircle(contact.Point, Radius, hits)
        for i := 0; i < n; i++ {
            wall := physics.Linecast(contact.Point, hits[i].WorldPosition(), -1)
            if !wall.Hit {
                log.Info("blast reaches " + hits[i].Name())
            }
        }
        down := physics.Raycast(node.WorldPosition(), Vec2{0, 1}, 5)
        if down.Hit {
            log.Info("ground " + string(down.Distance) + " units below, gravity " + string(physics.Gravity().Y))
        }
    }
}
```

## Gravity

```
func Gravity() Vec2
```

Returns the world gravity in units per second squared. +Y points down.

## Layer

```
func Layer(name string) int
```

Returns the index of a named physics layer.

## LayerMask

```
func LayerMask(name string) int
```

Returns the mask bit of a named layer. Add masks of different layers to combine them.

## Linecast

```
func Linecast(from Vec2, to Vec2, mask int) aego/physics.RayHit
```

Returns the first hit between from and to on the layers in mask; -1 means every layer.

## OverlapBox

```
func OverlapBox(center Vec2, size Vec2, out []NodeRef) int
```

Writes the bodies touching the axis-aligned box into out and returns the count.

## OverlapCircle

```
func OverlapCircle(center Vec2, radius float, out []NodeRef) int
```

Writes the bodies touching the circle into out and returns the count.

## OverlapPoint

```
func OverlapPoint(point Vec2, out []NodeRef) int
```

Writes the bodies whose colliders contain p into out and returns the count.

## Raycast

```
func Raycast(origin Vec2, dir Vec2, maxDist float) aego/physics.RayHit
```

Returns the closest hit along dir within maxDist. The caller's own body is ignored, triggers are skipped.

## RaycastAll

```
func RaycastAll(from Vec2, to Vec2, out []aego/physics.RayHit) int
```

Fills out with hits between from and to, nearest first, and returns how many it wrote. It writes at most len(out).

## SetGravity

```
func SetGravity(gravity Vec2)
```

Changes the world gravity for the rest of the Play session.
