-
Notifications
You must be signed in to change notification settings - Fork 0
/
circle_to_convex.go
100 lines (79 loc) · 2.09 KB
/
circle_to_convex.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
package main
import (
"time"
"golang.org/x/image/colornames"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"github.com/hueypark/physics/core"
"github.com/hueypark/physics/core/body"
"github.com/hueypark/physics/core/math/rotator"
"github.com/hueypark/physics/core/math/vector"
"github.com/hueypark/physics/core/shape/circle"
"github.com/hueypark/physics/core/shape/convex"
"github.com/hueypark/physics/examples/util"
)
const WINDOW_WIDTH = 1024
const WINDOW_HEIGHT = 768
func main() {
pixelgl.Run(run)
}
func run() {
cfg := pixelgl.WindowConfig{
Title: "Circle to Convex",
Bounds: pixel.R(-WINDOW_WIDTH/2, -WINDOW_HEIGHT/2, WINDOW_WIDTH/2, WINDOW_HEIGHT/2),
VSync: true,
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
imd := imdraw.New(nil)
world := physics.New()
circleA := body.New()
circleA.SetStatic()
circleA.SetShape(circle.New(50))
circleA.SetPosition(vector.ZERO())
world.Add(circleA)
convexB := body.New()
convexB.SetStatic()
convexB.SetShape(convex.New([]vector.Vector{{-50, -50}, {-100, 0}, {70, 70}, {50, -50}, {50, 50}, {-50, 50}}))
convexB.SetPosition(vector.Vector{100, 0})
convexB.SetRotation(rotator.Rotator{30})
world.Add(convexB)
delta := time.Second / 30
ticker := time.NewTicker(delta)
leftButtonClicked := false
rightButtonClicked := false
for range ticker.C {
if win.Closed() {
break
}
if win.JustPressed(pixelgl.MouseButtonLeft) {
leftButtonClicked = true
}
if win.JustReleased(pixelgl.MouseButtonLeft) {
leftButtonClicked = false
}
if leftButtonClicked {
pos := win.MousePosition()
circleA.SetPosition(vector.Vector{pos.X, pos.Y})
}
if win.JustPressed(pixelgl.MouseButtonRight) {
rightButtonClicked = true
}
if win.JustReleased(pixelgl.MouseButtonRight) {
rightButtonClicked = false
}
if rightButtonClicked {
pos := win.MousePosition()
convexB.SetPosition(vector.Vector{pos.X, pos.Y})
}
win.Clear(colornames.Black)
imd.Clear()
util.DrawWorld(imd, world)
world.Tick(delta.Seconds())
imd.Draw(win)
win.Update()
}
}