Przeglądaj źródła

Merge pull request #32 from mikatuo/look_at

Movement > Look At > Moved code into look_at function for reusability
Björn Ritzl 2 lat temu
rodzic
commit
c4e417b965
1 zmienionych plików z 12 dodań i 4 usunięć
  1. 12 4
      examples/movement/look_at/look_at.script

+ 12 - 4
examples/movement/look_at/look_at.script

@@ -3,14 +3,22 @@ function init(self)
 	msg.post(".", "acquire_input_focus")
 end
 
-function on_input(self, action_id, action)
-	-- the position to look at (mouse/finger)
-	local look_at = vmath.vector3(action.x, action.y, 0)
+local function look_at(target_position)
 	-- own positon
 	local my_position = go.get_position()
 
 	-- calculate the angle that this object has to rotate to look at the given point
-	local angle = math.atan2(my_position.x - look_at.x, look_at.y - my_position.y)
+	local angle = math.atan2(my_position.x - target_position.x, target_position.y - my_position.y)
 	-- set rotation as a quaternion
 	go.set_rotation(vmath.quat_rotation_z(angle))
+end
+
+function on_input(self, action_id, action)
+	-- mouse/finger movement has action_id set to nil
+	if not action_id then
+		-- the position to look at (mouse/finger)
+		local target_position = vmath.vector3(action.x, action.y, 0)
+		-- rotate this object to look at the target position
+		look_at(target_position)
+	end
 end