2 mobs.create_head_functions = function(def,mob_register)
3 --converts the degrees to radians
4 local degrees_to_radians = function(degrees)
6 return(degrees/180.0*math.pi)
9 --converts yaw to degrees
10 local degrees = function(yaw)
11 return(yaw*180.0/math.pi)
14 --rounds it up to an integer
15 local degree_round = function(degree)
16 return(degree + 0.5 - (degree + 0.5) % 1)
18 --turns radians into degrees - not redundant
20 local radians_to_degrees = function(radians)
21 return(radians*180.0/math.pi)
25 --make sure this is redefined as shown below aka
26 --don't run mob_rotation_degree_to_radians(rotation)
27 --run local radians = mob_rotation_degree_to_radians(rotation)
28 --or the mobs head rotation will become overwritten
29 local head_rotation_to_radians = function(rotation)
31 x = 0, --roll should never be changed
32 y = degrees_to_radians(180 - rotation.y)*-1,
33 z = degrees_to_radians(90 - rotation.z)
37 --a movement test to move the head
38 mob_register.move_head = function(self,pos2,dtime)
39 if self.head_bone then
40 if self.head_coord == "horizontal" then
41 --print(self.head_bone)
42 local head_position,head_rotation = self.object:get_bone_position(self.head_bone)
45 --print("--------------------------------")
46 --rotation.x = rotation.x + 1
47 rotation.z = rotation.z + 1
50 if rotation.x > 90 then
53 if rotation.z > 90 then
58 self.object:set_bone_position(self.head_bone, head_position, rotation)
62 --print(self.head_rotation.y)
63 --if passed a direction to look
64 local pos = self.object:get_pos()
65 local body_yaw = self.object:get_yaw()-math.pi/2
67 local dir = vector.multiply(minetest.yaw_to_dir(body_yaw),self.head_directional_offset)
70 body_yaw = minetest.dir_to_yaw(dir)
72 --pos is where the head actually is
73 pos = vector.add(pos,dir)
74 pos.y = pos.y + self.head_height_offset
76 --use this to literally look around
79 if self.debug_head_pos == true then
80 minetest.add_particle({
82 velocity = {x=0, y=0, z=0},
83 acceleration = {x=0, y=0, z=0},
90 --if the function was given a pos
92 --compare the head yaw to the body
93 --we must do a bunch of calculations to correct
94 --strange function returns
95 --for some reason get_yaw is offset 90 degrees
96 local head_yaw = minetest.dir_to_yaw(vector.direction(pos,pos2))
97 head_yaw = minetest.dir_to_yaw(minetest.yaw_to_dir(head_yaw))
98 head_yaw = degrees(head_yaw)-degrees(body_yaw)
100 if head_yaw < -180 then
101 head_yaw = head_yaw + 360
102 elseif head_yaw > 180 then
103 head_yaw = head_yaw - 360
106 --if within range then do calculations
107 if head_yaw >= -90 and head_yaw <= 90 then
108 ---begin pitch calculation
109 --feed a 2D coordinate flipped into dir to yaw to calculate pitch
110 head_rotation.x = degrees(minetest.dir_to_yaw(vector.new(vector.distance(vector.new(pos.x,0,pos.z),vector.new(pos2.x,0,pos2.z)),0,pos.y-pos2.y))+(math.pi/2))
111 head_rotation.z = head_yaw
112 self.object:set_bone_position(self.head_bone, head_position, head_rotation)
116 self.return_head_to_origin(self,dtime)
120 --if nothing to look at
122 self.return_head_to_origin(self,dtime)
126 elseif self.head_coord == "vertical" then
127 --print(self.head_bone)
128 local head_position,head_rotation = self.object:get_bone_position(self.head_bone)
131 --print("--------------------------------")
132 --rotation.x = rotation.x + 1
133 rotation.z = rotation.z + 1
136 if rotation.x > 90 then
139 if rotation.z > 90 then
144 self.object:set_bone_position(self.head_bone, head_position, rotation)
148 --print(self.head_rotation.y)
149 --if passed a direction to look
150 local pos = self.object:get_pos()
151 local body_yaw = self.object:get_yaw()-math.pi/2
153 local dir = vector.multiply(minetest.yaw_to_dir(body_yaw),self.head_directional_offset)
156 body_yaw = minetest.dir_to_yaw(dir)
158 --pos is where the head actually is
159 pos = vector.add(pos,dir)
160 pos.y = pos.y + self.head_height_offset
162 --use this to literally look around
165 if self.debug_head_pos == true then
166 minetest.add_particle({
168 velocity = {x=0, y=0, z=0},
169 acceleration = {x=0, y=0, z=0},
170 expirationtime = 0.2,
172 texture = "dirt.png",
176 --if the function was given a pos
178 --compare the head yaw to the body
179 --we must do a bunch of calculations to correct
180 --strange function returns
181 --for some reason get_yaw is offset 90 degrees
182 local head_yaw = minetest.dir_to_yaw(vector.direction(pos,pos2))
183 head_yaw = minetest.dir_to_yaw(minetest.yaw_to_dir(head_yaw))
184 head_yaw = degrees(head_yaw)-degrees(body_yaw)
186 if head_yaw < -180 then
187 head_yaw = head_yaw + 360
188 elseif head_yaw > 180 then
189 head_yaw = head_yaw - 360
192 --if within range then do calculations
193 if head_yaw >= -90 and head_yaw <= 90 then
194 ---begin pitch calculation
195 --feed a 2D coordinate flipped into dir to yaw to calculate pitch
196 head_rotation.x = degrees(minetest.dir_to_yaw(vector.new(vector.distance(vector.new(pos.x,0,pos.z),vector.new(pos2.x,0,pos2.z)),0,pos.y-pos2.y))+(math.pi/2))
197 head_rotation.y = -head_yaw
198 self.object:set_bone_position(self.head_bone, head_position, head_rotation)
202 self.return_head_to_origin(self,dtime)
206 --if nothing to look at
208 self.return_head_to_origin(self,dtime)
216 --this sets the mob to move it's head back to pointing forwards
218 mob_register.return_head_to_origin = function(self,dtime)
219 local head_position,head_rotation = self.object:get_bone_position(self.head_bone)
221 if self.head_coord == "horizontal" then
222 --make the head yaw move back
223 if head_rotation.x > 0 then
224 head_rotation.x = head_rotation.x - (dtime*100)
225 elseif head_rotation.x < 0 then
226 head_rotation.x = head_rotation.x + (dtime*100)
229 if math.abs(head_rotation.x) < (dtime*100) then
234 --move up down (pitch) back to center
235 if head_rotation.z > 0 then
236 head_rotation.z = head_rotation.z - (dtime*100)
237 elseif head_rotation.z < 0 then
238 head_rotation.z = head_rotation.z + (dtime*100)
241 if math.abs(head_rotation.z) < (dtime*100) then
244 elseif self.head_coord == "vertical" then
245 --make the head yaw move back
246 if head_rotation.x > 0 then
247 head_rotation.x = head_rotation.x - (dtime*100)
248 elseif head_rotation.x < 0 then
249 head_rotation.x = head_rotation.x + (dtime*100)
252 if math.abs(head_rotation.x) < (dtime*100) then
257 --move up down (pitch) back to center
258 if head_rotation.y > 0 then
259 head_rotation.y = head_rotation.y - (dtime*100)
260 elseif head_rotation.y < 0 then
261 head_rotation.y = head_rotation.y + (dtime*100)
264 if math.abs(head_rotation.y) < (dtime*100) then
268 self.object:set_bone_position(self.head_bone, head_position, head_rotation)