Page 1 of 1

Posted: Mon Mar 30, 2020 01:46
I have this mobs mod which spawns the body then the head spawns and becomes attached. The head becomes the self.child of the body. The body then controls the rotation of the head using the set_attach code. The problem is I cannot for the life of me figure out how to calculate the roll in this function from the two coordinates. This ends up working out to the head turning like the video shown below. If anyone knows how to solve this then please let me know.

I have this code:

Code: Select all

``````--converts yaw to degrees
local degrees = function(yaw)
yaw = yaw + math.pi
return(yaw*180.0/math.pi)
end

local degree_round = function(degree)
return(degree + 0.5 - (degree + 0.5) % 1)
end

end

--a movement test to move the head
if self.child then
local pos = self.object:get_pos()
local body_yaw = self.object:get_yaw() - (math.pi/2)
local dir = vector.multiply(minetest.yaw_to_dir(body_yaw),0.72)
local real_dir = minetest.yaw_to_dir(body_yaw)
local body_yaw = degree_round(degrees(minetest.dir_to_yaw(dir)))

pos.y = pos.y + 0.36

--pos is where the head actually is
--STARE O_O
for _,object in ipairs(minetest.get_objects_inside_radius(pos, 6)) do
if object:is_player() then
local pos2 = object:get_pos()
pos2.y = pos2.y + 1.625

local pitch = 0
if math.abs(new_yaw) <= 90 or math.abs(new_yaw) >= 270 then
--do other calculations on pitch and roll

local triangle = vector.new(vector.distance(pos,pos2),0,pos2.y-pos.y)

local tri_yaw = minetest.dir_to_yaw(triangle)+(math.pi/2)

else
new_yaw = 0
end
--                                                                      roll        newyaw      pitch
self.child:set_attach(self.object, "", vector.new(2.4,1.2,0), vector.new(180,     new_yaw,    180+pitch))
end
end
end
end

mob.on_step = function(self, dtime)
--self.move(self,dtime)
self.set_animation(self)