-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (91 loc) · 2.7 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"image/color"
"log"
"math"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
const (
screenWidth = 1200
screenHeight = 720
)
var emptyImage = ebiten.NewImage(3, 3)
func init() {
emptyImage.Fill(color.White)
}
type Game struct {
physicsActive bool
}
func (g *Game) Update() error {
touches := inpututil.AppendJustPressedTouchIDs(nil)
if inpututil.IsKeyJustPressed(ebiten.KeySpace) ||
inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) ||
len(touches) > 0 {
g.physicsActive = !g.physicsActive
if g.physicsActive {
Init()
}
}
if g.physicsActive {
RunStep()
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
for i := 0; i < GetBodiesCount(); i++ {
body := GetBody(i)
if body == nil {
continue
}
vertexCount := GetShapeVerticesCount(i)
for j := 0; j < vertexCount; j++ {
// Get physics bodies shape vertices to draw lines
// Note: GetPhysicsShapeVertex() already calculates rotation transformations
vertexA := GetShapeVertex(body, j)
jj := 0
if j+1 < vertexCount {
jj = j + 1 // Get next vertex or first to close the shape
}
vertexB := GetShapeVertex(body, jj)
ebitenutil.DrawLine(screen,
vertexA.X, vertexA.Y,
vertexB.X, vertexB.Y,
color.RGBA{0, 255, 0, 255},
)
}
}
ebitenutil.DebugPrint(screen, "Press <space> or click to start/stop")
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Physac golang demo")
// Create floor rectangle physics body
floor := CreateBodyRectangle(Vector2{screenWidth / 2, screenHeight * 0.975},
screenWidth*1.3, screenHeight/5, 10)
SetBodyRotation(floor, (math.Pi/180)*3)
floor.Enabled = false // Disable body state to convert it to static (no dynamics, but collisions)
floor.Restitution = 1
floor2 := CreateBodyRectangle(Vector2{screenWidth / 2, screenHeight * 0.975},
screenWidth*1.3, screenHeight/5, 10)
SetBodyRotation(floor2, (math.Pi/180)*-3)
floor2.Enabled = false // Disable body state to convert it to static (no dynamics, but collisions)
floor2.Restitution = 1
circle := CreateBodyCircle(Vector2{screenWidth * 0.10, screenHeight * 0.1},
screenWidth*0.02, 10)
circle.Restitution = 0.5
circle2 := CreateBodyCircle(Vector2{screenWidth * 0.90, screenHeight * 0.2},
screenWidth*0.03, 10)
circle2.Restitution = 0.3
circle3 := CreateBodyCircle(Vector2{screenWidth * 0.50, screenHeight * 0.05},
screenWidth*0.015, 10)
circle3.Restitution = 1
SetGravity(0, 9.81/10)
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}