- --if within range then do calculations
- if goal_yaw <= math.pi/2 and goal_yaw >= -math.pi/2 then
- ---begin pitch calculation
- --feed a 2D coordinate flipped into dir to yaw to calculate pitch
- 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))
- head_rotation.z = degrees(-goal_yaw)
- self.object:set_bone_position(self.head_bone, head_position, head_rotation)
- return(true)
- --nothing to look at
- else--dofile(path.."movement.lua")
+ --pos is where the head actually is
+ pos = vector.add(pos,dir)
+ pos.y = pos.y + self.head_height_offset
+
+ --use this to literally look around
+ self.head_pos = pos
+
+ if self.debug_head_pos == true then
+ minetest.add_particle({
+ pos = pos,
+ velocity = {x=0, y=0, z=0},
+ acceleration = {x=0, y=0, z=0},
+ expirationtime = 0.2,
+ size = 1,
+ texture = "dirt.png",
+ })
+ end
+
+ --if the function was given a pos
+ if pos2 then
+ --compare the head yaw to the body
+ --we must do a bunch of calculations to correct
+ --strange function returns
+ --for some reason get_yaw is offset 90 degrees
+ local head_yaw = minetest.dir_to_yaw(vector.direction(pos,pos2))
+ head_yaw = minetest.dir_to_yaw(minetest.yaw_to_dir(head_yaw))
+ head_yaw = degrees(head_yaw)-degrees(body_yaw)
+
+ if head_yaw < -180 then
+ head_yaw = head_yaw + 360
+ elseif head_yaw > 180 then
+ head_yaw = head_yaw - 360
+ end
+
+ --if within range then do calculations
+ if head_yaw >= -90 and head_yaw <= 90 then
+ ---begin pitch calculation
+ --feed a 2D coordinate flipped into dir to yaw to calculate pitch
+ 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))
+ if self.flip_pitch then
+ head_rotation.x = head_rotation.x * -1
+ end
+ head_rotation.y = -head_yaw
+ self.object:set_bone_position(self.head_bone, head_position, head_rotation)
+ return(true)
+ --nothing to look at
+ else
+ self.return_head_to_origin(self,dtime)
+ return(false)
+ end
+
+ --if nothing to look at
+ else