2013-07-20 33 views
1

嗨如何設置加速度計0指向用戶的位置/他持有的天使? 我使用:將加速度計設置爲用戶位置

delta = -40/180*math.pi -- 
cos_delta, sin_delta = math.cos(delta), math.sin(delta) 

要偏移,但居屋我可以讓設備被拿下的天使是0

額外的代碼:

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


    --delta = -50/180*math.pi -- 30 degrees, maybe should have minus sign 
    --cos_delta, sin_delta = math.cos(delta), math.sin(delta) 

-- Firstly, you need to get accelerometer's values in "zero position" 
-- probably, inside onTilt event 
local gy, gz = event.yGravity, event.zGravity 
local len = math.sqrt(gy*gy+gz*gz) * (gz < 0 and -1 or 1) 
cos_delta = gz/len 
sin_delta = -gy/len 

local function onTilt(event) 
    motionx = tiltSpeed * event.xGravity 
    motiony = tiltSpeed * (cos_delta*event.yGravity + sin_delta*event.zGravity) 
end 

回答

1
-- Firstly, you need to get accelerometer's values in "zero position" 
-- probably, inside onTilt event 
local gy, gz = event.yGravity, event.zGravity 

-- Secondly, update your cos_delta and sin_delta to remember "zero position" 
local len = math.sqrt(gy*gy+gz*gz) * (gz < 0 and -1 or 1) 
cos_delta = gz/len 
sin_delta = -gy/len 
+0

是用來代替「delta = -40/180 * math.pi cos_delta,sin_delta = math.cos(delta),math.sin(delta)」? –

+0

得到這個錯誤「game.lua線:319嘗試對本地'gy'執行算術(無值)」 –

+0

顯示您的代碼。 –

相關問題