]> git.lizzy.rs Git - Crafter.git/commitdiff
Make creeper head tracking work
authoroilboi <47129783+oilboi@users.noreply.github.com>
Fri, 15 May 2020 10:23:01 +0000 (06:23 -0400)
committeroilboi <47129783+oilboi@users.noreply.github.com>
Fri, 15 May 2020 10:23:01 +0000 (06:23 -0400)
mods/mob/api/head_code.lua
mods/mob/init.lua
mods/mob/models/creeper.b3d

index a76350fb545379c6948e5b2c8b58e16cfe10df03..f9bed64e0b196fdb46cf322d4330d9bd7ff8b3ff 100644 (file)
@@ -122,48 +122,38 @@ mobs.create_head_functions = function(def,mob_register)
                                        self.return_head_to_origin(self,dtime)
                                        return(false)
                                end
-                       --[[
+
                        elseif self.head_coord == "vertical" then
-                               
                                --print(self.head_bone)
                                local head_position,head_rotation = self.object:get_bone_position(self.head_bone)
-                               
-                               -- debug
-                               if head_rotation then
-                                       print("test")
+                               --[[ debug
+                               if rotation then
                                        --print("--------------------------------")
-                                       --head_rotation.x = head_rotation.x + 1
-                                       head_rotation.y = head_rotation.y + 1
-                                       head_rotation.z = 0
+                                       --rotation.x = rotation.x + 1
+                                       rotation.z = rotation.z + 1
+                                       rotation.y = 0
                                        
-                                       if head_rotation.x > 90 then
-                                                head_rotation.x = -90
-                                       end
-                                       if head_rotation.z > 90 then
-                                                head_rotation.z = -90
+                                       if rotation.x > 90 then
+                                                rotation.x = -90
                                        end
-                                       if head_rotation.y > 90 then
-                                                head_rotation.y = -90
+                                       if rotation.z > 90 then
+                                                rotation.z = -90
                                        end
                                        
                                        --print(rotation.x)
-                                       self.object:set_bone_position(self.head_bone, head_position, head_rotation)
+                                       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()
-                               if not body_yaw then
-                                       return
-                               end
-                               
-                               local dir = vector.multiply(minetest.yaw_to_dir(body_yaw+self.head_rotation_offset),self.head_directional_offset)
+                               local body_yaw = self.object:get_yaw()-math.pi/2
+                                                               
+                               local dir = vector.multiply(minetest.yaw_to_dir(body_yaw),self.head_directional_offset)
                                
                                
-                               local body_yaw = minetest.dir_to_yaw(dir)
+                               body_yaw = minetest.dir_to_yaw(dir)
                                
                                --pos is where the head actually is
                                pos = vector.add(pos,dir)
@@ -186,28 +176,29 @@ mobs.create_head_functions = function(def,mob_register)
                                --if the function was given a pos
                                if pos2 then
                                        --compare the head yaw to the body
+                                       --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))
-                                       
-                                       local goal_yaw = body_yaw-head_yaw
-                                       
-                                       if goal_yaw < -math.pi then
-                                               goal_yaw = goal_yaw + math.pi
-                                       elseif goal_yaw > math.pi then
-                                               goal_yaw = goal_yaw - math.pi
+                                       head_yaw = minetest.dir_to_yaw(minetest.yaw_to_dir(head_yaw))
+                                       head_yaw = degrees(head_yaw)-degrees(body_yaw)
+
+                                       if head_yaw < -180 then
+                                               head_yaw = head_yaw + 360
+                                       elseif head_yaw > 180 then
+                                               head_yaw = head_yaw - 360
                                        end
-                                       
-                                       print("goal_yaw="..goal_yaw,"body_yaw="..body_yaw,"head_yaw="..head_yaw)
-                                       
+
                                        --if within range then do calculations
-                                       if goal_yaw <= math.pi/2 and goal_yaw >= -math.pi/2 then
+                                       if head_yaw >= -90 and head_yaw <= 90 then
                                                ---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))
-                                               head_rotation.y = degrees(goal_yaw)
+                                               head_rotation.y = -head_yaw
                                                self.object:set_bone_position(self.head_bone, head_position, head_rotation)
                                                return(true)
                                        --nothing to look at
-                                       else--dofile(path.."movement.lua")
+                                       else
                                                self.return_head_to_origin(self,dtime)
                                                return(false)
                                        end
@@ -217,7 +208,6 @@ mobs.create_head_functions = function(def,mob_register)
                                        self.return_head_to_origin(self,dtime)
                                        return(false)
                                end
-                               ]]--
                        end
                end
        end
index 9556d1dc856e278a49ae7e583d5c6757df119cf3..122bfad512ded3db80d3a45abc67038562f30395 100644 (file)
@@ -321,12 +321,12 @@ mobs.register_mob(
         
         --these are used to anchor a point to the head position
         -----
-        automatic_face_movement_dir = -90,
+        automatic_face_movement_dir = 0,
         head_bone = "head",
         debug_head_pos = true,
-        head_directional_offset = 2, --used in vector.multiply(minetest.yaw_to_dir(body_yaw),head_offset)
+        --this always has to be slightly positive
+        head_directional_offset = 0.01,
         head_height_offset = 1.45, --added to the base y position
-        head_rotation_offset = 0,
         --use this to correct the head position initially because it becomes severly offset - look at your blender model to get this perfect
         head_position_correction = vector.new(0,2.4,0),
         head_coord = "vertical",
index 487410e1aac4e2a9317de8a59b55a3d77edfb2cd..0713cc27e89b7fb734da99243b0ea0dd39ded8cb 100644 (file)
Binary files a/mods/mob/models/creeper.b3d and b/mods/mob/models/creeper.b3d differ