如何在Lua和Corona SDK中进行加速度计偏移

我可以使用加速计来控制“鸟”的移动,但我的问题是它只在设备(手机/平板电脑)放平时才有效,而这并不适用于你手持设备玩游戏的情况。有没有办法将加速计的零点偏移,例如在y轴上偏移30度?

display.setStatusBar (display.HiddenStatusBar);
system.setAccelerometerInterval( 50 );
system.setIdleTimer(false);
local background = display.newImage("bg.png")

--> 游戏角色
local bird  = display.newImage ("helicopter.png")
bird.x      = 100;
bird.y      = 100;
bird:setReferencePoint(display.CenterReferencePoint);

-- 倾斜移动的速度。你可以改变它并查看其效果。
local tiltSpeed         = 30;
local motionx           = 0;
local motiony           = 0;
local rotation          = 0;

local function onTilt(event)
    -- 为鸟的新位置设置参数
    motionx = tiltSpeed * event.xGravity;
    motiony = tiltSpeed * event.yGravity;
end

local function moveBird (event)
    bird.x = motionx + bird.x;
    bird.y = bird.y - motiony;
    bird.rotation = rotation;
end

Runtime:addEventListener("accelerometer", onTilt)
Runtime:addEventListener("enterFrame", moveBird)
点赞
用户1847592
用户1847592
```lua
local delta = -30/180*math.pi  -- 30度
local cos_delta, sin_delta = math.cos(delta), math.sin(delta)

local function onTilt(event)
    -- 用于鸟的新位置
    motionx = tiltSpeed * event.xGravity
    motiony = tiltSpeed * (cos_delta*event.yGravity + sin_delta*event.zGravity)
end

```

2013-02-18 22:29:46