-
Notifications
You must be signed in to change notification settings - Fork 9
/
demo_lizard.lua
160 lines (144 loc) · 5.23 KB
/
demo_lizard.lua
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
154
155
156
157
158
159
160
local demo_lizard = {}
local mgl = require "MGL"
local skeleton = require "skeleton"
local skin = require "skin"
local part = require "part"
local LizardLeg = setmetatable({}, {__index = part.Part})
demo_lizard.LizardLeg = LizardLeg
function LizardLeg:build(root_joint, front_joint, target, elbow_pos)
self.root_joint = root_joint
self.front_joint = front_joint
self.target = target
self.elbow_pos = elbow_pos
self.speed = 800
self.step = 40
self.fixation = skeleton.Joint:new()
self.elbow = skeleton.Joint:new()
self.paw = skeleton.Joint:new()
self.current_target = skeleton.Joint:new()
self:init_skeleton()
self.patches = {}
local names = {'fixation', 'elbow', 'paw', 'current_target'}
local size = {5,2,2,3}
for i = 1, #names-1 do
local patch = skin.CircleSeries:new(self[names[i]], self[names[i+1]])
patch:set_from_to('fill', 4, size[i], size[i+1])
table.insert(self.patches, patch)
end
end
function LizardLeg:destroy()
end
function LizardLeg:get_rel_to_global()
local root_to_front = self.front_joint.pos - self.root_joint.pos
local root_to_front_dir = mgl.normalize(root_to_front)
local root_to_front_angle = math.atan2(root_to_front_dir.y, root_to_front_dir.x)
local rel_to_global = mgl.translate(self.root_joint.pos) * mgl.rotate(root_to_front_angle)
return rel_to_global
end
function LizardLeg:init_skeleton()
local rel_to_global = self:get_rel_to_global()
local paw_pos_rel = self.target
local elbow_len = mgl.length(self.elbow_pos)
local paw_len = mgl.length(paw_pos_rel - self.elbow_pos)
local paw_pos = mgl.vec2(rel_to_global * mgl.vec3(paw_pos_rel, 1))
local elbow_pos = mgl.vec2(rel_to_global * mgl.vec3(self.elbow_pos, 1))
self.fixation:add_mutual_neighbor(self.elbow, skeleton.link(elbow_len, elbow_len, 500))
self.elbow:add_mutual_neighbor(self.paw, skeleton.link(paw_len, paw_len, 500))
self.paw:add_mutual_neighbor(self.current_target, {
length_min = 0.1,
length_max = 1,
length_absolute_min = 0.1,
length_absolute_max = 1e5,
speed = self.speed,
exponential = false,
drag = 0
})
local elbow_to_fixation_angle = math.atan2(-self.elbow_pos.y, -self.elbow_pos.x)
local elbow_to_paw_angle = math.atan2((self.target - self.elbow_pos).y, (self.target - self.elbow_pos).x)
local fixation_to_paw_angle = elbow_to_paw_angle - elbow_to_fixation_angle
self.elbow:add_constraint(skeleton.constraint(
self.fixation,
self.paw,
fixation_to_paw_angle - math.pi/2,
fixation_to_paw_angle + math.pi/2,
12*math.pi
))
self.fixation.pos = self.root_joint.pos
self.elbow.pos = elbow_pos
self.paw.pos = paw_pos
self.current_target.pos = paw_pos
end
function LizardLeg:update(args)
local rel_to_global = self:get_rel_to_global()
local new_target = mgl.vec2(rel_to_global * mgl.vec3(self.target, 1))
local current_target = self.current_target.pos
if mgl.length(new_target - self.paw.pos) > self.step then
current_target = new_target
end
self.current_target.pos = current_target
self.current_target:influence_recursive(nil, args.time/2)
self.fixation.pos = self.root_joint.pos
self.fixation:influence_recursive(nil, args.time/2)
self.current_target.pos = current_target
end
function LizardLeg:draw(args)
for _, patch in ipairs(self.patches) do
patch:draw()
end
end
local LizardBody = setmetatable({}, {__index = part.Part})
demo_lizard.LizardBody = LizardBody
function LizardBody:build(target_joint, length, head_pos, delta_pos)
self.target_joint = target_joint
self.head = skeleton.Joint:new(head_pos)
self.target_joint:add_neighbor(self.head, {
length_min = 30,
length_max = 60,
length_absolute_min = 1,
length_absolute_max = 1e5,
speed = 50,
exponential = false,
drag = 0
})
self.joints = {self.head}
local joint_pos = head_pos
local delta_length = mgl.length(delta_pos)
for i = 2, length do
joint_pos = joint_pos + delta_pos
local joint = skeleton.Joint:new(joint_pos)
self.joints[#self.joints]:add_mutual_neighbor(joint, skeleton.link(delta_length, delta_length, 500))
table.insert(self.joints, joint)
end
for i = 3, length-1 do
local c = skeleton.constraint(
self.joints[i-1],
self.joints[i+1],
math.pi*7/8,
math.pi*9/8,
math.pi/2
)
self.joints[i]:add_constraint(c)
end
self.patches = {}
local size = {4,9,6,7, 7,5,5,4, 3,2,2,2, 1,1,0}
for i = 1, #self.joints-1 do
local patch = skin.CircleSeries:new(self.joints[i], self.joints[i+1])
patch:set_from_to('fill', 4, size[i], size[i+1])
table.insert(self.patches, patch)
end
end
function LizardBody:destroy()
if self.joints and #self.joints > 0 then
self.target_joint:remove_neighbor(self.joints[1])
end
end
function LizardBody:update(args)
self.target_joint.pos = args.mouse_pos
self.target_joint:influence_recursive(nil, args.time)
end
function LizardBody:draw(args)
for _, patch in ipairs(self.patches) do
patch:draw()
end
end
return demo_lizard