Amcasape Talnmxpotor hated


Room.ik = { }

Room.ik.keyboard_to_bone = {}

--
-- keyboard --> bone
--
Room.ik.keyboard_to_bone[string.byte('1')] = "Bone_LBackFootB"
Room.ik.keyboard_to_bone[string.byte('2')] = "Bone_RBackFootB"
Room.ik.keyboard_to_bone[string.byte('3')] = "Bone_LFrontFootB"
Room.ik.keyboard_to_bone[string.byte('4')] = "Bone_RFrontFootB"
Room.ik.keyboard_to_bone[string.byte('5')] = "Bone_TailD"
Room.ik.keyboard_to_bone[string.byte('6')] = "Bone_MouthB"
Room.ik.keyboard_to_bone[string.byte('7')] = "Bone_HeadEnd"

Room.ik.look_at = {
    -- enable/disable
    on = true,

    -- where to look
    target = {
        x = 0,
        y = 0,
        z = 0,
    },

    -- bone connector
    --bone = "Bip01 Head",
    --bone = "Bip01 TailNub",
    --bone = "Bone_MouthB",
    bone = "Bone_tailD",

    -- local pose tweak
    tweak = {
        axis = {
            x = 0,
            y = 1,
            z = 0,
        },

        angle = 45,
    },

    -- inverse kinematics
    link_count = 2,
    iteration_count = 12,
}

function Room.OnKeyHit(self, key)
    if Room.ik.keyboard_to_bone[key] then
        Room.ik.look_at.bone = Room.ik.keyboard_to_bone[key]
    elseif (key == string.byte(']')) then
        Room.ik.look_at.link_count =
        Room.ik.look_at.link_count + 1
    elseif (key == string.byte('[')) then
        if Room.ik.look_at.link_count != 0 then
           Room.ik.look_at.link_count =
           Room.ik.look_at.link_count - 1
        end
    elseif (key == string.byte('.')) then
        Room.ik.look_at.iteration_count =
        Room.ik.look_at.iteration_count + 1
    elseif (key == string.byte(',')) then
        if Room.ik.look_at.iteration_count != 0 then
           Room.ik.look_at.iteration_count =
           Room.ik.look_at.iteration_count - 1
        end
    end
end

function Room.NormalizedMouse(x, y)
    local w = Room:GetWidth()
    local h = Room:GetHeight()

    -- centered screen coordinates
    local u_x = (x - w/2)
    local u_y = (y - h/2)
    local u_length = math.sqrt(u_x*u_x + u_y*u_y)

    -- centered, normalized coordinates
    u_x = u_x/u_length
    u_y = u_y/u_length
    u_length = u_length/(0.5*math.sqrt(w*w + h*h))

    return u_x, u_y, u_length
end

function Room.OnMouseMove(this,x,y)
    --local pet_x, pet_y, pet_z = pet:GetPosition()
    local pet_x, pet_y, pet_z = 0, 0, 0
    local u_x, u_y, u_length = Room.NormalizedMouse(x,y)
    local look_offset_x, look_offset_y, look_offset_z = 0, -100, 0
    local look_scale = 100
   
    Room.ik.look_at.target.x = pet_x + look_offset_x - look_scale*u_x
    Room.ik.look_at.target.y = pet_y + look_offset_y
    Room.ik.look_at.target.z = pet_z + look_offset_z - look_scale*u_y
--[[   
    LogInfo(" x"..Room.ik.look_at.target.x..
            " y"..Room.ik.look_at.target.y..
            " z"..Room.ik.look_at.target.z)
--]]           
end

function Room.OnStartRoom(self)


    Room.m_cMainLight = CxLight()

    Room.m_cMainLight:SetType(LIGHT_POINT)
    Room.m_cMainLight:SetDiffuse(255, 255, 255, 255)
    Room.m_cMainLight:SetAttenuation0(0.1)
    Room.m_cMainLight:SetPosition(0, 300, 0)
    Room.m_cMainLight:SetDirection(0.0, -1.0, 0.0)
    Room.m_cMainLight:SetRange(900.0)

    -- Attach to Scene
    Room.m_cSceneManager:AddLight(Room.m_cMainLight)

end

function Room.OnStopRoom(self)
    Room.m_cSceneManager:RemoveLight(Room.m_cMainLight)
    Room.m_cMainLight = nil
end