ray

package
v4.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package ray holds utilities for performing iterative collision checks or raycasts

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultCaster is a global caster that all
	// NewCaster() calls are built on before options are applied.
	DefaultCaster = &Caster{
		PointSize: floatgeom.Point2{.1, .1},
		PointSpan: 1.0,

		CastDistance: 200,
		Tree:         collision.DefaultTree,
	}
)
View Source
var (
	// DefaultConeCaster is a global caster that all NewConeCaster()
	// calls are built on before options are applied.
	DefaultConeCaster = &ConeCaster{
		Caster:     DefaultCaster,
		CenterCone: true,
		ConeSpread: 1,
		Rays:       1,
	}
)

Functions

func Cast

func Cast(origin, angle floatgeom.Point2) []collision.Point

Cast calls DefaultCaster.Cast. See (*Caster).Cast

func CastTo

func CastTo(origin, target floatgeom.Point2) []collision.Point

CastTo calls DefaultCaster.CastTo. See (*Caster).CastTo

func ConeCast

func ConeCast(origin, angle floatgeom.Point2) []collision.Point

ConeCast calls DefaultConeCaster.Cast. See (*ConeCaster).Cast

func ConeCastTo

func ConeCastTo(origin, target floatgeom.Point2) []collision.Point

ConeCastTo calls DefaultConeCaster.CastTo. See (*ConeCaster).CastTo

func SetDefaultCaster

func SetDefaultCaster(caster *Caster)

SetDefaultCaster sets the global caster to be the input, and sets the caster behind the global cone caster as well.

func SetDefaultConeCaster

func SetDefaultConeCaster(coneCaster *ConeCaster)

SetDefaultConeCaster is analogous to SetDefaultCaster, however is equivalent to setting the global variable.

Types

type CastFilter

type CastFilter func(*collision.Space) bool

A CastFilter is a function that can be applied to a Caster for each space the Caster's rays hit, returning whether or not those spaces should be contained in the Caster's output.

type CastLimit

type CastLimit func([]collision.Point) bool

A CastLimit is a function that can be applied to a Caster's points to return after it adds each one. If a Limit returns false, that Caster will immediately cease casting.

If a Caster's ray collides with multiple spaces at the same point, and some of them would pass a CastLimit, but others would not, a Caster will not reliably return those that would pass the limit.

type CastOption

type CastOption func(*Caster)

A CastOption represents a transformation to a ray caster.

func AcceptIDs

func AcceptIDs(ids ...event.CallerID) CastOption

AcceptIDs is equivalent to AcceptLabels, but for CIDs.

func AcceptLabels

func AcceptLabels(ls ...collision.Label) CastOption

AcceptLabels signals to a Caster to only return spaces that have a space in the set of input labels. If anything in ls is also contained by an IgnoreLabels filter on the applied Caster, Ignore will dominate.

func AddFilter

func AddFilter(cf CastFilter) CastOption

AddFilter is a utility to convert a CastFilter to a CastOption.

func AddLimit

func AddLimit(cl CastLimit) CastOption

AddLimit is a helper for converting a CastLimit into a CastOption.

func CenterPoints

func CenterPoints(on bool) CastOption

CenterPoints sets whether a Caster should center its collision points that form its ray. This is by default false, and is only significant if said points' dimensions are significantly large.

func Distance

func Distance(dist float64) CastOption

Distance determines how far a caster will project rays before stopping

func IgnoreIDs

func IgnoreIDs(ids ...event.CallerID) CastOption

IgnoreIDs is equivalent to IgnoreLabels, but for CIDs.

func IgnoreLabels

func IgnoreLabels(ls ...collision.Label) CastOption

IgnoreLabels signals to a Caster to not return spaces that have spaces with labels in the set of input labels.

func LimitResults

func LimitResults(limit int) CastOption

LimitResults will cause a Caster to return a limited number of collision points.

func Pierce

func Pierce(n int) CastOption

