physics

package
v0.0.0-...-468adaa Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2019 License: Zlib Imports: 3 Imported by: 0

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 Close

func Close()

Close - Unitializes physics pointers

func DestroyBody

func DestroyBody(body *Body) bool

DestroyBody - Unitializes and destroys a physics body

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

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 SetPhysicsTimeStep

func SetPhysicsTimeStep(delta float32)

Sets physics fixed time step in milliseconds. 1.666666 by default

func Update

func Update()

Update - Physics steps calculations (dynamics, collisions and position corrections)

Types

type Body

type Body struct {
	// 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) AddForce

func (b *Body) AddForce(force rl.Vector2)

AddForce - Adds a force to a physics body

func (*Body) AddTorque

func (b *Body) AddTorque(amount float32)

AddTorque - Adds an angular force to 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)

SetRotation - Sets physics body shape transform based on radians parameter

func (*Body) Shatter

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

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

type Polygon

type Polygon struct {
	// Current used vertex and normals count
	VertexCount int
	// Polygon vertex positions vectors
	Vertices [maxVertices]rl.Vector2
	// Polygon vertex normals vectors
	Normals [maxVertices]rl.Vector2
	// Vertices transform matrix 2x2
	Transform rl.Mat2
}

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
	// 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 (Circle or Polygon)

Jump to

Keyboard shortcuts

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