]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/oldcode.txt
Fix mob head roll issue
[Crafter.git] / mods / mob / oldcode.txt
1 --converts yaw to degrees
2 local degrees = function(yaw)
3         yaw = yaw + math.pi
4         return(yaw*180.0/math.pi)
5 end
6
7 local degree_round = function(degree)
8         return(degree + 0.5 - (degree + 0.5) % 1)
9 end
10
11 local radians_to_degrees = function(radians)
12         return(radians*180.0/math.pi)
13 end
14
15 --a movement test to move the head
16 mob.move_head = function(self)
17         if self.child then
18                 local pos = self.object:get_pos()
19                 local body_yaw = self.object:get_yaw() - (math.pi/2)
20                 local dir = vector.multiply(minetest.yaw_to_dir(body_yaw),0.72)
21                 local real_dir = minetest.yaw_to_dir(body_yaw)
22                 local body_yaw = degree_round(degrees(minetest.dir_to_yaw(dir)))
23                 
24                 pos = vector.add(pos,dir)
25                 pos.y = pos.y + 0.36
26                 
27                 --pos is where the head actually is
28                 --STARE O_O
29                 for _,object in ipairs(minetest.get_objects_inside_radius(pos, 6)) do
30                         if object:is_player() then
31                                 local pos2 = object:get_pos()
32                                 pos2.y = pos2.y + 1.625
33                                 
34                                 local head_yaw  = degree_round(degrees(minetest.dir_to_yaw(vector.direction(pos,pos2))))                        
35                                 
36                                 local new_yaw = (head_yaw-body_yaw)
37
38                                 local pitch = 0 
39                                 local roll = 0  
40                                 if math.abs(new_yaw) <= 90 or math.abs(new_yaw) >= 270 then
41                                         --do other calculations on pitch and roll
42                                         
43                                         local triangle = vector.new(vector.distance(pos,pos2),0,pos2.y-pos.y)
44                                         
45                                         local tri_yaw = minetest.dir_to_yaw(triangle)+(math.pi/2)
46                                         
47                                         pitch = radians_to_degrees(tri_yaw)
48                                         
49                                         local pitch_adjustment = 0
50                                         
51                                         if new_yaw >= 270 then
52                                                 pitch_adjustment = new_yaw - 180
53                                         elseif new_yaw <= -270 then
54                                                 pitch_adjustment = new_yaw + 180
55                                         else
56                                                 pitch_adjustment = new_yaw
57                                         end
58                                         
59                                         local roll_adjustment = pitch_adjustment
60                                         
61                                         pitch_adjustment = 1-(math.abs(pitch_adjustment)/90)
62                                         pitch = pitch-- * pitch_adjustment
63                                         
64                                         
65                                         --------------------------------
66                                         
67                                         local roll_adjustment = 1-pitch_adjustment
68                                         
69                                         
70                                         local side_adjuster = 1
71                                         if new_yaw < 1 then
72                                                 side_adjuster = -1
73                                         end
74                                         
75                                         roll = (radians_to_degrees(tri_yaw)*side_adjuster)*roll_adjustment
76                                         
77                                         print(roll)
78                                         
79                                         
80                                 
81                                 else
82                                         new_yaw = 0
83                                 end
84                                 --                                                                      roll        newyaw      pitch
85                                 self.child:set_attach(self.object, "", vector.new(2.4,1.2,0), vector.new(180+roll,    new_yaw,    180+pitch))                           
86                                 --self.head_rotation = vector.new(180,new_yaw,180)
87                         end
88                 end
89         end
90 end