engoBox2dSystem

package module
v0.0.0-...-4520a46 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2023 License: MIT Imports: 6 Imported by: 3

README

engoBox2dSystem GoDoc

Physics, Collision, and Mouse systems for the Engo game engine that utilizes ByteArena's box2d Go port

This is a set of systems for use with the Engo game engine.

To use, make sure Engo is working first. Then you'll need the go port of box2d. Then you can run

go get github.com/Noofbiz/engoBox2dSystem

Check out the demos to see what you can do. They're currently a work in progress, so check back later for more!

Documentation

Overview

Package engoBox2dSystem provides a collision, physics, and mouse system for use with the engo game engine. These systems are integrated with the go port of the box2d physics engine.

Index

Constants

View Source
const MouseSystemPriority = 100

MouseSystemPriority ensures the mouse system is updated before any other systems

Variables

View Source
var Conv = &Convert{20}

Conv is the internal converter used by the system. Use this rather than create your own, but you can change the pixels per meter here and it'll change it for all the systems too.

View Source
var World = box2d.MakeB2World(box2d.B2Vec2{X: 0, Y: 0})

World is the box2d World used to generate bodies, test bodies for collisions, and simulate physics

Functions

This section is empty.

Types

type Box2dComponent

type Box2dComponent struct {
	// Body is the box2d body
	Body *box2d.B2Body
}

Box2dComponent holds the box2d Body for use by Systems

func (*Box2dComponent) DestroyBody

func (b *Box2dComponent) DestroyBody()

DestroyBody destroys the box2d body from the World this does it safely at the end of an Update, so no bodies are removed during a simulation step, which can cause a crash

func (*Box2dComponent) GetBox2dComponent

func (b *Box2dComponent) GetBox2dComponent() *Box2dComponent

GetBox2dComponent gets the *Box2dComponent from anything with a one, so they can implement the interfaces and AddByInterface can work

type Box2dFace

type Box2dFace interface {
	GetBox2dComponent() *Box2dComponent
}

Box2dFace is an interface for the Box2dComponent

type CollisionEndMessage

type CollisionEndMessage struct {
	Contact box2d.B2ContactInterface
}

CollisionEndMessage is sent out for the box2d collision callback CollisionEnd

func (CollisionEndMessage) Type

func (CollisionEndMessage) Type() string

Type implements the engo.Message interface

type CollisionStartMessage

type CollisionStartMessage struct {
	Contact box2d.B2ContactInterface
}

CollisionStartMessage is sent out for the box2d collision callback CollisionStart

func (CollisionStartMessage) Type

Type implements the engo.Message interface

type CollisionSystem

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

CollisionSystem is a system that handles the callbacks for box2d's collision system. This system does not require the physics system, but a they do need box2d bodies.

func (*CollisionSystem) Add

func (c *CollisionSystem) Add(basic *ecs.BasicEntity, space *common.SpaceComponent, box *Box2dComponent)

Add adds the entity to the collision system. It also adds the body's user data to the BasicEntity's ID, which makes it easy to figure out which entities are which when comparing in the messages / callbacks

func (*CollisionSystem) AddByInterface

func (c *CollisionSystem) AddByInterface(o Collisionable)

AddByInterface adds the entity to the collision system if it implements the Collisionable interface

func (*CollisionSystem) BeginContact

func (c *CollisionSystem) BeginContact(contact box2d.B2ContactInterface)

BeginContact implements the B2ContactListener interface. when a BeginContact callback is made by box2d, it sends a message containing the information from the callback.

func (*CollisionSystem) EndContact

func (c *CollisionSystem) EndContact(contact box2d.B2ContactInterface)

EndContact implements the B2ContactListener interface. when a EndContact callback is made by box2d, it sends a message containing the information from the callback.

func (*CollisionSystem) New

func (c *CollisionSystem) New(w *ecs.World)

New sets the system to the contact listener for box2d, which allows the collision messages to be sent out.

func (*CollisionSystem) PostSolve

