2013-07-20 32 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

显示您的代码。 –

相关问题