2021-04-25 08:03:06 +00:00
|
|
|
local vector_new = vector.new
|
2021-04-25 05:45:35 +00:00
|
|
|
|
2021-04-25 21:48:41 +00:00
|
|
|
|
|
|
|
--converts yaw to degrees
|
|
|
|
local degrees = function(yaw)
|
|
|
|
return(yaw*180.0/math.pi)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-04-25 08:03:06 +00:00
|
|
|
mobs.do_head_logic = function(self,dtime)
|
2021-04-25 05:45:35 +00:00
|
|
|
|
2021-04-25 21:48:41 +00:00
|
|
|
local player = minetest.get_player_by_name("singleplayer")
|
|
|
|
|
|
|
|
local look_at = player:get_pos()
|
|
|
|
look_at.y = look_at.y + player:get_properties().eye_height
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local pos = self.object:get_pos()
|
|
|
|
|
|
|
|
local body_yaw = self.object:get_yaw()
|
|
|
|
|
|
|
|
local body_dir = minetest.yaw_to_dir(body_yaw)
|
|
|
|
|
|
|
|
|
|
|
|
--needs to be INTERNAL(API)
|
|
|
|
self.head_height_offset = 1.0525
|
|
|
|
|
|
|
|
pos.y = pos.y + self.head_height_offset
|
|
|
|
|
|
|
|
--needs to be INTERNAL (API)
|
|
|
|
self.head_direction_offset = 0.5
|
|
|
|
|
|
|
|
local head_offset = vector.multiply(body_dir, self.head_direction_offset)
|
2021-04-25 05:45:35 +00:00
|
|
|
|
2021-04-25 21:48:41 +00:00
|
|
|
pos = vector.add(pos, head_offset)
|
2021-04-25 08:03:06 +00:00
|
|
|
|
|
|
|
|
2021-04-25 21:48:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
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 = "default_dirt.png",
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
local bone_pos = vector_new(0,0,0)
|
|
|
|
|
|
|
|
|
|
|
|
--needs to be INTERNAL (API)
|
|
|
|
--(horizontal)
|
|
|
|
self.head_bone_pos_y = 3.6
|
|
|
|
bone_pos.y = self.head_bone_pos_y
|
|
|
|
|
|
|
|
|
|
|
|
--needs to be INTERNAL (API)
|
|
|
|
--(vertical)
|
|
|
|
self.head_bone_pos_z = -0.6
|
|
|
|
bone_pos.z = self.head_bone_pos_z
|
2021-04-25 08:03:06 +00:00
|
|
|
|
|
|
|
--print(yaw)
|
|
|
|
|
|
|
|
--local _, bone_rot = self.object:get_bone_position("head")
|
|
|
|
|
|
|
|
--bone_rot.x = bone_rot.x + (dtime * 10)
|
|
|
|
--bone_rot.z = bone_rot.z + (dtime * 10)
|
|
|
|
|
2021-04-25 21:48:41 +00:00
|
|
|
local head_yaw
|
|
|
|
|
|
|
|
head_yaw = minetest.dir_to_yaw(vector.direction(pos,look_at)) - body_yaw
|
|
|
|
|
|
|
|
|
|
|
|
--over rotation protection
|
|
|
|
--stops radians from going out of spec
|
|
|
|
if head_yaw > math.pi then
|
|
|
|
head_yaw = head_yaw - (math.pi * 2)
|
|
|
|
elseif head_yaw < -math.pi then
|
|
|
|
head_yaw = head_yaw + (math.pi * 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-04-25 21:54:14 +00:00
|
|
|
local check_failed = false
|
|
|
|
--upper check + 90 degrees or upper math.radians (3.14/2)
|
2021-04-25 21:48:41 +00:00
|
|
|
if head_yaw > math.pi - (math.pi/2) then
|
|
|
|
head_yaw = 0
|
2021-04-25 21:54:14 +00:00
|
|
|
check_failed = true
|
|
|
|
--lower check - 90 degrees or lower negative math.radians (-3.14/2)
|
2021-04-25 21:48:41 +00:00
|
|
|
elseif head_yaw < -math.pi + (math.pi/2) then
|
|
|
|
head_yaw = 0
|
2021-04-25 21:54:14 +00:00
|
|
|
check_failed = true
|
2021-04-25 21:48:41 +00:00
|
|
|
end
|
|
|
|
|
2021-04-25 21:54:14 +00:00
|
|
|
local head_pitch = 0
|
|
|
|
|
|
|
|
--DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
|
|
|
|
--head_yaw = 0
|
|
|
|
--DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
|
2021-04-25 21:48:41 +00:00
|
|
|
|
2021-04-25 21:54:14 +00:00
|
|
|
if not check_failed then
|
|
|
|
head_pitch = minetest.dir_to_yaw(vector.new(vector.distance(vector.new(pos.x,0,pos.z),vector.new(look_at.x,0,look_at.z)),0,pos.y-look_at.y))+(math.pi/2)
|
|
|
|
end
|
2021-04-25 21:48:41 +00:00
|
|
|
|
2021-04-25 21:54:14 +00:00
|
|
|
self.object:set_bone_position("head", bone_pos, vector_new(degrees(head_pitch),0,degrees(head_yaw)))
|
2021-04-25 08:03:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--set_bone_position([bone, position, rotation])
|
2021-04-25 05:45:35 +00:00
|
|
|
end
|