Yaw and velocity

User avatar
runs
Member
 
Posts: 886
Joined: Sat Oct 27, 2018 08:32
GitHub: runsy

Yaw and velocity

by runs » Tue May 21, 2019 10:08

I want to set a yawn for an entity and want to increase the velocity in that yaw. My knowledge of trigonometry is terrible. :-/

Code: Select all
local yaw = clicker:get_look_horizontal()
self.object:set_yaw(yaw) --the mob has to runaway in the same way that the clicker
self.object:set_velocity( ????? )
 

User avatar
AiTechEye
Member
 
Posts: 744
Joined: Fri May 29, 2015 21:14
Location: Unknown
GitHub: AiTechEye

Re: Yaw and velocity

by AiTechEye » Tue May 21, 2019 11:51

Code: Select all
lookat=function(self,pos2)
   local pos1=self.object:get_pos()
   local vec = {x=pos1.x-pos2.x, y=pos1.y-pos2.y, z=pos1.z-pos2.z}
   local yaw = math.atan(vec.z/vec.x)-math.pi/2
   if pos1.x >= pos2.x then yaw = yaw+math.pi end
   self.object:set_yaw(yaw)
end

walk=function(self,speed)
   local yaw = self.object:get_yaw()
   local speed = speed or 1
   local x = math.sin(yaw) * -1
   local z = math.cos(yaw) * 1
   local y = self.object:get_velocity().y
   self.object:set_velocity({
      x = x*speed,
      y = y,
      z = z*speed
   })
   return self
end

run_away=function(self,pos)
   lookat(self,pos)
   self.object:get_yaw()
   self.object:set_yaw(yaw+math.pi)
   walk(self,2)
end
 

Termos
Member
 
Posts: 226
Joined: Sun Dec 16, 2018 12:50

Re: Yaw and velocity

by Termos » Tue May 21, 2019 20:46

????? = vector.multiply(minetest.yaw_to_dir(yaw),speed)
 

User avatar
runs
Member
 
Posts: 886
Joined: Sat Oct 27, 2018 08:32
GitHub: runsy

Re: Yaw and velocity

by runs » Tue May 21, 2019 20:47

AiTechEye wrote:
Code: Select all
lookat=function(self,pos2)
   local pos1=self.object:get_pos()
   local vec = {x=pos1.x-pos2.x, y=pos1.y-pos2.y, z=pos1.z-pos2.z}
   local yaw = math.atan(vec.z/vec.x)-math.pi/2
   if pos1.x >= pos2.x then yaw = yaw+math.pi end
   self.object:set_yaw(yaw)
end

walk=function(self,speed)
   local yaw = self.object:get_yaw()
   local speed = speed or 1
   local x = math.sin(yaw) * -1
   local z = math.cos(yaw) * 1
   local y = self.object:get_velocity().y
   self.object:set_velocity({
      x = x*speed,
      y = y,
      z = z*speed
   })
   return self
end

run_away=function(self,pos)
   lookat(self,pos)
   self.object:get_yaw()
   self.object:set_yaw(yaw+math.pi)
   walk(self,2)
end


Wow, thanks!!! It works like a charm!!!

Just a little error:

local yaw = self.object:get_yaw()
 

User avatar
runs
Member
 
Posts: 886
Joined: Sat Oct 27, 2018 08:32
GitHub: runsy

Re: Yaw and velocity

by runs » Tue May 21, 2019 20:48

Termos wrote:????? = vector.multiply(minetest.yaw_to_dir(yaw),speed)


Thanks.
 

User avatar
Krock
Developer
 
Posts: 4389
Joined: Thu Oct 03, 2013 07:48
Location: Switzerland
GitHub: SmallJoker

Re: Yaw and velocity

by Krock » Wed May 22, 2019 17:25

To rotate it automatically into the movement direction: https://github.com/minetest/minetest/bl ... .txt#L5683

Smoothen it: https://github.com/minetest/minetest/bl ... .txt#L5687

Code: Select all
-- entity definition .....
automatic_face_movement_dir = 0,
automatic_face_movement_max_rotation_per_sec = 100,
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>
 


Return to Modding Discussion



Who is online

Users browsing this forum: No registered users and 3 guests