3D旋转代码无法正常工作

时间:2014-04-22 16:51:55

标签: 3d lua rotation drawing geometry

首先,我有一些在Cheat Engine中运行的Lua代码来控制Sonic Adventure 2中的一些注入代码。(它不在线;不用担心。)注入的代码所做的就是调用在游戏中渲染图形的功能。该功能只能绘制3D线条。这不是一个真正的问题,因为我编写了一个从12个中抽出一个立方体的函数,我也可以为其他形状编写函数。

现在我需要能够旋转那个立方体。 Sonic Adventure 2处理旋转的方式(我非常肯定)是它在BAMS中存储了3个值(二进制角度测量系统;基本上,360度= 0x10000。)一个值对应于每个轴。旋转按Y,X,Z的顺序执行。由于我所做的绘图对应于游戏中的对象,这也是我在代码中处理旋转的方式。但是,旋转似乎不能正常工作。虽然它仍然形成六面体(角落仍然连接在一起等),但立方体会出现剪切和压扁,当我移动物体时,它会向错误的方向移动。

这是我的代码的相关部分:

function DrawLine3DPerformRotation(line, axis1, axis2, ctr1, ctr2, angle)
    --Internal function used by DrawLine3D
    if angle ~= 0 then
        for i=1,2 do
            local x = line[axis1..tonumber(i)] - ctr1
            local y = line[axis2..tonumber(i)] - ctr2
            local th = math.rad(angle * (360 / 0x10000))
            line[axis1..tonumber(i)] = (x*math.cos(th) - y*math.sin(th)) + ctr1
            line[axis1..tonumber(i)] = (x*math.sin(th) + y*math.cos(th)) + ctr2
        end
    end
end

function DrawLine3D(identifier, x1, y1, z1, x2, y2, z2, color, temp)
    local line = {}
    line.x1 = x1
    line.y1 = y1
    line.z1 = z1
    line.x2 = x2
    line.y2 = y2
    line.z2 = z2

    DrawLine3DPerformRotation(line, "x", "z", LDRotateCtrX, LDRotateCtrZ, LDRotateY)
    DrawLine3DPerformRotation(line, "y", "z", LDRotateCtrY, LDRotateCtrZ, LDRotateX)
    DrawLine3DPerformRotation(line, "x", "y", LDRotateCtrX, LDRotateCtrY, LDRotateZ)

    line.color = color
    if temp then
        LDLineListTemp[identifier] = line
    else
        LDLineList[identifier] = line
    end
    --UpdateLineList()
end

1 个答案:

答案 0 :(得分:1)

发现问题。在第9行,当axis1..tonumber(i)axis2..tonumber(i)时,它会显示{{1}}。