Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Set accelerometer to user position
Started by mmk.verheijden Jul 27 2013 09:08 AM

5 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

mmk.verheijden

[GLOBAL: userInfoPane.html]
mmk.verheijden
  • Contributor

  • 203 posts
  • Corona SDK

getting this error "game.lua Line: 319 Attempt to perform arithmetic on local 'gy' (a nil value)"

 

using this code : 

 

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
 

 

    
 
i am trying to set the accelerometers 0 point to the users position/the angel he is holding it 


[TOPIC: post.html]
#2

JonPM

[GLOBAL: userInfoPane.html]
JonPM
  • Corona Geek

  • 1,077 posts
  • Corona SDK

Would have to see more of this code to help.  Can you show the entire function as well as where it gets called?



[TOPIC: post.html]
#3

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,042 posts
  • Enterprise

Can you post some more code?  Where is event coming from?  Where are you setting up the accelerometer handler?



[TOPIC: post.html]
#4

mmk.verheijden

[GLOBAL: userInfoPane.html]
mmk.verheijden
  • Contributor

  • 203 posts
  • Corona SDK

Some more code : 

    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)
    
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

 



[TOPIC: post.html]
#5

jstrahan

[GLOBAL: userInfoPane.html]
jstrahan
  • Corona Geek

  • 1,926 posts
  • Corona SDK

event.yGravity, event.zGravity  are not in the event function so event.yGravity, event.zGravity  = nil so gy,gz = nil



[TOPIC: post.html]
#6

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,042 posts
  • Enterprise

This line of code:

 

local gy, gz = event.yGravity, event.zGravity

 

is executed outside of your onTilt function.  At that point your app doesn't know what event is and it's certainly not populating the xGravity, yGravity and zGravity values.   You have to do these calculations inside your onTilt() function.  You will have to have a flag indicating that you need to save the first values and the first time onTilt() is called, then you can set your baseline.




[topic_controls]
[/topic_controls]