From 4a50fe07a2dcc72ba9a5dd4cb920c7a39d80832f Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Sat, 7 Aug 2021 21:23:23 -0400 Subject: [PATCH] add some comments --- cmd/root.go | 1 + physics/physics.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/cmd/root.go b/cmd/root.go index 0ae9cbf..eb75e57 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -45,6 +45,7 @@ func init() { rootCmd.AddCommand(fireworksCmd) } +// Execute starts the confetty command-line interface func Execute() { if err := rootCmd.Execute(); err != nil { fmt.Fprintln(os.Stderr, err) diff --git a/physics/physics.go b/physics/physics.go index f38313e..eaacdeb 100644 --- a/physics/physics.go +++ b/physics/physics.go @@ -4,32 +4,46 @@ import ( "math" ) +// Position is the location of an object on a 2-dimensional plane type Position Point + +// Velocity is the velocity vector of an object's motion type Velocity Vector + +// Acceleration is the acceleration vector of an object's motion type Acceleration Vector +// Gravity is the acceleration of gravity +// Downward (+) by g m/s² var Gravity = Acceleration{ X: 0, Y: 9.81, } +// Motion represents an objects motion +// it keeps track of the position, velocity, and acceleration type Motion struct { pos Position vel Velocity acc Acceleration } +// 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 type Physics struct { current Motion initial Motion fps float64 } +// Vector represents a magnitude and a direction in the form of a Point +// from the origin (0, 0) type Vector struct { X float64 Y float64 } +// Point is a coordinate on a 2-dimensional plane type Point struct { X float64 Y float64 @@ -40,6 +54,7 @@ func (a Point) Distance(b Point) float64 { return math.Sqrt(math.Pow(b.X-a.X, 2) + math.Pow(b.Y-a.Y, 2)) } +// New initialize a physics simulation with simple motion func New(pos Point, vel, acc Vector, fps float64) *Physics { motion := Motion{ pos: Position(pos), @@ -53,10 +68,13 @@ func New(pos Point, vel, acc Vector, fps float64) *Physics { } } +// Reset resets the current motion back to the initial func (p *Physics) Reset() { p.current = p.initial } +// Update increases the position of the motion by the velocity +// and increases the velocity by the acceleration func (p *Physics) Update() { p.current.pos.X += p.current.vel.X / p.fps p.current.pos.Y += p.current.vel.Y / p.fps @@ -65,14 +83,19 @@ func (p *Physics) Update() { p.current.vel.Y += p.current.acc.Y / p.fps } +// Displacement calculates the displacement between the current position and +// its initial position func (p Physics) Displacement() float64 { return Point(p.initial.pos).Distance(Point(p.current.pos)) } +// PosX returns the integer value of the current x coordinate for motion +// not to be confused with Posix :D func (p Physics) PosX() int { return int(math.Round(p.current.pos.X)) } +// PosY returns the integer value of the current y coordinate for motion func (p Physics) PosY() int { return int(math.Round(p.current.pos.Y)) }