Oil_boi
Member
Posts: 139
Joined: Mon Jan 28, 2019 13:12
GitHub: oilboi
IRC: oilboi
In-game: oilboi
Contact:

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)
end``````
Here is a video Explaining it better:
https://youtu.be/_GxEBYfmKj8
Attachments
screenshot_20200329_213951.png
I like to make mods :D

Eran
Member
Posts: 123
Joined: Fri May 03, 2019 16:46

### Re: [help] Getting the "roll" from pitch and yaw

self.child:set_attach(self.object, "", vector.new(2.4,1.2,0), vector.new(180, new_yaw, 180+pitch))
x is pitch, y is yaw, z is roll. Youre'e setting the roll to 180 + pitch, try swapping it with pitch.

### Who is online

Users browsing this forum: No registered users and 2 guests