如何使用CoronaSDK中的加速度计使一个物体旋转到倾斜角度?

我的第一个游戏中,我有一个可以通过加速计控制的涂鸦。许多游戏中,在设备倾斜时,物体(在我的情况下是一个涂鸦鱼)会向倾斜旋转,因此它会给人一种“鱼”游向下方的假象;如果设备向上倾斜和向下倾斜,“鱼”就会上下游动。

当使用 Corona SDK 编写时,我该如何编写 lua 代码?

这是到目前为止移动涂鸦的代码;

display.setStatusBar(display.HiddenStatusBar)
system.setAccelerometerInterval( 50 )

_W = display.contentWidth
_H = display.contentHeight

local bg = display.newImageRect("images/bg.png", 480, 320)
      bg:setReferencePoint(display.CenterReferencePoint)
      bg.x = _W/2
      bg.y = _H/2

local player = display.newImageRect("images/doodle.png", 128, 64)
     player:setReferencePoint(display.CenterReferencePoint)
     player.x = _W/2
     player.y = _H/2

-- 在横屏中设置加速计参数

local motionX = 0
local motionY = 0

local function onAccelerate( event )
     motionX = 10 * event.yGravity;
     motionY = 10 * event.xGravity;
end

Runtime:addEventListener ("accelerometer", onAccelerate);

-- 使玩家根据倾斜进行移动

local function movePlayer (event)
    player.x = player.x + motionX;
    player.y = player.y + motionY;
end

Runtime:addEventListener("enterFrame", movePlayer)

    local function screenBoundaries (event)

        -- 左边界
    if player.x < 0 + player.width/2 then
       player.x = 0 + player.width/2
    end

        -- 右边界
    if player.x > display.contentWidth - player.width/2 then
       player.x = display.contentWidth - player.width/2
    end

        -- 上边界
    if player.y < 0 + player.height/2 then
       player.y = 0 + player.height/2
    end

        -- 下边界
    if player.y > display.contentHeight - player.height/2 then
       player.y = display.contentHeight - player.height/2
    end
end

Runtime:addEventListener("enterFrame", screenBoundaries)

编辑;

我试图使玩家根据倾斜旋转,但它不起作用,当我在模拟器中运行它时,它没有返回任何错误,我错在哪里?

基本上,我想要做的就是当加速计 y 值增加/减少时,玩家(鱼)向上和向下游泳,但我只想要适度的倾斜角度(0 到 +/- 45 度),或者看起来更真实的角度。也许 25 - 35 度是最佳的?

我的数学已经过时了,所以我为此道歉,有人可以帮我吗?

display.setStatusBar(display.HiddenStatusBar)
system.setAccelerometerInterval( 50 )

_W = display.contentWidth
_H = display.contentHeight

local ceil = math.ceil
local pi = math.pi
local atan2 = math.atan2
local playerRotation

 -- 将涂鸦鱼放在屏幕中央。
    local player = display.newImageRect("images/doodle.png", 128, 64)
    player:setReferencePoint(display.CenterReferencePoint)
    player.x = _W/2
    player.y = _H/2

 -- 我的旋转函数,不确定如何正确地执行它。

    local function getPlayerRotationAngle(xGravity, yGravity)
    angle = math.atan2(xGravity, yGravity)

    angle = angle*(180/pi)

    playerRotate = ceil(360 - angle(xGravity, yGravity))

    return playerRotate
     end

 -- 在横屏中设置加速计参数

    local motionX = 0
    local motionY = 0

    local function onAccelerate( event )
          motionX = 5 * event.yGravity;
          motionY = 5 * event.xGravity;

          -- 我应该在这里还是在 movePlayer 函数中放置旋转呢?

          player.rotation = playerRotate(event.yGravity, event.xGravity);
    end

    Runtime:addEventListener ("accelerometer", onAccelerate);

 -- 使玩家根据倾斜进行移动

    function movePlayer (event)

         player.x = player.x + motionX
         player.y = player.y + motionY

    end

    Runtime:addEventListener("enterFrame", movePlayer)

原文链接 https://stackoverflow.com/questions/6187403

点赞
stackoverflow用户142137
stackoverflow用户142137

你的整体代码看起来很不错。 我之前做过一些类似的游戏。

但是,你提到了以下问题

我已经尝试让玩家旋转倾斜,但是没有效果,当我在模拟器上运行时没有返回任何错误,我哪里做错了?

你是否在实际设备上运行代码? Corona SDK 模拟器不支持模拟加速度计,因此事件永远不会触发。

如果你在设备上运行代码,请包括Crawl Space Library。 它可以让你打印任何形式的数据,包括表格。 然后在你的onAccelerate()函数中添加

print(event)

将设备连接到开发机器上,观看控制台获取你实际接收到的数据。 (如果你在Mac上开发iOS应用程序,请使用 iPhone Configuration Utility 获取数据)。然后从那里进行调试。

在main.lua的开头包括

io.output():setvbuf("no")

以先在设备上禁用控制台输出缓存。否则,设备的控制台输出将被缓冲,难以调试。

2011-06-04 02:13:10