2015-02-10 228 views
0

我有两个Vec3s,Camera Forward和Turret Forward。这两种载体都在不同的飞机上,其中“摄像机前进”基于自由视角摄像机,“炮塔前进”由其所在的坦克决定,坦克开启的地形等。塔塔向上和摄像头向上几乎不会比赛。在2D平面上旋转3D矢量

我的问题如下:我希望炮塔能够以固定的速度(每秒44度)旋转,以便它始终与照相机指向的方向会聚。如果坦克处于一个奇怪的角度,它不能与相机会聚,它应该找到最近的地方并坐在那里,而不是无限期地抖动。

我不能为我的生活似乎解决了这个问题。我尝试了几种我在网上发现的方法,总是会产生奇怪的结果。

local forward = player.direction:rotate(player.turret, player.up) 
    local side  = forward:cross(player.up) 
    local projection = self.camera.direction:dot(forward) * forward + self.camera.direction:dot(side) * side 
    local angle  = math.atan2(forward.y, forward.x) - math.atan2(projection.y, projection.x) 

    if angle ~= 0 then 
     local dt = love.timer.getDelta() 

     if angle <= turret_speed * dt then 
      player.turret_velocity = turret_speed 
     elseif angle >= -turret_speed * dt then 
      player.turret_velocity = -turret_speed 
     else 
      player.turret_velocity = 0 
      player.turret   = player.turret + angle 
     end 
    end 

Figure 1

回答

0

经过一些更多的研究和测试,我结束了以下解决方案。它运作顺畅!

function Gameplay:moved_axisright(joystick, x, y) 
    if not self.manager.id then return end 

    local turret_speed = math.rad(44) 
    local stick  = cpml.vec2(-x, -y) 
    local player  = self.players[self.manager.id] 

    -- Mouse and axis control camera view 
    self.camera:rotateXY(stick.x * 18, stick.y * 9) 

    -- Get angle between Camera Forward and Turret Forward 
    local fwd = cpml.vec2(0, 1):rotate(player.orientation.z + player.turret) 
    local cam = cpml.vec2(1, 0):rotate(math.atan2(self.camera.direction.y, self.camera.direction.x)) 
    local angle = fwd:angle_to(cam) 

    -- If the turret is not true, adjust it 
    if math.abs(angle) > 0 then 
     local function new_angle(direction) 
      local dt  = love.timer.getDelta() 
      local velocity = direction * turret_speed * dt 
      return cpml.vec2(0, 1):rotate(player.orientation.z + player.turret + velocity):angle_to(cam) 
     end 

     -- Rotate turret into the correct direction 
     if new_angle(1) < 0 then 
      player.turret_velocity = turret_speed 
     elseif new_angle(-1) > 0 then 
      player.turret_velocity = -turret_speed 
     else 
      -- If rotating the turret a full frame will overshoot, set turret to camera position 
      -- atan2 starts from the left and we need to also add half a rotation. subtract player orientation to convert to local space. 
      player.turret   = math.atan2(self.camera.direction.y, self.camera.direction.x) + (math.pi * 1.5) - player.orientation.z 
      player.turret_velocity = 0 
     end 
    end 

    local direction   = cpml.mat4():rotate(player.turret, { 0, 0, 1 }) * cpml.mat4():rotate(player.orientation.z, { 0, 0, 1 }) 
    player.turret_direction = cpml.vec3(direction * { 0, 1, 0, 1 }) 
end 
0

我会做不同

  1. 在GCS获得摄像机的方向矢量c(全局坐标系)

  2. 获得GCS

    炮塔方向矢量t
    • 相同子弹1.
  3. 计算在展台方向

    旋转转塔方向矢量
    • t0=rotation(-44.0deg/s)*t
    • t1=rotation(+44.0deg/s)*t
  4. 现在计算点产品

    • a =dot(c,t)
    • a0=dot(c,t0)
    • a1=dot(c,t1)
  5. 确定转塔的旋转

    • 如果最大值(A0,A,A1)== A0旋转(-44.0deg /秒)`
    • 如果最大值(A0,A,A1)== A1旋转(44 。0deg /秒)`

[注释]

  • 这应该收敛到期望的方向
  • 角度步骤应被调整到用于更新这个
  • 的时间间隔相匹配您可以使用任何常见的子弹1,2的坐标系统,而不仅仅是GCS
  • 在这种情况下,点积是cos(angle between vectors),因为两个c,t是单位向量(如果取自标准变换矩阵)
  • 所以如果cos(角度)== 1那么方向是相同的
  • 但您的相机可以在不同的轴上旋转,所以只需找到最大的cos角度)