commit 1fc812dd2644908cc987b2236a93c4512f4b5588
parent 98c18dde4f648fec2d16a94fc0b4aeff78eeb727
Author: bsandro <brian.drosan@gmail.com>
Date: Sat, 14 Nov 2020 18:49:16 +0300
bouncing works wrong
Diffstat:
D | ebitest | | | 0 | |
M | ebitest.go | | | 77 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------- |
2 files changed, 55 insertions(+), 22 deletions(-)
diff --git a/ebitest b/ebitest
Binary files differ.
diff --git a/ebitest.go b/ebitest.go
@@ -8,10 +8,14 @@ import (
_ "image"
_ "image/png"
"log"
+ "math"
"math/rand"
"time"
)
+const S_FRAMERATE = 60
+const S_SPEED = 100.0
+
var ScreenSize struct {
width, height int
}
@@ -20,9 +24,10 @@ var ScreenSize struct {
var BakaHa *ebiten.Image
type Sprite struct {
- img *ebiten.Image
- opt ebiten.DrawImageOptions
- x, y, vx, vy int
+ img *ebiten.Image
+ opt ebiten.DrawImageOptions
+ x, y float64
+ angle float64
}
type Game struct {
@@ -47,25 +52,49 @@ func (g *Game) Update() error {
}
func (sprite *Sprite) Update() error {
- w, _ := sprite.img.Size()
+ wInt, hInt := sprite.img.Size()
+ w, h := float64(wInt), float64(hInt)
fps := ebiten.CurrentFPS()
if fps == 0 {
- fps = 60.0
+ fps = S_FRAMERATE
}
- //fmt.Printf("sprite x = %d, vx = %d, width = %d, fps = %0.2f\n", sprite.x, sprite.vx, w, fps)
+
+ delta := S_SPEED / fps
+ dx := delta * math.Cos(sprite.angle)
+ dy := delta * math.Sin(sprite.angle)
+
+ fmt.Printf("dx=%0.2f dy=%0.2f angle=%0.2f\n", dx, dy, sprite.angle)
+
+ x := sprite.x + dx
+ y := sprite.y + dy
+
+ fmt.Printf("x=%0.2f y=%0.2f, +size: x=%0.2f y=%0.2f\n", x, y, x+w, y+h)
+
+ scrW, scrH := float64(ScreenSize.width), float64(ScreenSize.height)
+
// bouncing off walls
- if sprite.x + w >= ScreenSize.width || sprite.x <= 0 {
+ if x+w >= scrW || x <= 0 || y+h >= scrH || y <= 0 {
fmt.Println("bounce")
- sprite.vx *= -1
- }
- // correcting speed for the framerate value
- dx := float64(sprite.vx) / fps
- //fmt.Printf("dx = %0.2f\n", dx)
- sprite.x += int(dx)
-
- if sprite.x < 0 {
- fmt.Printf("Error! x = %d\n", sprite.x)
- sprite.x = 0
+ //sprite.angle = math.Pi*2 + math.Mod(math.Pi-sprite.angle, math.Pi / 2.0)
+ sprite.angle = math.Pi * 0.75 + sprite.angle
+
+ sprite.angle = math.Mod(sprite.angle, math.Pi*2.0)
+
+ dx = delta * math.Cos(sprite.angle)
+ dy = delta * math.Sin(sprite.angle)
+ x = sprite.x + dx
+ y = sprite.y + dy
+
+ if x+w >= scrW || x <= 0 {
+ sprite.x -= dx
+ }
+ if y+h >= scrH || y <= 0 {
+ sprite.y -= dy
+ }
+
+ } else {
+ sprite.x = x
+ sprite.y = y
}
return nil
@@ -95,11 +124,14 @@ func (g *Game) init() {
fmt.Println("Game::init()")
rand.Seed(time.Now().UnixNano())
-
- for i := 0; i < 5; i++ {
- randX := rand.Intn(100)
- fmt.Println("randX := ", randX)
- g.sprites = append(g.sprites, &Sprite{BakaHa, ebiten.DrawImageOptions{}, randX, i * 100, 100, 0})
+ numSprites := 1
+
+ for i := 0; i < numSprites; i++ {
+ x := float64(ScreenSize.width) / 2.0
+ y := float64(ScreenSize.height) / 2.0
+ angle := rand.Float64() * math.Pi * 2.0
+ fmt.Printf("initial x, y, angle: %0.2f %0.2f %0.2f\n", x, y, angle)
+ g.sprites = append(g.sprites, &Sprite{BakaHa, ebiten.DrawImageOptions{}, x, y, angle})
}
}
@@ -109,6 +141,7 @@ func main() {
game := &Game{}
ebiten.SetWindowSize(ScreenSize.width, ScreenSize.height)
ebiten.SetWindowTitle("ebiten subgame")
+ ebiten.SetMaxTPS(S_FRAMERATE)
if err := ebiten.RunGame(game); err != nil {
log.Fatal(err)
}