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 --print(self.head_bone)
41 local head_position,head_rotation = self.object:get_bone_position(self.head_bone)
44 --print("--------------------------------")
45 --rotation.x = rotation.x + 1
46 rotation.z = rotation.z + 1
49 if rotation.x > 90 then
52 if rotation.z > 90 then
57 self.object:set_bone_position(self.head_bone, head_position, rotation)
61 --print(self.head_rotation.y)
62 --if passed a direction to look
63 local pos = self.object:get_pos()
64 local body_yaw = self.object:get_yaw()
69 local dir = vector.multiply(minetest.yaw_to_dir(body_yaw+self.head_rotation_offset),self.head_directional_offset)
72 local body_yaw = minetest.dir_to_yaw(dir)
74 --pos is where the head actually is
75 pos = vector.add(pos,dir)
76 pos.y = pos.y + self.head_height_offset
78 --use this to literally look around
81 if self.debug_head_pos == true then
82 minetest.add_particle({
84 velocity = {x=0, y=0, z=0},
85 acceleration = {x=0, y=0, z=0},
92 --if the function was given a pos
94 --compare the head yaw to the body
95 local head_yaw = minetest.dir_to_yaw(vector.direction(pos,pos2))
97 local goal_yaw = body_yaw-head_yaw
99 --if within range then do calculations
100 if goal_yaw <= math.pi/2 and goal_yaw >= -math.pi/2 then
101 ---begin pitch calculation
102 --feed a 2D coordinate flipped into dir to yaw to calculate pitch
103 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))
104 head_rotation.z = degrees(-goal_yaw)
105 self.object:set_bone_position(self.head_bone, head_position, head_rotation)
108 else--dofile(path.."movement.lua")
109 self.return_head_to_origin(self,dtime)
113 --if nothing to look at
115 self.return_head_to_origin(self,dtime)
122 --this sets the mob to move it's head back to pointing forwards
124 mob_register.return_head_to_origin = function(self,dtime)
125 local head_position,head_rotation = self.object:get_bone_position(self.head_bone)
126 --make the head yaw move back
127 if head_rotation.x > 0 then
128 head_rotation.x = head_rotation.x - 1
129 elseif head_rotation.x < 0 then
130 head_rotation.x = head_rotation.x + 1
133 --move up down (pitch) back to center
134 if head_rotation.z > 0 then
135 head_rotation.z = head_rotation.z - 1
136 elseif head_rotation.z < 0 then
137 head_rotation.z = head_rotation.z + 1
140 self.object:set_bone_position(self.head_bone, head_position, head_rotation)