forked from gen2brain/raylib-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (60 loc) · 2.18 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
package main
import (
"github.com/gen2brain/raylib-go/physics"
"github.com/gen2brain/raylib-go/raylib"
)
const (
velocity = 0.5
)
func main() {
screenWidth := float32(800)
screenHeight := float32(450)
rl.SetConfigFlags(rl.FlagMsaa4xHint)
rl.InitWindow(int32(screenWidth), int32(screenHeight), "Physac [raylib] - body shatter")
// Physac logo drawing position
logoX := int32(screenWidth) - rl.MeasureText("Physac", 30) - 10
logoY := int32(15)
// Initialize physics and default physics bodies
physics.Init()
physics.SetGravity(0, 0)
// Create random polygon physics body to shatter
physics.NewBodyPolygon(rl.NewVector2(screenWidth/2, screenHeight/2), float32(rl.GetRandomValue(80, 200)), int(rl.GetRandomValue(3, 8)), 10)
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
// Update created physics objects
physics.Update()
if rl.IsKeyPressed(rl.KeyR) { // Reset physics input
physics.Reset()
// Create random polygon physics body to shatter
physics.NewBodyPolygon(rl.NewVector2(screenWidth/2, screenHeight/2), float32(rl.GetRandomValue(80, 200)), int(rl.GetRandomValue(3, 8)), 10)
}
if rl.IsMouseButtonPressed(rl.MouseLeftButton) {
for _, b := range physics.GetBodies() {
b.Shatter(rl.GetMousePosition(), 10/b.InverseMass)
}
}
rl.BeginDrawing()
rl.ClearBackground(rl.Black)
// Draw created physics bodies
for i, body := range physics.GetBodies() {
vertexCount := physics.GetShapeVerticesCount(i)
for j := 0; j < vertexCount; j++ {
// Get physics bodies shape vertices to draw lines
// NOTE: GetShapeVertex() already calculates rotation transformations
vertexA := body.GetShapeVertex(j)
jj := 0
if j+1 < vertexCount { // Get next vertex or first to close the shape
jj = j + 1
}
vertexB := body.GetShapeVertex(jj)
rl.DrawLineV(vertexA, vertexB, rl.Green) // Draw a line between two vertex positions
}
}
rl.DrawText("Left mouse button in polygon area to shatter body\nPress 'R' to reset example", 10, 10, 10, rl.White)
rl.DrawText("Physac", logoX, logoY, 30, rl.White)
rl.DrawText("Powered by", logoX+50, logoY-7, 10, rl.White)
rl.EndDrawing()
}
physics.Close() // Unitialize physics
rl.CloseWindow()
}