more physics tweaks
This commit is contained in:
parent
88295b720d
commit
5e6bd64fef
1 changed files with 25 additions and 6 deletions
31
game/aw.go
31
game/aw.go
|
@ -30,25 +30,41 @@ func (aw *Awakeman) Update() error {
|
||||||
const (
|
const (
|
||||||
bounceDampen = 0.5
|
bounceDampen = 0.5
|
||||||
gravity = 0.3
|
gravity = 0.3
|
||||||
jumpVelocity = -3.5
|
jumpVelocity = -3.6
|
||||||
runVelocity = 1.4
|
runVelocity = 1.4
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// High-school physics time! Under constant acceleration:
|
||||||
|
// v = v_0 + a*t
|
||||||
|
// and
|
||||||
|
// s = t * (v_0 + v) / 2
|
||||||
|
// (note t is in ticks and s is in world units)
|
||||||
|
// and since we get one Update per tick (t = 1),
|
||||||
|
// v = v_0 + a,
|
||||||
|
// and
|
||||||
|
// s = (v_0 + v) / 2.
|
||||||
|
// Capture current v_0 to use later.
|
||||||
|
ux, uy := aw.vx, aw.vy
|
||||||
|
|
||||||
// Standing on something?
|
// Standing on something?
|
||||||
if aw.CollidesAt(aw.Pos.Add(image.Pt(0, 1))) {
|
if aw.CollidesAt(aw.Pos.Add(image.Pt(0, 1))) {
|
||||||
// Not falling
|
// Not falling. Let's assume aw always lands safely.
|
||||||
|
// Setting a = -v_0 gives v = v_0 - v_0 = 0.
|
||||||
aw.vy = 0
|
aw.vy = 0
|
||||||
aw.coyoteTimer = 5
|
aw.coyoteTimer = 5
|
||||||
} else {
|
} else {
|
||||||
// Falling
|
// Falling. v = v_0 + a, and a is gravity.
|
||||||
aw.vy += gravity
|
aw.vy += gravity
|
||||||
if aw.coyoteTimer > 0 {
|
if aw.coyoteTimer > 0 {
|
||||||
aw.coyoteTimer--
|
aw.coyoteTimer--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle controls
|
||||||
|
|
||||||
// NB: spacebar sometimes does things on web pages (scrolls down)
|
// NB: spacebar sometimes does things on web pages (scrolls down)
|
||||||
if aw.coyoteTimer > 0 && (inpututil.IsKeyJustPressed(ebiten.KeySpace) || inpututil.IsKeyJustPressed(ebiten.KeyZ)) {
|
if aw.coyoteTimer > 0 && (inpututil.IsKeyJustPressed(ebiten.KeySpace) || inpututil.IsKeyJustPressed(ebiten.KeyZ)) {
|
||||||
// Jump
|
// Jump. One frame of a = jumpVelocity (ignoring any gravity already applied this tick).
|
||||||
aw.vy = jumpVelocity
|
aw.vy = jumpVelocity
|
||||||
}
|
}
|
||||||
switch {
|
switch {
|
||||||
|
@ -76,8 +92,11 @@ func (aw *Awakeman) Update() error {
|
||||||
aw.camera.Rotation += math.Pi / 6
|
aw.camera.Rotation += math.Pi / 6
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
aw.MoveX(aw.vx, func() { aw.vx = -aw.vx * bounceDampen })
|
|
||||||
aw.MoveY(aw.vy, func() { aw.vy = -aw.vy * bounceDampen })
|
// s = (v_0 + v) / 2.
|
||||||
|
// On collision, bounce a little bit.
|
||||||
|
aw.MoveX((ux+aw.vx)/2, func() { aw.vx = -aw.vx * bounceDampen })
|
||||||
|
aw.MoveY((uy+aw.vy)/2, func() { aw.vy = -aw.vy * bounceDampen })
|
||||||
// aw.Pos is top-left corner, so add half size to get centre
|
// aw.Pos is top-left corner, so add half size to get centre
|
||||||
aw.camera.Centre = aw.Pos.Add(aw.Size.Div(2))
|
aw.camera.Centre = aw.Pos.Add(aw.Size.Div(2))
|
||||||
return aw.Sprite.Update()
|
return aw.Sprite.Update()
|
||||||
|
|
Loading…
Reference in a new issue