confetty/physics/physics.go

102 lines
2.4 KiB
Go
Raw Normal View History

2021-08-07 02:00:08 +00:00
package physics
2021-08-07 18:43:40 +00:00
import (
"math"
)
2021-08-07 02:00:08 +00:00
2021-08-08 01:23:23 +00:00
// Position is the location of an object on a 2-dimensional plane
2021-08-07 21:45:59 +00:00
type Position Point
2021-08-08 01:23:23 +00:00
// Velocity is the velocity vector of an object's motion
2021-08-07 02:00:08 +00:00
type Velocity Vector
2021-08-08 01:23:23 +00:00
// Acceleration is the acceleration vector of an object's motion
2021-08-07 02:00:08 +00:00
type Acceleration Vector
2021-08-08 01:23:23 +00:00
// Gravity is the acceleration of gravity
// Downward (+) by g m/s²
2021-08-07 02:00:08 +00:00
var Gravity = Acceleration{
X: 0,
Y: 9.81,
}
2021-08-08 01:23:23 +00:00
// Motion represents an objects motion
// it keeps track of the position, velocity, and acceleration
2021-08-07 18:43:40 +00:00
type Motion struct {
2021-08-07 02:00:08 +00:00
pos Position
vel Velocity
acc Acceleration
2021-08-07 18:43:40 +00:00
}
2021-08-08 01:23:23 +00:00
// Physics tracks the current motion and initial motion of an object along with
// fps to account for the Update in frames rather than per second
2021-08-07 18:43:40 +00:00
type Physics struct {
current Motion
initial Motion
fps float64
2021-08-07 02:00:08 +00:00
}
2021-08-08 01:23:23 +00:00
// Vector represents a magnitude and a direction in the form of a Point
// from the origin (0, 0)
2021-08-07 02:00:08 +00:00
type Vector struct {
X float64
Y float64
}
2021-08-08 01:23:23 +00:00
// Point is a coordinate on a 2-dimensional plane
2021-08-07 21:45:59 +00:00
type Point struct {
X float64
Y float64
}
// Distance calculates the euclidean distance between two points
func (a Point) Distance(b Point) float64 {
return math.Sqrt(math.Pow(b.X-a.X, 2) + math.Pow(b.Y-a.Y, 2))
}
2021-08-08 01:23:23 +00:00
// New initialize a physics simulation with simple motion
2021-08-07 21:45:59 +00:00
func New(pos Point, vel, acc Vector, fps float64) *Physics {
2021-08-07 18:43:40 +00:00
motion := Motion{
2021-08-07 02:00:08 +00:00
pos: Position(pos),
vel: Velocity(vel),
acc: Acceleration(acc),
}
2021-08-07 18:43:40 +00:00
return &Physics{
initial: motion,
current: motion,
fps: fps,
}
}
2021-08-08 01:23:23 +00:00
// Reset resets the current motion back to the initial
2021-08-07 18:43:40 +00:00
func (p *Physics) Reset() {
p.current = p.initial
2021-08-07 02:00:08 +00:00
}
2021-08-08 01:23:23 +00:00
// Update increases the position of the motion by the velocity
// and increases the velocity by the acceleration
2021-08-07 02:00:08 +00:00
func (p *Physics) Update() {
2021-08-08 00:04:23 +00:00
p.current.pos.X += p.current.vel.X / p.fps
p.current.pos.Y += p.current.vel.Y / p.fps
p.current.vel.X += p.current.acc.X / p.fps
p.current.vel.Y += p.current.acc.Y / p.fps
2021-08-07 02:00:08 +00:00
}
2021-08-08 01:23:23 +00:00
// Displacement calculates the displacement between the current position and
// its initial position
2021-08-07 21:45:59 +00:00
func (p Physics) Displacement() float64 {
return Point(p.initial.pos).Distance(Point(p.current.pos))
}
2021-08-08 01:23:23 +00:00
// PosX returns the integer value of the current x coordinate for motion
// not to be confused with Posix :D
2021-08-07 02:00:08 +00:00
func (p Physics) PosX() int {
2021-08-07 18:43:40 +00:00
return int(math.Round(p.current.pos.X))
2021-08-07 02:00:08 +00:00
}
2021-08-08 01:23:23 +00:00
// PosY returns the integer value of the current y coordinate for motion
2021-08-07 02:00:08 +00:00
func (p Physics) PosY() int {
2021-08-07 18:43:40 +00:00
return int(math.Round(p.current.pos.Y))
2021-08-07 02:00:08 +00:00
}