diff options
author | TenPlus1 <kinsellaja@yahoo.com> | 2017-01-19 22:46:28 +0000 |
---|---|---|
committer | TenPlus1 <kinsellaja@yahoo.com> | 2017-01-19 22:46:28 +0000 |
commit | 6eb6e458af042d0e800c5b00416b14ae7fee71b8 (patch) | |
tree | 9f92f14d2f8f464fcc3d70ea37ac75e51e979c6f /api.lua | |
parent | 2bcd528e22ac9bd9bf9574f0da6cbefade1b7b76 (diff) |
Fixed rotation bug
Diffstat (limited to 'api.lua')
-rw-r--r-- | api.lua | 104 |
1 files changed, 73 insertions, 31 deletions
@@ -64,14 +64,12 @@ local abs = math.abs local min = math.min local max = math.max local atann = math.atan -local atan2 = math.atan2 local random = math.random local floor = math.floor local atan = function(x) if x ~= x then --error("atan bassed NaN") - --print ("atan based NaN") return 0 else return atann(x) @@ -109,20 +107,25 @@ end set_yaw = function(self, yaw) if yaw ~= yaw then +print ("--- yaw nan") return end - self.yaw = yaw + self.rotate + self.yaw = yaw -- + self.rotate self.object:setyaw(self.yaw) end set_velocity = function(self, v) + local yaw = self.object:getyaw() + self.rotate + self.object:setvelocity({ - x = sin(self.yaw) * -v, +-- x = sin(self.yaw) * -v, + x = sin(yaw) * -v, y = self.object:getvelocity().y, - z = cos(self.yaw) * v +-- z = cos(self.yaw) * v + z = cos(yaw) * v }) end @@ -506,8 +509,11 @@ local function is_at_cliff(self) return false end - local dir_x = -sin(self.yaw) * (self.collisionbox[4] + 0.5) - local dir_z = cos(self.yaw) * (self.collisionbox[4] + 0.5) + local yaw = self.object:getyaw() +-- local dir_x = -sin(self.yaw) * (self.collisionbox[4] + 0.5) +-- local dir_z = cos(self.yaw) * (self.collisionbox[4] + 0.5) + local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5) + local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5) local pos = self.object:getpos() local ypos = pos.y + self.collisionbox[2] -- just above floor @@ -642,6 +648,7 @@ do_jump = function(self) end local pos = self.object:getpos() + local yaw = self.object:getyaw() -- what is mob standing on? pos.y = pos.y + self.collisionbox[2] - 0.2 @@ -655,8 +662,10 @@ do_jump = function(self) end -- where is front - local dir_x = -sin(self.yaw) * (self.collisionbox[4] + 0.5) - local dir_z = cos(self.yaw) * (self.collisionbox[4] + 0.5) +-- local dir_x = -sin(self.yaw) * (self.collisionbox[4] + 0.5) +-- local dir_z = cos(self.yaw) * (self.collisionbox[4] + 0.5) + local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5) + local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5) -- what is in front of mob? local nod = node_ok({ @@ -1029,7 +1038,8 @@ function smart_mobs(self, s, p, dist, dtime) else -- dig 2 blocks to make door toward player direction - local yaw1 = self.yaw + pi / 2 +-- local yaw1 = self.yaw + pi / 2 + local yaw1 = self.object:getyaw() + pi / 2 local p1 = { x = s.x + cos(yaw1), @@ -1284,9 +1294,13 @@ local follow_flop = function(self) z = p.z - s.z } - local yaw = atan2(vec.z, vec.x) - pi / 2 +-- local yaw = atan2(vec.z, vec.x) - pi / 2 + local yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate + + if p.x > s.x then yaw = yaw + pi end - set_yaw(self, yaw) + self.object:setyaw(yaw) +-- set_yaw(self, yaw) -- anyone but standing npc's can move along if dist > self.reach @@ -1363,7 +1377,7 @@ end -- execute current state (stand, walk, run, attacks) local do_states = function(self, dtime) - local yaw = self.yaw -- 0 + local yaw = 0 -- self.yaw if self.state == "stand" then @@ -1389,12 +1403,17 @@ local do_states = function(self, dtime) z = lp.z - s.z } - yaw = atan2(vec.z, vec.x) - pi / 2 +-- yaw = atan2(vec.z, vec.x) - pi / 2 + yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate + + if lp.x > s.x then yaw = yaw + pi end else - yaw = random() * 2 * pi +-- yaw = random() * 2 * pi + yaw = (random(0, 360) - 180) / 180 * pi end - set_yaw(self, yaw) + self.object:setyaw(yaw) +-- set_yaw(self, yaw) end set_velocity(self, 0) @@ -1453,9 +1472,13 @@ local do_states = function(self, dtime) z = lp.z - s.z } - yaw = atan2(vec.z, vec.x) + pi / 2 +-- yaw = atan2(vec.z, vec.x) + pi / 2 + yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate + + if lp.x > s.x then yaw = yaw + pi end else - yaw = random() * 2 * pi +-- yaw = random() * 2 * pi + yaw = (random(0, 360) - 180) / 180 * pi end else @@ -1465,17 +1488,22 @@ local do_states = function(self, dtime) z = lp.z - s.z } - yaw = atan2(vec.z, vec.x) + pi / 2 +-- yaw = atan2(vec.z, vec.x) + pi / 2 + yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate + + if lp.x > s.x then yaw = yaw + pi end end - set_yaw(self, yaw) + self.object:setyaw(yaw) +-- set_yaw(self, yaw) -- otherwise randomly turn elseif random(1, 100) <= 30 then yaw = random() * 2 * pi - set_yaw(self, yaw) + self.object:setyaw(yaw) +-- set_yaw(self, yaw) end -- stand for great fall in front @@ -1560,9 +1588,13 @@ local do_states = function(self, dtime) z = p.z - s.z } - yaw = atan2(vec.z, vec.x) - pi / 2 +-- yaw = atan2(vec.z, vec.x) - pi / 2 + yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate + + if p.x > s.x then yaw = yaw + pi end - set_yaw(self, yaw) + self.object:setyaw(yaw) +-- set_yaw(self, yaw) if dist > self.reach then @@ -1724,9 +1756,13 @@ local do_states = function(self, dtime) z = p.z - s.z } - yaw = atan2(vec.z, vec.x) - pi / 2 +-- yaw = atan2(vec.z, vec.x) - pi / 2 + yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate - set_yaw(self, yaw) + if p.x > s.x then yaw = yaw + pi end + + self.object:setyaw(yaw) +-- set_yaw(self, yaw) -- move towards enemy if beyond mob reach if dist > self.reach then @@ -1832,9 +1868,13 @@ local do_states = function(self, dtime) z = p.z - s.z } - yaw = atan2(vec.z, vec.x) - pi / 2 +-- yaw = atan2(vec.z, vec.x) - pi / 2 + yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate + + if p.x > s.x then yaw = yaw + pi end - set_yaw(self, yaw) + self.object:setyaw(yaw) +-- set_yaw(self, yaw) set_velocity(self, 0) @@ -2233,7 +2273,8 @@ local mob_activate = function(self, staticdata, dtime_s, def) -- set anything changed above self.object:set_properties(self) - set_yaw(self, random() * 2 * pi) +-- set_yaw(self, random() * 2 * pi) + self.object:setyaw((random(0, 360) - 180) / 180 * pi) update_tag(self) end @@ -2241,7 +2282,8 @@ end local mob_step = function(self, dtime) local pos = self.object:getpos() - local yaw = self.yaw +-- local yaw = self.yaw + local yaw = 0 -- when lifetimer expires remove mob (except npc and tamed) if self.type ~= "npc" @@ -2373,8 +2415,8 @@ function mobs:register_mob(name, def) minetest.register_entity(name, { -automatic_face_movement_dir = def.rotate and math.rad(def.rotate) or false, -automatic_face_movement_max_rotation_per_sec = -1, +--automatic_face_movement_dir = def.rotate and math.rad(def.rotate) or false, +--automatic_face_movement_max_rotation_per_sec = -1, stepheight = def.stepheight or 0.6, name = name, |