Documentation
¶
Overview ¶
Package motion is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.
Package motion provides deterministic, rate-independent 2D motion helpers for games that drive objects from Go code without scene files, sharing the exact math and semantics of sceneformat's built-in 2D motion behavior components:
- Bullet: linear projectile motion with speed, acceleration, gravity, optional angle alignment and continuous swept bouncing off solid geometry.
- Sine: periodic oscillation (sine, triangle, sawtooth, reverse_sawtooth, square) with phase and magnitude applied as drift-free deltas.
- Rotate: angular velocity and angular acceleration in degrees per second.
- Orbit: circular motion around a fixed point or moving target.
- MoveTo: direct movement toward a target point with optional acceleration and solid stopping.
- Wrap: toroidal edge wrapping across scene or camera view bounds.
- Bound: clamping an object's edge box or origin inside bounds.
- DestroyOutside: detecting when an object leaves expanded bounds.
- Fade: fade-in, hold (wait) and fade-out opacity envelope.
- Flash: timed on/off visibility blinking with base-alpha restoration.
All trigonometry uses internal/detmath so arm64, amd64 and wasm produce bit-for-bit identical trajectories. Constant-acceleration and closed-form stepping ensure fixed-rate simulations (such as 60 Hz vs 120 Hz) reach the same positions per simulated second.
Index ¶
- func EvalWave(wave WaveKind, u float64) float64
- type Bound
- type Bounds
- type Bullet
- type BulletStepResult
- type DestroyOutside
- type Fade
- type FadeStepResult
- type Flash
- type FlashStepResult
- type MoveTo
- type MoveToStepResult
- type Orbit
- type OrbitStepResult
- type Rotate
- type Sine
- type SolidCastFunc
- type SolidHit
- type WaveKind
- type Wrap
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Bound ¶
type Bound struct {
// Edge keeps the object's full [x-Left, x+Right] x [y-Top, y+Bottom] box inside bounds;
// when false, clamps only the origin (x, y).
Edge bool
// Enabled controls whether Apply clamps coordinates.
Enabled bool
}
Bound clamps an object inside a rectangle, either by its edge extents or by its origin.
type Bounds ¶
Bounds is an axis-aligned 2D rectangle [MinX, MaxX] x [MinY, MaxY].
type Bullet ¶
type Bullet struct {
// Speed is the current scalar travel speed in points per second along Angle.
Speed float64
// Acceleration is the linear acceleration along the velocity direction in points/s^2.
Acceleration float64
// Gravity is the downward (+Y) acceleration in points/s^2.
Gravity float64
// Angle is the heading in degrees (0 = +X right, 90 = +Y down).
Angle float64
// SetAngle reports whether the object's rotation should track the heading.
SetAngle bool
// BounceOffSolids reflects velocity across solid surface normals when a SolidCastFunc is supplied.
BounceOffSolids bool
// Enabled controls whether Step advances the bullet.
Enabled bool
// contains filtered or unexported fields
}
Bullet advances a 2D projectile with speed, linear acceleration along its travel direction, vertical gravity (+Y down), optional orientation alignment and continuous bouncing off solids.
func NewBullet ¶
func NewBullet(speed, acceleration, gravity, angleDeg float64, setAngle, bounceOffSolids bool) Bullet
NewBullet returns an enabled Bullet initialized from speed, acceleration, gravity and angle (degrees).
func (*Bullet) SetHeading ¶
SetHeading updates the bullet's heading in degrees while preserving its current speed.
func (*Bullet) SetSpeed ¶
SetSpeed updates the bullet's scalar speed while preserving its current heading.
func (*Bullet) Step ¶
func (b *Bullet) Step(x, y float64, dt time.Duration, cast SolidCastFunc) BulletStepResult
Step advances the bullet from (x, y) by dt. When BounceOffSolids is true and cast is non-nil, the displacement is swept continuously against solids and reflected across the contact normal.
type BulletStepResult ¶
type BulletStepResult struct {
X, Y float64
Rotation float64
SetRot bool
Bounced bool
Bounces int
HitNormal [2]float64
}
BulletStepResult reports the updated position, heading and bounce info for one Bullet.Step call.
type DestroyOutside ¶
type DestroyOutside struct {
// Margin is the distance outside the bounds an object may travel before being destroyed (>= 0).
Margin float64
// Enabled controls whether Outside returns true.
Enabled bool
}
DestroyOutside tests whether an object has left a rectangle expanded by Margin.
func NewDestroyOutside ¶
func NewDestroyOutside(margin float64) DestroyOutside
NewDestroyOutside returns an enabled DestroyOutside check.
type Fade ¶
type Fade struct {
// FadeIn is the ramp-up duration from 0 to BaseAlpha in seconds (>= 0).
FadeIn float64
// Wait is the hold duration at BaseAlpha in seconds (>= 0).
Wait float64
// FadeOut is the ramp-down duration from BaseAlpha to 0 in seconds (>= 0).
FadeOut float64
// Destroy reports whether the owner should be freed upon completion.
Destroy bool
// BaseAlpha is the peak opacity in [0, 1] (0 defaults to 1 if BaseAlphaSet is false).
BaseAlpha float64
// Running controls whether Step advances the fade timer.
Running bool
// Enabled controls whether Step runs at all.
Enabled bool
// contains filtered or unexported fields
}
Fade drives an opacity envelope: FadeIn -> Wait -> FadeOut, optionally marking the object for destruction when finished.
func NewFade ¶
NewFade returns a Fade behavior configured with fadeIn, wait, fadeOut (seconds) and destroy flag.
type FadeStepResult ¶
FadeStepResult reports the computed opacity and completion flags for one Fade.Step call.
type Flash ¶
type Flash struct {
// On is the visible interval duration in seconds (>= 0).
On float64
// Off is the hidden interval duration in seconds (>= 0).
Off float64
// Enabled controls whether Step advances the flash.
Enabled bool
// contains filtered or unexported fields
}
Flash toggles visibility on and off for a requested duration, then restores visibility.
func (*Flash) Step ¶
func (f *Flash) Step(dt time.Duration) FlashStepResult
Step advances the flash timer by dt and returns visibility and completion state.
func (*Flash) Stop ¶
func (f *Flash) Stop()
Stop halts flashing immediately and restores visibility.
type FlashStepResult ¶
FlashStepResult reports whether the object should be visible and whether the flash ended on this step.
type MoveTo ¶
type MoveTo struct {
// MaxSpeed is the top speed in points per second (>= 0).
MaxSpeed float64
// Acceleration is the linear ramp-up in points/s^2 (0 reaches MaxSpeed immediately).
Acceleration float64
// StopOnSolids halts movement when a solid is encountered along the path.
StopOnSolids bool
// TargetX and TargetY are the current destination coordinates.
TargetX, TargetY float64
// Active reports whether the object is currently moving toward the target.
Active bool
// Enabled controls whether Step advances movement.
Enabled bool
// contains filtered or unexported fields
}
MoveTo drives an object straight toward (TargetX, TargetY) at up to MaxSpeed points per second, optionally accelerating smoothly and stopping on solid collisions.
func (*MoveTo) Step ¶
func (m *MoveTo) Step(x, y float64, dt time.Duration, cast SolidCastFunc) MoveToStepResult
Step advances from (x, y) toward (TargetX, TargetY) by dt using exact piecewise constant-acceleration integration (rate-independent across fixed step rates).
type MoveToStepResult ¶
MoveToStepResult reports the updated position and arrival/block flags from one MoveTo.Step call.
type Orbit ¶
type Orbit struct {
// Radius is the orbit radius in points (>= 0).
Radius float64
// Speed is the angular velocity in degrees per second.
Speed float64
// Angle is the current orbit angle in degrees (0 = +X right, 90 = +Y down).
Angle float64
// SetAngle reports whether the object's rotation should match the orbit angle.
SetAngle bool
// Enabled controls whether Step advances the orbit.
Enabled bool
}
Orbit moves an object along a circle of Radius around a center point at Speed degrees per second.
type OrbitStepResult ¶
OrbitStepResult holds the updated position and optional rotation from one Orbit.Step call.
type Rotate ¶
type Rotate struct {
// Speed is the angular velocity in degrees per second (positive = clockwise in Y-down 2D).
Speed float64
// Acceleration is the angular acceleration in degrees per second squared.
Acceleration float64
// Enabled controls whether Step advances the rotation.
Enabled bool
}
Rotate spins an object continuously at Speed degrees per second with optional angular Acceleration.
type Sine ¶
type Sine struct {
// Period is the duration of one full oscillation cycle in seconds (> 0).
Period float64
// Magnitude is the peak amplitude in property units (points, degrees, scale, or alpha).
Magnitude float64
// Wave selects the periodic waveform (defaults to WaveSine when empty).
Wave WaveKind
// Phase is the initial cycle offset (0.25 = quarter period, 0.5 = half period).
Phase float64
// Enabled controls whether Step advances the oscillation.
Enabled bool
// contains filtered or unexported fields
}
Sine oscillates a scalar quantity periodically around its authored baseline using additive deltas, so it composes cleanly with other behaviors (such as Bullet).
func (*Sine) Reset ¶
func (s *Sine) Reset()
Reset sets elapsed time and applied offset back to zero.
type SolidCastFunc ¶
SolidCastFunc sweeps an object from (x, y) by displacement (dx, dy) and reports the earliest solid hit in [0, 1], if any.
type SolidHit ¶
type SolidHit struct {
// Fraction is the normalized time of impact in [0, 1].
Fraction float64
// NormalX and NormalY form the unit surface normal pointing out of the solid.
NormalX, NormalY float64
}
SolidHit is the earliest continuous contact along a motion sweep from (x, y) by (dx, dy).
type WaveKind ¶
type WaveKind string
WaveKind identifies the periodic waveform used by Sine.
const ( // WaveSine oscillates as sin(2*pi*u) in [-1, 1]. WaveSine WaveKind = "sine" // WaveTriangle oscillates linearly from 0 -> 1 -> -1 -> 0 in [-1, 1]. WaveTriangle WaveKind = "triangle" // WaveSawtooth ramps linearly from -1 to +1 over each period, jumping back to -1 at u = 1. WaveSawtooth WaveKind = "sawtooth" // WaveReverseSawtooth ramps linearly from +1 to -1 over each period, jumping back to +1 at u = 1. WaveReverseSawtooth WaveKind = "reverse_sawtooth" // WaveSquare holds +1 for the first half of each period [0, 0.5) and -1 for [0.5, 1). WaveSquare WaveKind = "square" )
type Wrap ¶
type Wrap struct {
// Margin expands the wrap rectangle outward on all four sides (>= 0).
Margin float64
// Enabled controls whether Apply wraps coordinates.
Enabled bool
}
Wrap wraps coordinates leaving one side of a rectangle onto the opposite side, preserving overshoot so wrapped motion stays rate-independent.
Example ¶
Wrap carries an object that leaves one side of the play area onto the opposite side, preserving the overshoot so wrapped motion stays rate-independent.
package main
import (
"fmt"
"github.com/nsgo-dev/game/pkg/motion"
)
func main() {
bounds := motion.Bounds{MinX: 0, MinY: 0, MaxX: 100, MaxY: 50}
wrap := motion.NewWrap(0)
x, y, wrapped := wrap.Apply(130, -10, bounds)
fmt.Println(x, y, wrapped)
}
Output: 30 40 true