Aego Русский

aego/physics

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.

On this page

Example

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

Functions

ContactContact describes a touch between two colliders from the receiver's side.
Gravity() Vec2Returns the world gravity in units per second squared. +Y points down.
Layer(name string) intReturns the index of a named physics layer.
LayerMask(name string) intReturns the mask bit of a named layer. Add masks of different layers to combine them.
Linecast(from Vec2, to Vec2, mask int) aego/physics.RayHitReturns the first hit between from and to on the layers in mask; -1 means every layer.
OverlapBox(center Vec2, size Vec2, out []NodeRef) intWrites the bodies touching the axis-aligned box into out and returns the count.
OverlapCircle(center Vec2, radius float, out []NodeRef) intWrites the bodies touching the circle into out and returns the count.
OverlapPoint(point Vec2, out []NodeRef) intWrites the bodies whose colliders contain p into out and returns the count.
Raycast(origin Vec2, dir Vec2, maxDist float) aego/physics.RayHitReturns the closest hit along dir within maxDist. The caller's own body is ignored, triggers are skipped.
RaycastAll(from Vec2, to Vec2, out []aego/physics.RayHit) intFills out with hits between from and to, nearest first, and returns how many it wrote. It writes at most len(out).
RayHitRayHit is the result of a ray query. Hit is false when nothing was found; Node is the body, Collider the node of the shape that was hit.
SetGravity(gravity Vec2)Changes the world gravity for the rest of the Play session.

Gravity function

func Gravity() Vec2

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

See the module example for this function in use.

Layer function

func Layer(name string) int

Returns the index of a named physics layer.

See the module example for this function in use.

LayerMask function

func LayerMask(name string) int

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

Example

mask := physics.LayerMask("World") + physics.LayerMask("Enemies")

Linecast function

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.

Example

hit := physics.Linecast(eye, target, physics.LayerMask("World"))

OverlapBox function

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

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

See the module example for this function in use.

OverlapCircle function

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

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

See the module example for this function in use.

OverlapPoint function

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

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

See the module example for this function in use.

Raycast function

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.

Example

hit := physics.Raycast(pos, Vec2{Y: 1}, 0.6)
if hit.Hit {
	grounded = true
}

RaycastAll function

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

See the module example for this function in use.

SetGravity function

func SetGravity(gravity Vec2)

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

See the module example for this function in use.