89 lines
1.9 KiB
Go
89 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"math"
|
|
|
|
"github.com/solarlune/tetra3d"
|
|
)
|
|
|
|
type garbageTruck struct {
|
|
x float64
|
|
y float64
|
|
xVelocity float64
|
|
yVelocity float64
|
|
power float64
|
|
reverse float64
|
|
angle float64
|
|
angularVelocity float64
|
|
|
|
MoveX int // -1 left, 0 center, 1 right
|
|
MoveY int // -1 backward, 0 center, 1 forward
|
|
Braking bool
|
|
|
|
Model *tetra3d.Model
|
|
}
|
|
|
|
// Update updates the garbage truck. The code for handling vehicle physics was
|
|
// copied and adapted from the following repository:
|
|
// https://github.com/pakastin/car (MIT license)
|
|
func (t *garbageTruck) Update() {
|
|
const (
|
|
maxPower = 0.025
|
|
maxReverse = 0.025
|
|
powerFactor = 0.00005
|
|
reverseFactor = 0.000075
|
|
drag = 0.095
|
|
angularDrag = 0.9
|
|
turnSpeed = 0.001
|
|
)
|
|
|
|
isDriving := true
|
|
if isDriving { // TODO
|
|
if t.Braking {
|
|
t.MoveY = 0
|
|
}
|
|
|
|
if t.MoveY < 0 {
|
|
t.power += powerFactor
|
|
} else {
|
|
t.power -= powerFactor
|
|
}
|
|
if t.MoveY > 0 {
|
|
t.reverse += reverseFactor
|
|
} else {
|
|
t.reverse -= reverseFactor
|
|
}
|
|
|
|
if t.Braking {
|
|
const adjustment = 0.9975
|
|
t.power = t.power * adjustment
|
|
t.reverse = t.reverse * adjustment
|
|
}
|
|
|
|
t.power = math.Max(0, math.Min(maxPower, t.power))
|
|
t.reverse = math.Max(0, math.Min(maxReverse, t.reverse))
|
|
|
|
direction := 1.0
|
|
if t.power <= t.reverse {
|
|
direction = -1.0
|
|
}
|
|
|
|
const turnThreshold = 0.0002
|
|
if t.xVelocity < -turnThreshold || t.xVelocity > turnThreshold || t.yVelocity < -turnThreshold || t.yVelocity > turnThreshold {
|
|
if t.MoveX < 0 {
|
|
t.angularVelocity -= direction * turnSpeed
|
|
} else if t.MoveX > 0 {
|
|
t.angularVelocity += direction * turnSpeed
|
|
}
|
|
}
|
|
|
|
t.xVelocity += math.Sin(t.angle) * (t.power - t.reverse)
|
|
t.yVelocity += math.Cos(t.angle) * (t.power - t.reverse)
|
|
t.x += t.xVelocity
|
|
t.y -= t.yVelocity
|
|
t.xVelocity *= drag
|
|
t.yVelocity *= drag
|
|
t.angle += t.angularVelocity
|
|
t.angularVelocity *= angularDrag
|
|
}
|
|
}
|