Gravity2DPlugin

View source

ExtendsPlugin

Adds configurable 2D gravity to an App.

The plugin installs a Gravity2D resource initialized from the configured Vector2, or (0, -980) by default. It applies that acceleration to eligible bodies as the app updates. The plugin depends on CorePlugin, ReflectPlugin, MathPlugin, Movable2DPlugin, and NarrowPhase2DPlugin.

Pass a Vector2 in options.gravity to choose a different acceleration. The vector is copied into the installed resource.

Examples

import { App } from '@wimaengine/app'
import { CorePlugin } from '@wimaengine/core'
import { Gravity2DPlugin } from '@wimaengine/gravity'
import { MathPlugin, Vector2 } from '@wimaengine/math'
import { Movable2DPlugin } from '@wimaengine/movable'
import { NarrowPhase2DPlugin } from '@wimaengine/narrowphase'
import { ReflectPlugin } from '@wimaengine/reflect'

new App()
  .registerPlugin(new ReflectPlugin())
  .registerPlugin(new CorePlugin())
  .registerPlugin(new MathPlugin())
  .registerPlugin(new Movable2DPlugin())
  .registerPlugin(new NarrowPhase2DPlugin())
  .registerPlugin(new Gravity2DPlugin({
    gravity: new Vector2(0, -9.8)
  }))
  .run()

Constructor


constructor(options?: Gravity2DPluginOptions)

View source

Creates a 2D gravity plugin.

The optional gravity value is a Vector2. When omitted, it defaults to (0, -980).

Parameters

NameDescriptionDefault
options?

Optional gravity configuration.

{}

Properties


readonlygravity: Vector2

View source

Configured Vector2 gravity vector.

The vector is copied into the installed Gravity2D resource when the plugin is registered, so the resource has its own value.

Methods


register(app: App): void

View source

Installs configured 2D gravity in an app.

The installed Gravity2D contains a copy of Gravity2DPlugin#gravity. Its acceleration is added to eligible bodies as the app updates.

Parameters

NameDescriptionDefault
app

App to configure.

—