shaker

package
v0.0.0-...-5b3fe6d Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

This package defines a Shaker interface that the ebipixel camera can use to perform screen shakes, and provides a few default implementations.

All provided implementations respect a few properties:

  • Resolution independent: range of motion for the shakes is not hardcoded, but proportional to the game's resolution.
  • Tick-rate independent: results are visually similar regardless of your Tick().UPS() and Tick().GetRate() values. See ups-vs-tps if you need more context.

These are nice properties for public implementations, but if you are writing your own, remember that most often these properties won't be relevant to you. You can ignore them and make your life easier if you are only getting started.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Balanced

type Balanced struct {
	// contains filtered or unexported fields
}

Cubic bézier curves with start and end points at (0, 0), in some kind of circular oscillation.

You can set the motion scale to 0.02 and travel time to 1.4 for a soft ship-like motion. The default is more like a dampened earthquake. The shaking is fairly homogeneous and consistent in speed and motion. You could probably dive into the code and tweak a few things here and there to destabilize speed and get something more similar to a drunken effect. You probably shouldn't, but you could.

The implementation is tick-rate independent.

func (*Balanced) GetShakeOffsets

func (self *Balanced) GetShakeOffsets(level float64) (float64, float64)

Implements the Shaker interface.

func (*Balanced) SetMotionScale

func (self *Balanced) SetMotionScale(axisScalingFactor float64)

To preserve resolution independence, shakers often simulate the shaking within a [-0.5, 0.5] space and only later scale it. For example, if you have a resolution of 32x32 and set a motion scale of 0.25, the shaking will range within [-4, +4] in both axes.

Defaults to 0.01.

func (*Balanced) SetTravelTime

func (self *Balanced) SetTravelTime(travelTime float64)

Change the travel time between generated shake points. Defaults to 0.05.

func (*Balanced) SetZoomCompensation

func (self *Balanced) SetZoomCompensation(compensation float64)

The range of motion of most shakers is based on the logical resolution of the game. This means that when zooming in or out, the shaking effect will become more or less pronounced, respectively. If you want the shaking to maintain the same relative magnitude regardless of zoom level, change the zoom compensation from 0 (the default) to 1.

type Bezier

type Bezier struct {
	// contains filtered or unexported fields
}

Implementation of a Shaker using bézier curves in strange ways.

This shaker has a fair share of personality. I would say it's quite biased and unpleasant, like someone throwing a tantrum.

The implementation is tick-rate independent.

func (*Bezier) GetShakeOffsets

func (self *Bezier) GetShakeOffsets(level float64) (float64, float64)

Implements the Shaker interface.

func (*Bezier) SetMotionScale

func (self *Bezier) SetMotionScale(axisScalingFactor float64)

To preserve resolution independence, shakers often simulate the shaking within a [-0.5, 0.5] space and only later scale it. For example, if you have a resolution of 32x32 and set a motion scale of 0.25, the shaking will range within [-4, +4] in both axes.

Defaults to 0.05.

func (*Bezier) SetTravelTime

func (self *Bezier) SetTravelTime(travelTime float64)

Change the travel time between generated shake points. Defaults to 0.1.

func (*Bezier) SetZoomCompensation

func (self *Bezier) SetZoomCompensation(compensation float64)

The range of motion of most shakers is based on the logical resolution of the game. This means that when zooming in or out, the shaking effect will become more or less pronounced, respectively. If you want the shaking to maintain the same relative magnitude regardless of zoom level, change the zoom compensation from 0 (the default) to 1.

type Channel

type Channel uint8

Used by ebipixel in case multiple shakes need to be active at the same time.

