physics

package module
v0.0.0-...-f406ea4 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Zlib Imports: 4 Imported by: 96

README

physics GoDoc

2D Physics library for videogames.

A port of Victor Fisac's physac engine.

Documentation

Overview

Package physics - 2D Physics library for videogames

A port of Victor Fisac's physac engine (https://github.com/raysan5/raylib/blob/master/src/physac.h)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddForce

func AddForce(body *Body, force rl.Vector2)

AddForce - Adds a force to a physics body

func AddTorque

func AddTorque(body *Body, amount float32)

AddTorque - Adds an angular force to a physics body

func Close

func Close()

Close - Unitializes physics pointers

func GetBodiesCount

func GetBodiesCount() int

GetBodiesCount - Returns the current amount of created physics bodies

func GetShapeVerticesCount

func GetShapeVerticesCount(index int) int

GetShapeVerticesCount - Returns the amount of vertices of a physics body shape

func Init

func Init()

Init - Initializes physics values, pointers and creates physics loop thread

func Reset

func Reset()

Reset - Destroys created physics bodies and manifolds

func SetGravity

func SetGravity(x, y float32)

SetGravity - Sets physics global gravity force

func SetTimeStep

func SetTimeStep(delta float32)

SetTimeStep - Sets physics fixed time step in milliseconds. 1.666666 by default

func Shatter

func Shatter(body *Body, position rl.Vector2, force float32)

Shatter - Shatters a polygon shape physics body to little physics bodies with explosion force

func Update

func Update()

Update - Runs physics step

Types

type Body

type Body struct {
	// Reference unique identifier
	ID int
	// Enabled dynamics state (collisions are calculated anyway)
	Enabled bool
	// Physics body shape pivot
	Position rl.Vector2
	// Current linear velocity applied to position
	Velocity rl.Vector2
	// Current linear force (reset to 0 every step)
	Force rl.Vector2
	// Current angular velocity applied to orient
	AngularVelocity float32
	// Current angular force (reset to 0 every step)
	Torque float32
	// Rotation in radians
	Orient float32
	// Moment of inertia
	Inertia float32
	// Inverse value of inertia
	InverseInertia float32
	// Physics body mass
	Mass float32
	// Inverse value of mass
	InverseMass float32
	// Friction when the body has not movement (0 to 1)
	StaticFriction float32
	// Friction when the body has movement (0 to 1)
	DynamicFriction float32
	// Restitution coefficient of the body (0 to 1)
	Restitution float32
	// Apply gravity force to dynamics
	UseGravity bool
	// Physics grounded on other body state
	IsGrounded bool
	// Physics rotation constraint
	FreezeOrient bool
	// Physics body shape information (type, radius, vertices, normals)
	Shape Shape
}

Body type

func GetBodies

func GetBodies() []*Body

GetBodies - Returns the slice of created physics bodies

func GetBody

func GetBody(index int) *Body

GetBody - Returns a physics body of the bodies pool at a specific index

func NewBodyCircle

func NewBodyCircle(pos rl.Vector2, radius, density float32) *Body

NewBodyCircle - Creates a new circle physics body with generic parameters

func NewBodyPolygon

func NewBodyPolygon(pos rl.Vector2, radius float32, sides int, density float32) *Body

NewBodyPolygon - Creates a new polygon physics body with generic parameters

func NewBodyRectangle

func NewBodyRectangle(pos rl.Vector2, width, height, density float32) *Body

NewBodyRectangle - Creates a new rectangle physics body with generic parameters

func (*Body) Destroy

func (b *Body) Destroy()

Destroy - Unitializes and destroy a physics body

func (*Body) GetShapeVertex

func (b *Body) GetShapeVertex(vertex int) rl.Vector2

GetShapeVertex - Returns transformed position of a body shape (body position + vertex transformed position)

func (*Body) SetRotation

func (b *Body) SetRotation(radians float32)

SetBodyRotation - Sets physics body shape transform based on radians parameter

type Manifold

type Manifold struct {
	// Reference unique identifier
	ID int
	// Manifold first physics body reference
	BodyA *Body
	// Manifold second physics body reference
	BodyB *Body
	// Depth of penetration from collision
	Penetration float32
	// Normal direction vector from 'a' to 'b'
	Normal rl.Vector2
	// Points of contact during collision
	Contacts [2]rl.Vector2
	// Current collision number of contacts
	ContactsCount int
	// Mixed restitution during collision
	Restitution float32
	// Mixed dynamic friction during collision
	DynamicFriction float32
	// Mixed static friction during collision
	StaticFriction float32
}

Manifold type

type Polygon

type Polygon struct {
	// Current used vertex and normals count
	VertexCount int
	// Polygon vertex positions vectors
	Positions [maxVertices]rl.Vector2
	// Polygon vertex normals vectors
	Normals [maxVertices]rl.Vector2
}

Polygon type

type Shape

type Shape struct {
	// Physics shape type (circle or polygon)
	Type ShapeType
	// Shape physics body reference
	Body *Body
	// Circle shape radius (used for circle shapes)
	Radius float32
	// Vertices transform matrix 2x2
	Transform rl.Mat2
	// Polygon shape vertices position and normals data (just used for polygon shapes)
	VertexData Polygon
}

Shape type

type ShapeType

type ShapeType int

ShapeType type

const (
	// Circle type
	CircleShape ShapeType = iota
	// Polygon type
	PolygonShape
)

Physics shape types

func GetShapeType

func GetShapeType(index int) ShapeType

GetShapeType - Returns the physics body shape type (PHYSICS_CIRCLE or PHYSICS_POLYGON)

Jump to

Keyboard shortcuts

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