2013-02-18 62 views
1

我可以使用加速度計控制「鳥」,這是我的唯一問題,它可以在我的設備(手機/平板電腦)平坦時起作用,但是當您拿着它時不會閒置在你的手中玩。有沒有辦法將加速度計的零點偏移到y軸上的30°(度)?如何偏移加速度計lua corona sdk

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


--> GAME CHARACTER 
local bird = display.newImage ("helicopter.png") 
bird.x  = 100; 
bird.y  = 100; 
bird:setReferencePoint(display.CenterReferencePoint); 

-- Speed of Movement with tilt. You can change it ans see the effects. 
local tiltSpeed   = 30; 
local motionx   = 0; 
local motiony   = 0; 
local rotation   = 0; 

local function onTilt(event) 
    -- Its for the new position of the bird 
    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) 
+0

如果你有event.zGravity那麼你可以簡單地旋轉,3D重力矢量任何你想要的位置。 – 2013-02-18 20:35:38

+0

我將如何使用它? – 2013-02-18 21:43:34

+0

更改爲-30 :) – 2013-02-19 19:42:29

回答

2
local delta = -30/180*math.pi -- 30 degrees 
local cos_delta, sin_delta = math.cos(delta), math.sin(delta) 

local function onTilt(event) 
    -- Its for the new position of the bird 
    motionx = tiltSpeed * event.xGravity 
    motiony = tiltSpeed * (cos_delta*event.yGravity + sin_delta*event.zGravity) 
end