func (c *CollisionSystem) PostSolve(contact box2d.B2ContactInterface, impulse *box2d.B2ContactImpulse)

PostSolve implements the B2ContactListener interface. this is called after the solver is finished. When it is called, a message is sent containing the information from the callback

func (*CollisionSystem) PreSolve

func (c *CollisionSystem) PreSolve(contact box2d.B2ContactInterface, oldManifold box2d.B2Manifold)

PreSolve implements the B2ContactListener interface. this is called after a contact is updated but before it goes to the solver. When it is called, a message is sent containing the information from the callback

func (*CollisionSystem) Remove

func (c *CollisionSystem) Remove(basic ecs.BasicEntity)

Remove removes the entity from the system

func (*CollisionSystem) Update

func (c *CollisionSystem) Update(dt float32)

Update doesn't do anything, since the physics engine handles passing out the callbacks.

type Collisionable

type Collisionable interface {
	common.BasicFace
	common.SpaceFace
	Box2dFace
}

Collisionable is for the CollisionSystem's AddByInterface

type Convert

type Convert struct {
	// PixelsPerMeter is how many pixels in the space component are in one meter in the Box2d World
	PixelsPerMeter float32
}

Convert handles conversion between the space component and the Box2d World

func (*Convert) DegToRad

func (c *Convert) DegToRad(d float32) float64

DegToRad converts from degrees to radians

func (*Convert) MetersToPx

func (c *Convert) MetersToPx(m float64) float32

MetersToPx converts from Box2d's meters to the space component's pixels

func (*Convert) PxToMeters

func (c *Convert) PxToMeters(px float32) float64

PxToMeters converts from the space component's px to Box2d's meters

func (*Convert) RadToDeg

func (c *Convert) RadToDeg(r float64) float32

RadToDeg converts from radians to degrees

func (*Convert) ToBox2d2Vec

func (c *Convert) ToBox2d2Vec(pt engo.Point) box2d.B2Vec2

ToBox2d2Vec converts an engo.Point into a box2d.B2Vec2

note that the units are converted, not just copying values

func (*Convert) ToEngoPoint

func (c *Convert) ToEngoPoint(vec box2d.B2Vec2) engo.Point

ToEngoPoint converts a box2d.B2Vec2 into an engo.Point

note that the units are converted, not just copying values

type MouseComponent

type MouseComponent struct {
	// Clicked is true whenever the Mouse was clicked over
	// the entity space in this frame
	Clicked bool
	// Released is true whenever the left mouse button is released over the
	// entity space in this frame
	Released bool
	// Hovered is true whenever the Mouse is hovering
	// the entity space in this frame. This does not necessarily imply that
	// the mouse button was pressed down in your entity space.
	Hovered bool
	// Dragged is true whenever the entity space was left-clicked,
	// and then the mouse started moving (while holding)
	Dragged bool
	// RightClicked is true whenever the entity space was right-clicked
	// in this frame
	RightClicked bool
	// RightDragged is true whenever the entity space was right-clicked,
	// and then the mouse started moving (while holding)
	RightDragged bool
	// RightReleased is true whenever the right mouse button is released over
	// the entity space in this frame. This does not necessarily imply that
	// the mouse button was pressed down in your entity space.
	RightReleased bool
	// Enter is true whenever the Mouse entered the entity space in that frame,
	// but wasn't in that space during the previous frame
	Enter bool
	// Leave is true whenever the Mouse was in the space on the previous frame,
	// but now isn't
	Leave bool
	// Position of the mouse at any moment this is generally used
	// in conjunction with Track = true
	MouseX float32
	MouseY float32
	// Set manually this to true and your mouse component will track the mouse
	// and your entity will always be able to receive an updated mouse
	// component even if its space is not under the mouse cursor
	// WARNING: you MUST know why you want to use this because it will
	// have serious performance impacts if you have many entities with
	// a MouseComponent in tracking mode.
	// This is ideally used for a really small number of entities
	// that must really be aware of the mouse details event when the
	// mouse is not hovering them
	Track bool
	// Modifier is used to store the eventual modifiers that were pressed during
	// the same time the different click events occurred
	Modifier engo.Modifier

	// IsHUDShader is used to update the mouse component properly for the common.HUDShader
	IsHUDShader bool
	// contains filtered or unexported fields
}

