main.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2018 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "image/color"
  18. "log"
  19. "math/rand/v2"
  20. "github.com/hajimehoshi/ebiten/v2"
  21. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  22. "github.com/jakecoffman/cp/v2"
  23. )
  24. const (
  25. screenWidth = 600
  26. screenHeight = 480
  27. )
  28. var (
  29. dot = ebiten.NewImage(1, 1)
  30. )
  31. func init() {
  32. dot.Fill(color.White)
  33. }
  34. type Game struct {
  35. space *cp.Space
  36. }
  37. func NewGame() *Game {
  38. const (
  39. imageWidth = 188
  40. imageHeight = 35
  41. )
  42. space := cp.NewSpace()
  43. space.Iterations = 1
  44. // The space will contain a very large number of similarly sized objects.
  45. // This is the perfect candidate for using the spatial hash.
  46. // Generally you will never need to do this.
  47. space.UseSpatialHash(2.0, 10000)
  48. var body *cp.Body
  49. var shape *cp.Shape
  50. for y := 0; y < imageHeight; y++ {
  51. for x := 0; x < imageWidth; x++ {
  52. if getPixel(uint(x), uint(y)) == 0 {
  53. continue
  54. }
  55. xJitter := 0.05 * rand.Float64()
  56. yJitter := 0.05 * rand.Float64()
  57. shape = makeBall(2.0*(float64(x)+imageWidth/2+xJitter)-75, 2*(imageHeight/2.0+float64(y)+yJitter)+150)
  58. space.AddBody(shape.Body())
  59. space.AddShape(shape)
  60. }
  61. }
  62. body = space.AddBody(cp.NewBody(1e9, cp.INFINITY))
  63. body.SetPosition(cp.Vector{X: -1000, Y: 225})
  64. body.SetVelocity(400, 0)
  65. shape = space.AddShape(cp.NewCircle(body, 8, cp.Vector{}))
  66. shape.SetElasticity(0)
  67. shape.SetFriction(0)
  68. return &Game{
  69. space: space,
  70. }
  71. }
  72. func (g *Game) Update() error {
  73. g.space.Step(1.0 / float64(ebiten.TPS()))
  74. return nil
  75. }
  76. func (g *Game) Draw(screen *ebiten.Image) {
  77. screen.Fill(color.Black)
  78. op := &ebiten.DrawImageOptions{}
  79. op.ColorScale.Scale(200.0/255.0, 200.0/255.0, 200.0/255.0, 1)
  80. g.space.EachBody(func(body *cp.Body) {
  81. op.GeoM.Reset()
  82. op.GeoM.Translate(body.Position().X, body.Position().Y)
  83. screen.DrawImage(dot, op)
  84. })
  85. ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.ActualTPS()))
  86. }
  87. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  88. return screenWidth, screenHeight
  89. }
  90. func getPixel(x, y uint) int {
  91. const imageRowLength = 24
  92. return (imageBitmap[(x>>3)+y*imageRowLength] >> (^x & 0x7)) & 1
  93. }
  94. func makeBall(x, y float64) *cp.Shape {
  95. body := cp.NewBody(1.0, cp.INFINITY)
  96. body.SetPosition(cp.Vector{X: x, Y: y})
  97. shape := cp.NewCircle(body, 0.95, cp.Vector{})
  98. shape.SetElasticity(0)
  99. shape.SetFriction(0)
  100. return shape
  101. }
  102. var imageBitmap = []int{
  103. 15, -16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, -64, 15, 63, -32, -2, 0, 0, 0, 0, 0, 0, 0,
  104. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, -64, 15, 127, -125, -1, -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  105. 0, 0, 0, 127, -64, 15, 127, 15, -1, -64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, -64, 15, -2,
  106. 31, -1, -64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, -64, 0, -4, 63, -1, -32, 0, 0, 0, 0, 0, 0,
  107. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, -64, 15, -8, 127, -1, -32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  108. 1, -1, -64, 0, -8, -15, -1, -32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -31, -1, -64, 15, -8, -32,
  109. -1, -32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, -15, -1, -64, 9, -15, -32, -1, -32, 0, 0, 0, 0, 0,
  110. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, -15, -1, -64, 0, -15, -32, -1, -32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  111. 0, 0, 63, -7, -1, -64, 9, -29, -32, 127, -61, -16, 63, 15, -61, -1, -8, 31, -16, 15, -8, 126, 7, -31,
  112. -8, 31, -65, -7, -1, -64, 9, -29, -32, 0, 7, -8, 127, -97, -25, -1, -2, 63, -8, 31, -4, -1, 15, -13,
  113. -4, 63, -1, -3, -1, -64, 9, -29, -32, 0, 7, -8, 127, -97, -25, -1, -2, 63, -8, 31, -4, -1, 15, -13,
  114. -2, 63, -1, -3, -1, -64, 9, -29, -32, 0, 7, -8, 127, -97, -25, -1, -1, 63, -4, 63, -4, -1, 15, -13,
  115. -2, 63, -33, -1, -1, -32, 9, -25, -32, 0, 7, -8, 127, -97, -25, -1, -1, 63, -4, 63, -4, -1, 15, -13,
  116. -1, 63, -33, -1, -1, -16, 9, -25, -32, 0, 7, -8, 127, -97, -25, -1, -1, 63, -4, 63, -4, -1, 15, -13,
  117. -1, 63, -49, -1, -1, -8, 9, -57, -32, 0, 7, -8, 127, -97, -25, -8, -1, 63, -2, 127, -4, -1, 15, -13,
  118. -1, -65, -49, -1, -1, -4, 9, -57, -32, 0, 7, -8, 127, -97, -25, -8, -1, 63, -2, 127, -4, -1, 15, -13,
  119. -1, -65, -57, -1, -1, -2, 9, -57, -32, 0, 7, -8, 127, -97, -25, -8, -1, 63, -2, 127, -4, -1, 15, -13,
  120. -1, -1, -57, -1, -1, -1, 9, -57, -32, 0, 7, -1, -1, -97, -25, -8, -1, 63, -1, -1, -4, -1, 15, -13, -1,
  121. -1, -61, -1, -1, -1, -119, -57, -32, 0, 7, -1, -1, -97, -25, -8, -1, 63, -1, -1, -4, -1, 15, -13, -1,
  122. -1, -61, -1, -1, -1, -55, -49, -32, 0, 7, -1, -1, -97, -25, -8, -1, 63, -1, -1, -4, -1, 15, -13, -1,
  123. -1, -63, -1, -1, -1, -23, -49, -32, 127, -57, -1, -1, -97, -25, -1, -1, 63, -1, -1, -4, -1, 15, -13,
  124. -1, -1, -63, -1, -1, -1, -16, -49, -32, -1, -25, -1, -1, -97, -25, -1, -1, 63, -33, -5, -4, -1, 15,
  125. -13, -1, -1, -64, -1, -9, -1, -7, -49, -32, -1, -25, -8, 127, -97, -25, -1, -1, 63, -33, -5, -4, -1,
  126. 15, -13, -1, -1, -64, -1, -13, -1, -32, -49, -32, -1, -25, -8, 127, -97, -25, -1, -2, 63, -49, -13,
  127. -4, -1, 15, -13, -1, -1, -64, 127, -7, -1, -119, -17, -15, -1, -25, -8, 127, -97, -25, -1, -2, 63,
  128. -49, -13, -4, -1, 15, -13, -3, -1, -64, 127, -8, -2, 15, -17, -1, -1, -25, -8, 127, -97, -25, -1,
  129. -8, 63, -49, -13, -4, -1, 15, -13, -3, -1, -64, 63, -4, 120, 0, -17, -1, -1, -25, -8, 127, -97, -25,
  130. -8, 0, 63, -57, -29, -4, -1, 15, -13, -4, -1, -64, 63, -4, 0, 15, -17, -1, -1, -25, -8, 127, -97,
  131. -25, -8, 0, 63, -57, -29, -4, -1, -1, -13, -4, -1, -64, 31, -2, 0, 0, 103, -1, -1, -57, -8, 127, -97,
  132. -25, -8, 0, 63, -57, -29, -4, -1, -1, -13, -4, 127, -64, 31, -2, 0, 15, 103, -1, -1, -57, -8, 127,
  133. -97, -25, -8, 0, 63, -61, -61, -4, 127, -1, -29, -4, 127, -64, 15, -8, 0, 0, 55, -1, -1, -121, -8,
  134. 127, -97, -25, -8, 0, 63, -61, -61, -4, 127, -1, -29, -4, 63, -64, 15, -32, 0, 0, 23, -1, -2, 3, -16,
  135. 63, 15, -61, -16, 0, 31, -127, -127, -8, 31, -1, -127, -8, 31, -128, 7, -128, 0, 0,
  136. }
  137. func main() {
  138. ebiten.SetWindowSize(screenWidth, screenHeight)
  139. ebiten.SetWindowTitle("Ebiten")
  140. if err := ebiten.RunGame(NewGame()); err != nil {
  141. log.Fatal(err)
  142. }
  143. }