Using the Accelerometer

I have multiple questions but they all surround the accelerometer.
Background of the game. A bunch of balls on screen rolling around using accelerometer.

1st: I used the script from the input examples and was able to get all the balls moving. Debug has issues with aquire_focus over 16 am I going about this wrong? they still move but am I goign to run into issues with the buffer full.

2nd: When the game first loads the balls start as intended but always roll to the right of the screen and then pile up. This is unintended. I can post the code if that will help but its the exact code from the accelerometer example code code with increased force.

3rd: If I am to have a game over screen I have the gui acquire focus but the balls still roll in the background. can I revoke focus from them somehow? or disable the accelerometer maybe.

Also I have read all I could find on the accelerometer and acquiring focus I could find.

Thanks for any Help!

Have 1 script acquire input and save the input into a Lua module. Then have all of the balls detect the input values on update and act based on the data there.

You might need to add a dead zone to the accelerometer input. So if it’s not outside of the deadzone range it has no impact.

You can consume the input when the game over screen is active. Or return false at the top of the ball on input when game over is active.

Any examples of this being done I could look at? Makes perfect sense but how id code it is another thing.

Acquire input from one script and in this script read the accelerometer data and pass that to or directly affect the balls from the script.

Is this when starting with the phone laying on a flat surface? The accelerometer module that I provide in my input lib will take whatever accelerometer data it receives when started/calibrated and consider that a “zero” state. Tilting on any axis from this zero state will give you calibrated accelerometer data in x and y direction.

You can release_input_focus to stop receiving any input to a script.

So I laid it out flat then picked it up and that made them work right. Any idea how id have it calibrate holding it or is that just an issue with using the accelerometer? I didnt seem to have the same issue when I made an apk from the input examples.

You can call calibrate() at any time to calibrate the accelerometer data based on the current orientation. Do this in your game when transitioning from say the main menu to the game.

Thank you appreciate the help.

I am having Trouble passing the data to the factory created game objects. I Have them reporting into the script and storing them into a table. But I can’t seem to get them out of the table I get an error of unknown instance. I spent hours with trial and error.

> local acc = require "in.accelerometer"
> 
> function init(self)
>   msg.post(".", "acquire_input_focus")
>   self.ball_id = {}
>   self.height = tonumber(sys.get_config("display.width", 0))
>   self.width = tonumber(sys.get_config("display.height", 0))
>   window.set_listener(function(self, event, data)
>   if event == window.WINDOW_EVENT_RESIZED then
>     acc.on_window_resized(data.width, data.height)
>     self.width = data.height
>     self.height = data.width
>   end
>   end)
>   acc.calibrate()
> end
> 
> function on_input(self, action_id, action)
> 
>   if not action.acc_x then
>     local action_pos = vmath.vector3(action.x, action.y, 0)
>     if action.pressed then
>       self.drag = true
>       action.dx = 0
>       action.dy = 0
>       acc.calibrate()
>     elseif action.released then
>       self.drag = nil
>       acc.calibrate()
>     end
> 
>     if self.drag then
>       local diff = action_pos - vmath.vector3(self.width / 2, self.height / 2, 0)
>       action.acc_x = diff.x / (self.height / 2)
>       action.acc_y = diff.y / (self.width/ 2)
>       action.acc_z = 0
>     else
>       action.acc_x = 0
>       action.acc_y = 0
>       action.acc_z = 0
>     end
>     acc.on_input(action)
>   else
>     --action.acc_x, action.acc_y = action.acc_y, action.acc_x
>     --action.acc_x = action.acc_x * -1
>     acc.on_input(action)
> 
> 
> 
>   end
> end
> 
> function final(self)
>   acc.reset()
> end
> 
> 
> local function update_label()
>   local avg = acc.average()
>   local zero = acc.zero()
>   local latest = acc.latest()
>   local calibrated = acc.calibrated()
>   local adjusted = acc.adjusted()
> 
>   local info = ([[
>   Average (%.3f),(%.3f),(%.3f)
>   Zero (%.3f),(%.3f),(%.3f)
>   Latest (%.3f),(%.3f),(%.3f)
>   Calibrated (%.3f),(%.3f),(%.3f)
>   Adjusted (%.3f),(%.3f),(%.3f)
>   ]]):format(
>   avg.x, avg.y, avg.z,
>   zero.x, zero.y, zero.z,
>   latest.x, latest.y, latest.z,
>   calibrated.x, calibrated.y, calibrated.z,
>   adjusted.x, adjusted.y, adjusted.z
>   )
>   label.set_text("info#label", info)
> end
> 
> function update(self, dt)
>   update_label()
>   local adjusted = acc.adjusted()
>   local force = adjusted * 100000 * dt
> 
>   if self.ball_id ~= nil then
>     for _, value in pairs(self.ball_id) do
>       do
>         print(value)
> 
>         msg.post(value, "#apply_force", { position = go.get_world_position(), force = force })
>         --msg.post(msg.url(nil, value, "#apply_force"), { position = go.get_world_position(), force = force })
>       end
> 
> 
>     end
>   end
> end
> 
> function on_message(self, message_id, message, sender)
>   if message_id==hash("report_in") then
>     table.insert(self.ball_id, sender)
>   end
> end
> function on_reload(self)
>   -- Add reload-handling code here
>   -- Remove this function if not needed
> end