88 lines
1.9 KiB
Go
88 lines
1.9 KiB
Go
package physics
|
|
|
|
import rl "github.com/gen2brain/raylib-go/raylib"
|
|
|
|
// Object defines the interface for physical objects.
|
|
//
|
|
// This is defined here in the object.go file because after
|
|
// passing an 'object' to an 'entity' (e.g. 'penguin'),
|
|
// then to a 'player', as well as the main function,
|
|
// we needed almost all of the functions for the whole
|
|
// interface defined in each place.
|
|
type Object interface {
|
|
Update() error
|
|
|
|
GetPosition() rl.Vector2
|
|
GetVelocity() rl.Vector2
|
|
GetAcceleration() rl.Vector2
|
|
GetBaseAcceleration() rl.Vector2
|
|
|
|
SetPosition(pos rl.Vector2)
|
|
SetVelocity(vel rl.Vector2)
|
|
SetAcceleration(acc rl.Vector2)
|
|
SetBaseAcceleration(base rl.Vector2)
|
|
}
|
|
|
|
type object struct {
|
|
position rl.Vector2
|
|
velocity rl.Vector2
|
|
acceleration rl.Vector2
|
|
baseAcceleration rl.Vector2 // some default speed for this object
|
|
}
|
|
|
|
func NewObject() *object {
|
|
pos := rl.Vector2{X: 0.0, Y: 0.0}
|
|
vel := rl.Vector2{X: 0.0, Y: 0.0}
|
|
acc := rl.Vector2{X: 0.0, Y: 0.0}
|
|
base := rl.Vector2{X: 1.0, Y: 1.0}
|
|
|
|
return &object{
|
|
position: pos,
|
|
velocity: vel,
|
|
acceleration: acc,
|
|
baseAcceleration: base,
|
|
}
|
|
}
|
|
|
|
func (o *object) Update() error {
|
|
p := o.position
|
|
v := o.velocity
|
|
a := o.acceleration
|
|
|
|
v = rl.Vector2Add(v, a)
|
|
o.velocity = v
|
|
|
|
p = rl.Vector2Add(p, v)
|
|
o.position = p
|
|
|
|
return nil
|
|
}
|
|
|
|
func (o *object) SetPosition(pos rl.Vector2) {
|
|
o.position = pos
|
|
}
|
|
|
|
func (o *object) SetVelocity(vel rl.Vector2) {
|
|
o.velocity = vel
|
|
}
|
|
func (o *object) SetAcceleration(acc rl.Vector2) {
|
|
o.acceleration = acc
|
|
}
|
|
|
|
func (o *object) SetBaseAcceleration(base rl.Vector2) {
|
|
o.baseAcceleration = base
|
|
}
|
|
|
|
func (o *object) GetPosition() rl.Vector2 {
|
|
return o.position
|
|
}
|
|
func (o *object) GetVelocity() rl.Vector2 {
|
|
return o.velocity
|
|
}
|
|
func (o *object) GetAcceleration() rl.Vector2 {
|
|
return o.acceleration
|
|
}
|
|
func (o *object) GetBaseAcceleration() rl.Vector2 {
|
|
return o.baseAcceleration
|
|
}
|