MouseComponent is the location for the MouseSystem to store its results; to be used / viewed by other Systems

func (*MouseComponent) GetMouseComponent

func (m *MouseComponent) GetMouseComponent() *MouseComponent

GetMouseComponent gets the *MouseComponent

type MouseFace

type MouseFace interface {
	GetMouseComponent() *MouseComponent
}

MouseFace is an interface for the MouseComponent

type MouseSystem

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

MouseSystem listens for mouse events and changes value for MouseComponent accordingly

func (*MouseSystem) Add

func (m *MouseSystem) Add(basic *ecs.BasicEntity, mouse *MouseComponent, space *common.SpaceComponent, render *common.RenderComponent, box *Box2dComponent)

Add adds a new entity to the MouseSystem

func (*MouseSystem) AddByInterface

func (m *MouseSystem) AddByInterface(o Mouseable)

AddByInterface adds the entity that implements the Mouseable interface to the MouseSystem

func (*MouseSystem) New

func (m *MouseSystem) New(w *ecs.World)

New adds world and camera to the MouseSystem

func (*MouseSystem) Priority

func (m *MouseSystem) Priority() int

Priority implements prioritizer interface

func (*MouseSystem) Remove

func (m *MouseSystem) Remove(basic ecs.BasicEntity)

Remove removes an entity from the MouseSystem

func (*MouseSystem) Update

func (m *MouseSystem) Update(dt float32)

Update updates the MouseComponent based on location of cursor and state of the mouse buttons

type Mouseable

Mouseable is for he MouseSystem's AddByInterface

type PhysicsSystem

type PhysicsSystem struct {
	VelocityIterations, PositionIterations int
	// contains filtered or unexported fields
}

PhysicsSystem provides a system that allows entites to follow the box2d physics engine calculations.

func (*PhysicsSystem) Add

func (b *PhysicsSystem) Add(basic *ecs.BasicEntity, space *common.SpaceComponent, box *Box2dComponent)

Add adds the entity to the physics system An entity needs a github.com/EngoEngine/ecs.BasicEntity, github.com/EngoEngine/engo/common.SpaceComponent, and a Box2dComponent in order to be added to the system

func (*PhysicsSystem) AddByInterface

func (b *PhysicsSystem) AddByInterface(o Physicsable)

AddByInterface adds an entity to the Physics system

func (*PhysicsSystem) Remove

func (b *PhysicsSystem) Remove(basic ecs.BasicEntity)

Remove removes the entity from the physics system.

func (*PhysicsSystem) Update

func (b *PhysicsSystem) Update(dt float32)

Update runs every time the systems update. Updates the box2d world and simulates physics based on the timestep, positions, and forces on the bodies.

type Physicsable

type Physicsable interface {
	common.BasicFace
	common.SpaceFace
	Box2dFace
}

Physicsable is for the PhysicsSystem's AddByInterface

type PostSolveMessage

type PostSolveMessage struct {
	Contact box2d.B2ContactInterface
	Impulse *box2d.B2ContactImpulse
}

PostSolveMessage is sent out after a step of the physics engine

func (PostSolveMessage) Type

func (PostSolveMessage) Type() string

Type implements the engo.Message interface

type PreSolveMessage

type PreSolveMessage struct {
	Contact     box2d.B2ContactInterface
	OldManifold box2d.B2Manifold
}

PreSolveMessage is sent out before a step of the physics engine

func (PreSolveMessage) Type

func (PreSolveMessage) Type() string

Type implements the engo.Message interface

Directories

Path Synopsis
demos

Jump to

Keyboard shortcuts

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