Testing accelerometer

Basically i tied the gravity of my game to an acelerometer event.
Does anyone know a way to test the correct functionality with the simulator?
Cause the “shake” command is not what I am searching for.
Now im stuck with the a static game and i have no clue if it works or not
because i cant tilt the computer to create an acceleration obviously :laughing:

thanks in advance to anyone who will take time to reply

In general simulator on apple is better than simulator on windows (more interactive) … but I’m not sure if what are asking for is doable or not …

You can do a special build that stores the event readings in a file that you can send to yourself to calibrate your work based on the readings … or just show the values on the screen

1 Like

Live building is the best solution:
https://docs.coronalabs.com/guide/distribution/liveBuild/index.html

…or, if you want to be more “old school”:
Use the key event to simulate the axes variance, but then you’ll need to tune the sensitivity to match the values fired by the accelerometer.

thanks, now another problem came up.
I want the gravity to activate itself after a 30degree phone tilt.
Works in every direction but not on the negative x axis (right side tilt)
Anyone knows a fix?

this is the code i made

-- Set up display and physics

local physics = require("physics")

physics.start()

-- Create a box object

local box = display.newRect(display.contentCenterX, 100, 50, 50)

box:setFillColor(1, 0.5, 0.5)

-- Enable physics for the box

physics.addBody(box, "dynamic", { density = 1.0, friction = 0.3, bounce = 0 })

-- Function to calculate angle in degrees from x and y gravity values

local function calculateTiltAngle(xGravity, yGravity)

    return math.deg(math.atan2(yGravity, xGravity))

end

-- Function to handle accelerometer updates

local function onAccelerate(event)

    local xGravity = event.xGravity

    local yGravity = event.yGravity

    -- Calculate tilt angle in degrees

    local tiltAngle = calculateTiltAngle(xGravity, yGravity)

    -- Apply gravity only if tilt angle is greater than 30 degrees or less than -30 degrees

    if math.abs(tiltAngle) > 30 then

        physics.setGravity(xGravity * 9.8, -yGravity * 9.8)

    else

        physics.setGravity(0, 0)  -- No gravity if tilt angle is within -30 to 30 degrees

    end

end

-- Set accelerometer interval and listen for changes

system.setAccelerometerInterval(60)  -- Update interval in Hz 

Runtime:addEventListener("accelerometer", onAccelerate)