我有两个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