Pierce signals to a Caster to ignore the first n spaces its rays collide with, regardless of their composition.

func PointSize

func PointSize(ps floatgeom.Point2) CastOption

PointSize determines the size of a caster's collision checks

func PointSpan

func PointSpan(span float64) CastOption

PointSpan determines the distance between collision check points

func StopAtID

func StopAtID(ids ...event.CallerID) CastOption

StopAtID will cause a caster to cease casting as soon as it hits one of the input CIDs.

func StopAtLabel

func StopAtLabel(ls ...collision.Label) CastOption

StopAtLabel will cause a caster to cease casting as soon as it hits one of the input labels.

func Tree

func Tree(t *collision.Tree) CastOption

Tree sets the collision tree of a Caster.

type Caster

type Caster struct {
	Filters      []CastFilter
	Limits       []CastLimit
	PointSize    floatgeom.Point2
	PointSpan    float64
	CastDistance float64
	Tree         *collision.Tree
	CenterPoints bool
}

A Caster can cast rays and return the colliding collision points of rays cast from points at angles. This behavior is customizable through CastOptions.

func NewCaster

func NewCaster(opts ...CastOption) *Caster

NewCaster will copy and modify the DefaultCaster by the input options and return the modified Caster. Giving no inputs is valid.

func (*Caster) Cast

func (c *Caster) Cast(origin, angle floatgeom.Point2) []collision.Point

Cast creates a ray from origin pointing at the given angle and returns some spaces collided with at the point of collision, given the settings of this Caster. By default, all spaces hit will be returned.

func (*Caster) CastTo

func (c *Caster) CastTo(origin, target floatgeom.Point2) []collision.Point

CastTo casts a ray from origin to target, and otherwise acts as Cast.

func (*Caster) Copy

func (c *Caster) Copy() *Caster

Copy copies a Caster.

type ConeCastOption

type ConeCastOption func(*ConeCaster)

A ConeCastOption represents a transformation on a ConeCaster.

func CenterCone

func CenterCone(on bool) ConeCastOption

CenterCone sets whether the caster should center its cones around the input angles or progress out from those input angles. True by default.

Example: Casting from a to b: if True:

  .  b  .
 . . .
...

a if False:

  b     .
 .   .
. .

a

func ConeRays

func ConeRays(rays int) ConeCastOption

ConeRays sets how many rays a ConeCaster should divide its spread into.

func ConeSpread

func ConeSpread(degrees float64) ConeCastOption

ConeSpread sets how far a ConeCaster should progress its angles in degrees.

func ConeSpreadRadians

func ConeSpreadRadians(radians float64) ConeCastOption

ConeSpreadRadians sets how far a ConeCaster should progress its angles in radians.

type ConeCaster

type ConeCaster struct {
	*Caster
	CenterCone bool
	// ConeSpread is represented in radians
	ConeSpread float64
	Rays       float64
}

A ConeCaster will repeatedly Cast its underlying Caster in a cone shape.

func NewConeCaster

func NewConeCaster(opts ...ConeCastOption) *ConeCaster

NewConeCaster copies the DefaultConeCaster and modifies it with the input options, returning the modified Caster. Zero arguments is valid input.

func (*ConeCaster) Cast

func (cc *ConeCaster) Cast(origin, angle floatgeom.Point2) []collision.Point

Cast creates a ray from origin pointing at the given angle and returns some spaces collided with at the point of collision, given the settings of this ConeCaster. By default, all spaces hit will be returned. ConeCasters in addition will recast at progressive angles until they have cast up to their Rays value. Angles progress in counter-clockwise order.

func (*ConeCaster) CastTo

func (cc *ConeCaster) CastTo(origin, target floatgeom.Point2) []collision.Point

CastTo casts a ray from origin to target, and otherwise acts as Cast.

func (*ConeCaster) Copy

func (cc *ConeCaster) Copy() *ConeCaster

Copy copies a ConeCaster.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL