-
Notifications
You must be signed in to change notification settings - Fork 0
/
falling.go
102 lines (92 loc) · 2.43 KB
/
falling.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
package main
import (
"github.com/clambin/pixelmunk"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"github.com/vova616/chipmunk/vect"
"golang.org/x/image/colornames"
"math"
)
func main() {
w := createWorld(1024, 1080)
pixelgl.Run(w.Run)
}
func createWorld(x, y float64) (world *pixelmunk.World) {
world = pixelmunk.NewWorld("falling blocks", 0, 0, x, y)
world.Space.Gravity = vect.Vect{Y: -981}
// Floor
world.Add(pixelmunk.NewBox(pixelmunk.ObjectOptions{
Color: colornames.Blue,
BodyOptions: pixelmunk.ObjectBodyOptions{
StaticBody: true,
Position: vect.Vect{X: vect.Float(x) / 2, Y: 20},
Mass: 10e3,
Elasticity: 0.1,
Friction: 1.0,
BoxOptions: pixelmunk.ObjectBoxOptions{
Width: vect.Float(x) - 200,
Height: 40,
},
},
}))
// Flat box
world.Add(pixelmunk.NewBox(pixelmunk.ObjectOptions{
Color: colornames.Green,
CustomDrawFunc: []pixelmunk.CustomDrawFunc{drawVelocity},
BodyOptions: pixelmunk.ObjectBodyOptions{
Position: vect.Vect{X: 500, Y: 200},
Mass: 10e10,
Elasticity: 0.2,
Friction: 0.1,
BoxOptions: pixelmunk.ObjectBoxOptions{
Width: 500,
Height: 25,
},
},
}))
// First falling object
world.Add(pixelmunk.NewBox(pixelmunk.ObjectOptions{
Color: colornames.Red,
CustomDrawFunc: []pixelmunk.CustomDrawFunc{drawVelocity},
BodyOptions: pixelmunk.ObjectBodyOptions{
Position: vect.Vect{X: 600, Y: 1000},
Velocity: vect.Vect{X: -10, Y: 0},
Angle: math.Pi * 5.9 / 3,
Mass: 10e3,
Elasticity: 0.2,
Friction: 5.0,
BoxOptions: pixelmunk.ObjectBoxOptions{
Width: 50,
Height: 100,
},
},
}))
// Second falling object
world.Add(pixelmunk.NewCircle(pixelmunk.ObjectOptions{
Color: colornames.Purple,
CustomDrawFunc: []pixelmunk.CustomDrawFunc{drawVelocity},
BodyOptions: pixelmunk.ObjectBodyOptions{
Position: vect.Vect{X: 600, Y: 2000},
Angle: math.Pi * 4 / 3,
Mass: 10e3,
Elasticity: 0.9,
Friction: 10.0,
CircleOptions: pixelmunk.ObjectCircleOptions{
Radius: 25,
},
},
}))
return
}
func drawVelocity(object *pixelmunk.Object, imd *imdraw.IMDraw) {
body := object.GetBody()
pos := body.Position()
velocity := body.Velocity()
imd.Color = colornames.Red
imd.Push(
pixel.V(float64(pos.X), float64(pos.Y)),
pixel.V(float64(pos.X+velocity.X), float64(pos.Y+velocity.Y)),
)
imd.Line(1)
}