Channel zero is special and will use a fallback shaker even if uninitialized (though it's best practice to always set your shakers explicitly). It's also the channel that will be automatically selected for most shaker functions in ebipixel if no channel is explicitly passed.

Here's an example of when multiple channels are useful:

  • You need an always-on shake for camera motion or environment shaking, like being in a ship or hot air ballon.
  • You need the typical triggered shakes for momentary impacts, explosions, earthquakes and so on.
  • You have some alter states like drunk or confused that might use some extra shaker channels.

In these cases, you should define your own channel constants, e.g:

const (
  ChanBackground shaker.Channel = iota
  ChanTrigger
  ChanDrunk
)

In even more complex cases, you might decide to treat your channels like a shaker pool to manage everything more dynamically; who knows, it all depends on the game.

type Combo

type Combo struct {
	// contains filtered or unexported fields
}

An example Shaker created by combining a Balanced and a Random shaker. This is only offered to showcase how easy it is to create new shakers by combining previously existing ones. Since this is only an example, no methods for configuring the parameters are exposed.

func (*Combo) GetShakeOffsets

func (self *Combo) GetShakeOffsets(level float64) (float64, float64)

Implements Shaker.

type Quake

type Quake struct {
	// contains filtered or unexported fields
}

Implementation of a Shaker with consistently oscillating movement in both axes, but with some irregularities in speed and travel distance. It's interesting because it has those unpredictable variances within a very predictable and slightly smoothed motion pattern.

The implementation is tick-rate independent.

func (*Quake) GetShakeOffsets

func (self *Quake) GetShakeOffsets(level float64) (float64, float64)

Implements the Shaker interface.

func (*Quake) SetMotionScale

func (self *Quake) SetMotionScale(axisScalingFactor float64)

To preserve resolution independence, shakers often simulate the shaking within a [-0.5, 0.5] space and only later scale it. For example, if you have a resolution of 32x32 and set a motion scale of 0.25, the shaking will range within [-4, +4] in both axes.

Defaults to 0.0225.

func (*Quake) SetSpeedRange

func (self *Quake) SetSpeedRange(minSpeed, maxSpeed float64)

Internally, both x and y oscillate at their own speeds. While going from side to side, these speeds can change between any random values within [minSpeed, maxSpeed].

This method allows you to configure those speeds. The default values are (5.0, 23.0).

func (*Quake) SetZoomCompensation

func (self *Quake) SetZoomCompensation(compensation float64)

The range of motion of most shakers is based on the logical resolution of the game. This means that when zooming in or out, the shaking effect will become more or less pronounced, respectively. If you want the shaking to maintain the same relative magnitude regardless of zoom level, change the zoom compensation from 0 (the default) to 1.

type Random

type Random struct {
	// contains filtered or unexported fields
}

Very basic implementation of a Shaker using random values.

This shaker is not particularly nice; good screen shakes generally try to create a continuous movement, avoiding sharp direction changes and so on. Random is cheap (still classic and effective, though).

The implementation is tick-rate independent.

func (*Random) GetShakeOffsets

func (self *Random) GetShakeOffsets(level float64) (float64, float64)

Implements the Shaker interface.

func (*Random) SetMotionScale

func (self *Random) SetMotionScale(axisScalingFactor float64)

To preserve resolution independence, shakers often simulate the shaking within a [-0.5, 0.5] space and only later scale it. For example, if you have a resolution of 32x32 and set a motion scale of 0.25, the shaking will range within [-4, +4] in both axes.

Defaults to 0.02.

func (*Random) SetTravelTime

func (self *Random) SetTravelTime(travelTime float64)

Change the travel time between generated shake points. Defaults to 0.1.

func (*Random) SetZoomCompensated

func (self *Random) SetZoomCompensated(compensated bool)

The range of motion of most shakers is based on the logical resolution of the game. This means that when zooming in or out, the shaking effect will become more or less pronounced, respectively. If you want the shaking to maintain the same relative magnitude regardless of zoom level, set zoom compensated to true.

type Shaker

type Shaker interface {
	GetShakeOffsets(level float64) (float64, float64)
}

The interface for ebipixel screen shakers.

Given a level that transitions linearly between 0 and 1 during the fade in and fade out stages, GetShakeOffsets() returns the logical offsets for the camera.

After stoping, there will be one call with level = 0 that can be used to reset the shaker state. The results of this call will be disregarded.

Minor detail: all built-in implementations happen to normalize the fade in/out level with a cubic smoothstep, just to make things nicer.

type Spring

type Spring struct {
	// contains filtered or unexported fields
}

A Shaker implementation based on spring simulations. It's nothing special, but it has its own flavor. Common configurations remind me of boxes falling from the closet, or driving through a bad road (e.g., params {0.1, 40.0} and motion scale {0.02, 0.01}).

The implementation is tick-rate independent.

func (*Spring) GetShakeOffsets

func (self *Spring) GetShakeOffsets(level float64) (float64, float64)

Implements the Shaker interface.

func (*Spring) SetMotionScale

func (self *Spring) SetMotionScale(xScalingFactor, yScalingFactor float64)

To preserve resolution independence, shakers often simulate the shaking within a [-0.5, 0.5] space and only later scale it. For example, if you have a resolution of 32x32 and set a motion scale of (0.25, 0.25), the shaking will range within [-4, +4] in both axes.

Defaults to 0.02.

func (*Spring) SetParameters

func (self *Spring) SetParameters(damping, power float64)

Sets the internal spring simulation parameters. Defaults are (0.25, 80.0), but it depends a lot on the motion scaling too.

func (*Spring) SetZoomCompensation

func (self *Spring) SetZoomCompensation(compensation float64)

The range of motion of most shakers is based on the logical resolution of the game. This means that when zooming in or out, the shaking effect will become more or less pronounced, respectively. If you want the shaking to maintain the same relative magnitude regardless of zoom level, change the zoom compensation from 0 (the default) to 1.

Jump to

Keyboard shortcuts

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