6 --converts the degrees to radians
7 local degrees_to_radians = function(degrees)
9 return(degrees/180.0*math.pi)
12 --converts yaw to degrees
13 local degrees = function(yaw)
14 return(yaw*180.0/math.pi)
17 --rounds it up to an integer
18 local degree_round = function(degree)
19 return(degree + 0.5 - (degree + 0.5) % 1)
21 --turns radians into degrees - not redundant
23 local radians_to_degrees = function(radians)
24 return(radians*180.0/math.pi)
27 --make sure this is redefined as shown below aka
28 --don't run mob_rotation_degree_to_radians(rotation)
29 --run local radians = mob_rotation_degree_to_radians(rotation)
30 --or the mobs head rotation will become overwritten
31 local head_rotation_to_radians = function(rotation)
33 x = 0, --roll should never be changed
34 y = degrees_to_radians(180 - rotation.y)*-1,
35 z = degrees_to_radians(90 - rotation.z)
47 mobs.create_head_functions = function(def,mob_register)
48 --a movement test to move the head
49 mob_register.move_head = function(self,pos2,dtime)
50 if self.head_bone then
51 if self.head_coord == "horizontal" then
52 --print(self.head_bone)
53 head_position,head_rotation = self.object:get_bone_position(self.head_bone)
56 --print("--------------------------------")
57 --rotation.x = rotation.x + 1
58 rotation.z = rotation.z + 1
61 if rotation.x > 90 then
64 if rotation.z > 90 then
69 self.object:set_bone_position(self.head_bone, head_position, rotation)
73 --if passed a direction to look
74 pos = self.object:get_pos()
75 body_yaw = self.object:get_yaw()-math.pi/2+self.rotational_correction
77 dir = vector.multiply(minetest.yaw_to_dir(body_yaw),self.head_directional_offset)
80 body_yaw = minetest.dir_to_yaw(dir)
82 --pos is where the head actually is
83 pos = vector.add(pos,dir)
84 pos.y = pos.y + self.head_height_offset
86 --use this to literally look around
89 --if self.debug_head_pos == true then
90 -- minetest.add_particle({
92 -- velocity = {x=0, y=0, z=0},
93 -- acceleration = {x=0, y=0, z=0},
94 -- expirationtime = 0.2,
96 -- texture = "dirt.png",
100 --if the function was given a pos
102 --compare the head yaw to the body
103 --we must do a bunch of calculations to correct
104 --strange function returns
105 --for some reason get_yaw is offset 90 degrees
106 head_yaw = minetest.dir_to_yaw(vector.direction(pos,pos2))
107 head_yaw = minetest.dir_to_yaw(minetest.yaw_to_dir(head_yaw))
108 head_yaw = degrees(head_yaw)-degrees(body_yaw)
110 if head_yaw < -180 then
111 head_yaw = head_yaw + 360
112 elseif head_yaw > 180 then
113 head_yaw = head_yaw - 360
116 --if within range then do calculations
117 if head_yaw >= -90 and head_yaw <= 90 then
118 ---begin pitch calculation
119 --feed a 2D coordinate flipped into dir to yaw to calculate pitch
120 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))
121 if self.flip_pitch then
122 head_rotation.x = head_rotation.x * -1
124 head_rotation.z = head_yaw
125 self.object:set_bone_position(self.head_bone, head_position, head_rotation)
129 self.return_head_to_origin(self,dtime)
133 --if nothing to look at
135 self.return_head_to_origin(self,dtime)
139 elseif self.head_coord == "vertical" then
140 --print(self.head_bone)
141 head_position,head_rotation = self.object:get_bone_position(self.head_bone)
144 --print("--------------------------------")
145 --rotation.x = rotation.x + 1
146 rotation.z = rotation.z + 1
149 if rotation.x > 90 then
152 if rotation.z > 90 then
157 self.object:set_bone_position(self.head_bone, head_position, rotation)
161 --print(self.head_rotation.y)
162 --if passed a direction to look
163 pos = self.object:get_pos()
164 body_yaw = self.object:get_yaw()-math.pi/2+self.rotational_correction
166 dir = vector.multiply(minetest.yaw_to_dir(body_yaw),self.head_directional_offset)
169 body_yaw = minetest.dir_to_yaw(dir)
171 --pos is where the head actually is
172 pos = vector.add(pos,dir)
173 pos.y = pos.y + self.head_height_offset
175 --use this to literally look around
176 --self.head_pos = pos
178 --if self.debug_head_pos == true then
179 -- minetest.add_particle({
181 -- velocity = {x=0, y=0, z=0},
182 -- acceleration = {x=0, y=0, z=0},
183 -- expirationtime = 0.2,
185 -- texture = "dirt.png",
189 --if the function was given a pos
191 --compare the head yaw to the body
192 --we must do a bunch of calculations to correct
193 --strange function returns
194 --for some reason get_yaw is offset 90 degrees
195 head_yaw = minetest.dir_to_yaw(vector.direction(pos,pos2))
196 head_yaw = minetest.dir_to_yaw(minetest.yaw_to_dir(head_yaw))
197 head_yaw = degrees(head_yaw)-degrees(body_yaw)
199 if head_yaw < -180 then
200 head_yaw = head_yaw + 360
201 elseif head_yaw > 180 then
202 head_yaw = head_yaw - 360
205 --if within range then do calculations
206 if head_yaw >= -90 and head_yaw <= 90 then
207 ---begin pitch calculation
208 --feed a 2D coordinate flipped into dir to yaw to calculate pitch
209 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))
210 if self.flip_pitch then
211 head_rotation.x = head_rotation.x * -1
213 head_rotation.y = -head_yaw
214 self.object:set_bone_position(self.head_bone, head_position, head_rotation)
218 self.return_head_to_origin(self,dtime)
222 --if nothing to look at
224 self.return_head_to_origin(self,dtime)
232 --this sets the mob to move it's head back to pointing forwards
234 mob_register.return_head_to_origin = function(self,dtime)
235 head_position,head_rotation = self.object:get_bone_position(self.head_bone)
237 if self.head_coord == "horizontal" then
238 --make the head yaw move back
239 if head_rotation.x > 0 then
240 head_rotation.x = head_rotation.x - (dtime*100)
241 elseif head_rotation.x < 0 then
242 head_rotation.x = head_rotation.x + (dtime*100)
245 if math.abs(head_rotation.x) < (dtime*100) then
250 --move up down (pitch) back to center
251 if head_rotation.z > 0 then
252 head_rotation.z = head_rotation.z - (dtime*100)
253 elseif head_rotation.z < 0 then
254 head_rotation.z = head_rotation.z + (dtime*100)
257 if math.abs(head_rotation.z) < (dtime*100) then
260 elseif self.head_coord == "vertical" then
261 --make the head yaw move back
262 if head_rotation.x > 0 then
263 head_rotation.x = head_rotation.x - (dtime*100)
264 elseif head_rotation.x < 0 then
265 head_rotation.x = head_rotation.x + (dtime*100)
268 if math.abs(head_rotation.x) < (dtime*100) then
273 --move up down (pitch) back to center
274 if head_rotation.y > 0 then
275 head_rotation.y = head_rotation.y - (dtime*100)
276 elseif head_rotation.y < 0 then
277 head_rotation.y = head_rotation.y + (dtime*100)
280 if math.abs(head_rotation.y) < (dtime*100) then
284 self.object:set_bone_position(self.head_bone, head_position, head_rotation)