[fixed] please delete

Post Reply
User avatar
Oil_boi
Member
Posts: 136
Joined: Mon Jan 28, 2019 13:12
GitHub: oilboi
IRC: oilboi
In-game: oilboi
Contact:

[fixed] please delete

by Oil_boi » Post

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

local radians_to_degrees = function(radians)
	return(radians*180.0/math.pi)
end

--a movement test to move the head
mob.move_head = function(self)
	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 = vector.add(pos,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 head_yaw  = degree_round(degrees(minetest.dir_to_yaw(vector.direction(pos,pos2))))			
				
				local new_yaw = (head_yaw-body_yaw)

				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)
					
					pitch = radians_to_degrees(tri_yaw)
				
				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))				
				--self.head_rotation = vector.new(180,new_yaw,180)
			end
		end
	end
end

mob.on_step = function(self, dtime)
	--self.move(self,dtime)
	self.set_animation(self)
	self.move_head(self)
end
Here is a video Explaining it better:
https://youtu.be/_GxEBYfmKj8
Image
Attachments
screenshot_20200329_213951.png
(471.76 KiB) Not downloaded yet
I like to make mods :D
I also made a patreon!

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

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

by Eran » Post

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests