-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
153 lines (128 loc) · 3.64 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"github.com/veandco/go-sdl2/sdl"
"log"
"math"
)
func main() {
err := sdl.Init(sdl.INIT_EVERYTHING)
if err != nil {
log.Fatalln("Unable to initialize SDL:", err)
}
defer sdl.Quit()
window, renderer, err := sdl.CreateWindowAndRenderer(800, 600, 0)
if err != nil {
log.Fatalln("Unable to create SDL window:", err)
}
defer func() {
_ = window.Destroy()
}()
defer func() {
_ = renderer.Destroy()
}()
window.SetTitle("Learn IK")
window.Show()
// Target.
target := sdl.FPoint{}
// Arm and bones.
arm := Arm{}
arm.anchor = sdl.FPoint{X: 400, Y: 600}
arm.bones = make([]*Bone, 0)
for i := 0; i < 5; i++ {
arm.bones = append(arm.bones, &Bone{
head: sdl.FPoint{X: 400, Y: 600},
tail: sdl.FPoint{X: 400, Y: 600},
width: 10,
length: 100,
})
}
mainLoop:
for {
event := sdl.WaitEvent()
switch e := event.(type) {
case *sdl.MouseMotionEvent:
target.X = float32(e.X)
target.Y = float32(e.Y)
case *sdl.QuitEvent:
break mainLoop
}
_ = renderer.SetDrawColor(0, 0, 0, 0)
_ = renderer.Clear()
_ = renderer.SetDrawColor(255, 255, 255, 0)
_ = drawFrame(renderer, arm, target)
renderer.Present()
}
}
func drawFrame(renderer *sdl.Renderer, arm Arm, target sdl.FPoint) error {
_ = updateArm(arm, target)
drawArm(renderer, arm)
return nil
}
func drawArm(renderer *sdl.Renderer, arm Arm) {
for _, bone := range arm.bones {
_ = drawBone(renderer, *bone)
}
}
func updateArm(arm Arm, target sdl.FPoint) error {
// Update forward
for k := range arm.bones {
// First bone reaches the target, the rest follows.
if k == 0 {
boneReach(&arm.bones[k].head, &arm.bones[k].tail, arm.bones[k].length, target)
} else {
boneReach(&arm.bones[k].head, &arm.bones[k].tail, arm.bones[k].length, arm.bones[k-1].tail)
}
}
// Update backward
for k := len(arm.bones) - 1; k >= 0; k-- {
if k == len(arm.bones) - 1 {
boneReach(&arm.bones[k].tail, &arm.bones[k].head, arm.bones[k].length, arm.anchor)
} else {
boneReach(&arm.bones[k].tail, &arm.bones[k].head, arm.bones[k].length, arm.bones[k+1].head)
}
}
return nil
}
func drawBone(renderer *sdl.Renderer, bone Bone) error {
_ = renderer.SetDrawColor(255, 0, 0, 255)
_ = renderer.DrawLine(int32(bone.head.X), int32(bone.head.Y), int32(bone.tail.X), int32(bone.tail.Y))
N := bone.width
x1 := float64(bone.head.X)
y1 := float64(bone.head.Y)
x2 := float64(bone.tail.X)
y2 := float64(bone.tail.Y)
dx := x1-x2
dy := y1-y2
dist := math.Sqrt(dx*dx + dy*dy)
dx /= dist
dy /= dist
x3 := x1 + (N/2)*dy
y3 := y1 - (N/2)*dx
x4 := x2 + (N/2)*dy
y4 := y2 - (N/2)*dx
x5 := x2 - (N/2)*dy
y5 := y2 + (N/2)*dx
x6 := x1 - (N/2)*dy
y6 := y1 + (N/2)*dx
_ = renderer.SetDrawColor(255, 255, 255, 255)
_ = renderer.DrawLineF(float32(x3), float32(y3), float32(x4), float32(y4))
_ = renderer.DrawLineF(float32(x5), float32(y5), float32(x6), float32(y6))
_ = renderer.DrawLineF(float32(x3), float32(y3), float32(x6), float32(y6))
_ = renderer.DrawLineF(float32(x4), float32(y4), float32(x5), float32(y5))
return nil
}
func boneReach(boneHead *sdl.FPoint, boneTail *sdl.FPoint, boneLength float64, target sdl.FPoint) {
// Given a bone and a target, set the bone's head to the target.
*boneHead = target
// Notice that this stretches the bone. We fix that by sliding the tail along the new bone.
// Calculate the stretched distance.
sdx := boneTail.X - target.X
sdy := boneTail.Y - target.Y
distance := math.Sqrt(float64(sdx)*float64(sdx) + float64(sdy)*float64(sdy))
scale := float32(boneLength / distance)
// Scale the new tail based on the distance from the target.
*boneTail = sdl.FPoint{
X: target.X + sdx * scale,
Y: target.Y + sdy * scale,
}
}