X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=mods%2Fmob%2Fapi%2Fhead_code.lua;h=4211fb9b4d9f2ed0f66f24f1ca8475a0f7478e89;hb=242a3d1665399c43fc68e3ae36cb5bf01334d98a;hp=a06936dc7f01f2f9c826ccce827976dbae5828bc;hpb=c09067c77ece357b018dd03963e0a1e19f9c4686;p=Crafter.git diff --git a/mods/mob/api/head_code.lua b/mods/mob/api/head_code.lua index a06936d..4211fb9 100644 --- a/mods/mob/api/head_code.lua +++ b/mods/mob/api/head_code.lua @@ -1,45 +1,56 @@ +local +minetest,math,vector += +minetest,math,vector + +--converts the degrees to radians +local degrees_to_radians = function(degrees) + --print(d) + return(degrees/180.0*math.pi) +end + +--converts yaw to degrees +local degrees = function(yaw) + return(yaw*180.0/math.pi) +end + +--rounds it up to an integer +local degree_round = function(degree) + return(degree + 0.5 - (degree + 0.5) % 1) +end +--turns radians into degrees - not redundant +--doesn't add math.pi +local radians_to_degrees = function(radians) + return(radians*180.0/math.pi) +end + +--make sure this is redefined as shown below aka +--don't run mob_rotation_degree_to_radians(rotation) +--run local radians = mob_rotation_degree_to_radians(rotation) +--or the mobs head rotation will become overwritten +local head_rotation_to_radians = function(rotation) + return{ + x = 0, --roll should never be changed + y = degrees_to_radians(180 - rotation.y)*-1, + z = degrees_to_radians(90 - rotation.z) + } +end + -- -mobs.create_head_functions = function(def,mob_register) - --converts the degrees to radians - local degrees_to_radians = function(degrees) - --print(d) - return(degrees/180.0*math.pi) - end - - --converts yaw to degrees - local degrees = function(yaw) - return(yaw*180.0/math.pi) - end - - --rounds it up to an integer - local degree_round = function(degree) - return(degree + 0.5 - (degree + 0.5) % 1) - end - --turns radians into degrees - not redundant - --doesn't add math.pi - local radians_to_degrees = function(radians) - return(radians*180.0/math.pi) - end - - - --make sure this is redefined as shown below aka - --don't run mob_rotation_degree_to_radians(rotation) - --run local radians = mob_rotation_degree_to_radians(rotation) - --or the mobs head rotation will become overwritten - local head_rotation_to_radians = function(rotation) - return{ - x = 0, --roll should never be changed - y = degrees_to_radians(180 - rotation.y)*-1, - z = degrees_to_radians(90 - rotation.z) - } - end - + +local pos +local body_yaw +local head_yaw +local dir +local head_position +local head_rotation +mobs.create_head_functions = function(def,mob_register) --a movement test to move the head mob_register.move_head = function(self,pos2,dtime) if self.head_bone then if self.head_coord == "horizontal" then --print(self.head_bone) - local head_position,head_rotation = self.object:get_bone_position(self.head_bone) + head_position,head_rotation = self.object:get_bone_position(self.head_bone) --[[ debug if rotation then --print("--------------------------------") @@ -58,13 +69,12 @@ mobs.create_head_functions = function(def,mob_register) self.object:set_bone_position(self.head_bone, head_position, rotation) end ]]-- - - --print(self.head_rotation.y) + --if passed a direction to look - local pos = self.object:get_pos() - local body_yaw = self.object:get_yaw()-math.pi/2+self.rotational_correction + pos = self.object:get_pos() + body_yaw = self.object:get_yaw()-math.pi/2+self.rotational_correction - local dir = vector.multiply(minetest.yaw_to_dir(body_yaw),self.head_directional_offset) + dir = vector.multiply(minetest.yaw_to_dir(body_yaw),self.head_directional_offset) body_yaw = minetest.dir_to_yaw(dir) @@ -74,18 +84,18 @@ mobs.create_head_functions = function(def,mob_register) pos.y = pos.y + self.head_height_offset --use this to literally look around - self.head_pos = pos + --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 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 @@ -93,7 +103,7 @@ mobs.create_head_functions = function(def,mob_register) --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(vector.direction(pos,pos2)) head_yaw = minetest.dir_to_yaw(minetest.yaw_to_dir(head_yaw)) head_yaw = degrees(head_yaw)-degrees(body_yaw) @@ -108,6 +118,9 @@ mobs.create_head_functions = function(def,mob_register) ---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.z = head_yaw self.object:set_bone_position(self.head_bone, head_position, head_rotation) return(true) @@ -125,7 +138,7 @@ mobs.create_head_functions = function(def,mob_register) elseif self.head_coord == "vertical" then --print(self.head_bone) - local head_position,head_rotation = self.object:get_bone_position(self.head_bone) + head_position,head_rotation = self.object:get_bone_position(self.head_bone) --[[ debug if rotation then --print("--------------------------------") @@ -147,10 +160,10 @@ mobs.create_head_functions = function(def,mob_register) --print(self.head_rotation.y) --if passed a direction to look - local pos = self.object:get_pos() - local body_yaw = self.object:get_yaw()-math.pi/2 + pos = self.object:get_pos() + body_yaw = self.object:get_yaw()-math.pi/2+self.rotational_correction - local dir = vector.multiply(minetest.yaw_to_dir(body_yaw),self.head_directional_offset) + dir = vector.multiply(minetest.yaw_to_dir(body_yaw),self.head_directional_offset) body_yaw = minetest.dir_to_yaw(dir) @@ -160,18 +173,18 @@ mobs.create_head_functions = function(def,mob_register) pos.y = pos.y + self.head_height_offset --use this to literally look around - self.head_pos = pos + --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 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 @@ -179,7 +192,7 @@ mobs.create_head_functions = function(def,mob_register) --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(vector.direction(pos,pos2)) head_yaw = minetest.dir_to_yaw(minetest.yaw_to_dir(head_yaw)) head_yaw = degrees(head_yaw)-degrees(body_yaw) @@ -194,6 +207,9 @@ mobs.create_head_functions = function(def,mob_register) ---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) @@ -216,7 +232,7 @@ mobs.create_head_functions = function(def,mob_register) --this sets the mob to move it's head back to pointing forwards mob_register.return_head_to_origin = function(self,dtime) - local head_position,head_rotation = self.object:get_bone_position(self.head_bone) + head_position,head_rotation = self.object:get_bone_position(self.head_bone) if self.head_coord == "horizontal" then --make the head yaw move back