Rotate A Object With Acelerometer

I want to rotate a object with acelerometer like this game:

https://www.youtube.com/watch?v=tWUaKRrXof8

i have this code:

[lua]local object = display.newImage(“object.png”)
object:setReferencePoint(display.CenterReferencePoint)
object.x = 150
object.y = 200

local acc = {}
local centerX = 150
local centerY = 200

function acc:accelerometer(e)
    object.y = centerY + (centerY * e.yGravity)
    
   
    end
    
    Runtime:addEventListener(“accelerometer”, acc)[/lua]

but its not working like i need

I make this code:

[lua]

– Hides the status bar
display.setStatusBar (display.HiddenStatusBar);
local physics = require “physics”
physics.start()

– Set Accelerometer Interval
system.setAccelerometerInterval( 50 );

– Prevent the app from becoming suspended
system.setIdleTimer(false);
physicsData = (require “shapedefs”).physicsData(1.0)
local carro = display.newGroup()

        rodaT = display.newImageRect(“roda.png”,22,22)
        rodaT.x = 50
        rodaT.y = 175
        physics.addBody(rodaT,{density=10,friction=1,bounce=0,radius=11})
        carro:insert(rodaT)
        
        rodaD = display.newImageRect(“roda.png”,22,22)
        rodaD.x = 112
        rodaD.y = 175
        physics.addBody(rodaD,{density=10,friction=1,bounce=0,radius=11})
        carro:insert(rodaD)
                
        camaro = display.newImage(“camaro.png”)
        camaro.myName = “camaro”
        physics.addBody(camaro,physicsData:get(“camaro”))
        camaro.x = 78
        camaro.y = 180
        carro:insert(camaro)
        
        backDamper = display.newRect(45, 167, 10, 10)
        backDamper:setFillColor(255,75,75)
        backDamper.alpha = 0
        physics.addBody(backDamper,{density=10,friction=0,bounce=0})
        carro:insert(backDamper)
        
        frontDamper = display.newRect(107, 167, 10, 10)
        frontDamper:setFillColor(255,75,75)
        frontDamper.alpha = 0
        physics.addBody(frontDamper,{density=10,friction=0,bounce=0})
        carro:insert(frontDamper)
        
        retblue1 = display.newImage(“retblue.png”)
        physics.addBody(retblue1,“static”,{density=10,friction=5,bounce=0})
        retblue1.x = 300
        retblue1.y = 205
        carro:insert(retblue1)
        
        retblue2 = display.newImage(“retblue.png”)
        physics.addBody(retblue2,“static”,{density=10,friction=5,bounce=0})
        retblue2.x = 892
        retblue2.y = 205
        carro:insert(retblue2)
        
        retblue3 = display.newImage(“retblue.png”)
        physics.addBody(retblue3,“static”,{density=10,friction=5,bounce=0})
        retblue3.x = 1484
        retblue3.y = 205
        carro:insert(retblue3)
        
        rampablue1 = display.newImage(“rampablue.png”)
        rampablue1.myName = “rampablue”
        physics.addBody(rampablue1,“static”, physicsData:get(“rampablue”))
        rampablue1.x = 1990
        rampablue1.y = 100        
        carro:insert(rampablue1)
        
        acne1 = display.newImage(“acne.png”)
        acne1.myName = “acne”
        physics.addBody(acne1,“static”, physicsData:get(“acne”))
        acne1.x = 3000
        acne1.y = -300
        carro:insert(acne1)
        
        
        

–joints

        backPistonJoint = physics.newJoint( “piston”,  backDamper,camaro, backDamper.x,backDamper.y,0,-1)
        backPistonJoint.isLimitEnabled = true
        backPistonJoint:setLimits(15,20)
        backPistonJoint.isMotorEnabled = true
        backPistonJoint.motorSpeed = 50
        backPistonJoint.maxMotorForce = 10

        frontPistonJoint = physics.newJoint(“piston”, frontDamper, camaro, frontDamper.x, frontDamper.y,0,-1)
        frontPistonJoint.isLimitEnabled = true
        frontPistonJoint:setLimits(15,20)
        frontPistonJoint.isMotorEnabled = true
        frontPistonJoint.motorSpeed = 50
        frontPistonJoint.maxMotorForce = 10

        backPivotJoint = physics.newJoint(“pivot”, rodaT,backDamper,rodaT.x,rodaT.y)
        backPivotJoint.isMotorEnabled = true
        backPivotJoint.maxMotorTorque = 10
        backPivotJoint.motorSpeed = 0

        frontWheelJoint = physics.newJoint(“pivot”, rodaD,frontDamper,rodaD.x,rodaD.y)
        frontWheelJoint.isMotorEnabled = false
        frontWheelJoint.maxMotorTorque = 10
        
      
        
        left = display.newImage(“leftx.png”);
        left.x = 40 ; left.y = 300;

        right = display.newImage(“rightx.png”);
        right.x = 455; right.y = 300;
        
        moverx = 0;

function stop (event)
    if(event.phase == “ended”) then
        motionx = 0;
    end
end
Runtime:addEventListener(“touch”, stop );

function esquerda()
    print(“left”)
moverx = -400;
end
left:addEventListener(“touch”, esquerda);

function direita()
    print(“right”)
moverx = 400;
end
right:addEventListener(“touch”, direita);

function moveRoda(event)
–frontWheel.x = frontWheel.x + motionx
    rodaT:applyAngularImpulse(moverx,0,rodaT.x,rodaT.y)
    --backWheel:applyAngularImpulse( motionx )
–frontWheel.y = frontWheel.y + motionx
end
Runtime:addEventListener(“enterFrame”, moveRoda)

function loop(e)
    
    local alvo = 100 - rodaT.x
    carro.x = carro.x + ((alvo - carro.x) *0.1)
    local alvoy = 200 - rodaT.y
    carro.y = carro.y + ((alvoy - carro.y) *0.1)
    
    
end
Runtime:addEventListener(“enterFrame”,loop)   
        
        ----------------------------------------------------------
                
    
– Speed of Movement with tilt. You can change it ans see the effects.
local tiltSpeed         = 30;
local motionx             = 0;
local motiony             = 0;
local rotation             = 0;

local function onTilt(event)
    – Its for the new position of the bird
    --motionx = tiltSpeed * event.xGravity;
    --motiony = tiltSpeed * event.yGravity;

    – Don’t change this equaton. Its for new Rotation
    rotation = (math.atan2(event.xGravity, event.yGravity) * (180/math.pi));
    if (rotation < 0) then
        rotation = 360 + rotation;
    end

end

local function moveCar (event)
    – Move to new Position
    camaro.x = motionx + camaro.x;
    camaro.y = camaro.y - motiony;
    
    
    if(camaro.x<0)then
        camaro.x = 0;
    end
    if(camaro.x>320)then
        camaro.x = 320;
    end
    if(camaro.y<0)then
        camaro.y = 0;
    end
    if(camaro.y>480)then
        camaro.y = 480;
    end
    --]]
    – Set New Angle
    camaro.rotation = rotation;
end

Runtime:addEventListener(“accelerometer”, onTilt)
Runtime:addEventListener(“enterFrame”, moveCar)

[/lua]

but when run on the device the car disappears on the screen…

Hi everybody
 
try to explain again
 
I would like to rotate a car like this example:

 

 

I created a code based on that post:

 

http://forums.coronalabs.com/topic/33905-how-do-i-rotate-an-object-around-a-specific-axis/?hl=rotate

 

but when i created a car with body joints they doesn´t work

I want to rotate the car around the dot

I use a weld joint to join the body of the car (camaro) and the dot in the center of the car

 

[lua] weldJoint = physics.newJoint(“weld”,dot,camaro,dot.x,dot.y) [/lua]

 

and the function to move the car

 

[lua]local doTurn = function(event)
    camaro.x = dot.x  + Cos(Rad(angle)) * radius
    camaro.y = dot.y  + Sin(Rad(angle)) * radius
    
    local angleBetween = Atan2(dot.y-camaro.y, dot.x-camaro.x)
    camaro.rotation = Deg(angleBetween)
        
    angle = angle + event.yGravity*10    
end        [/lua]

 

and here a the complete code with the images:

 

https://dl.dropboxusercontent.com/u/11765686/rotateCar.rar

 

Please help me :frowning:

 

 

I make this code:

[lua]

– Hides the status bar
display.setStatusBar (display.HiddenStatusBar);
local physics = require “physics”
physics.start()

– Set Accelerometer Interval
system.setAccelerometerInterval( 50 );

– Prevent the app from becoming suspended
system.setIdleTimer(false);
physicsData = (require “shapedefs”).physicsData(1.0)
local carro = display.newGroup()

        rodaT = display.newImageRect(“roda.png”,22,22)
        rodaT.x = 50
        rodaT.y = 175
        physics.addBody(rodaT,{density=10,friction=1,bounce=0,radius=11})
        carro:insert(rodaT)
        
        rodaD = display.newImageRect(“roda.png”,22,22)
        rodaD.x = 112
        rodaD.y = 175
        physics.addBody(rodaD,{density=10,friction=1,bounce=0,radius=11})
        carro:insert(rodaD)
                
        camaro = display.newImage(“camaro.png”)
        camaro.myName = “camaro”
        physics.addBody(camaro,physicsData:get(“camaro”))
        camaro.x = 78
        camaro.y = 180
        carro:insert(camaro)
        
        backDamper = display.newRect(45, 167, 10, 10)
        backDamper:setFillColor(255,75,75)
        backDamper.alpha = 0
        physics.addBody(backDamper,{density=10,friction=0,bounce=0})
        carro:insert(backDamper)
        
        frontDamper = display.newRect(107, 167, 10, 10)
        frontDamper:setFillColor(255,75,75)
        frontDamper.alpha = 0
        physics.addBody(frontDamper,{density=10,friction=0,bounce=0})
        carro:insert(frontDamper)
        
        retblue1 = display.newImage(“retblue.png”)
        physics.addBody(retblue1,“static”,{density=10,friction=5,bounce=0})
        retblue1.x = 300
        retblue1.y = 205
        carro:insert(retblue1)
        
        retblue2 = display.newImage(“retblue.png”)
        physics.addBody(retblue2,“static”,{density=10,friction=5,bounce=0})
        retblue2.x = 892
        retblue2.y = 205
        carro:insert(retblue2)
        
        retblue3 = display.newImage(“retblue.png”)
        physics.addBody(retblue3,“static”,{density=10,friction=5,bounce=0})
        retblue3.x = 1484
        retblue3.y = 205
        carro:insert(retblue3)
        
        rampablue1 = display.newImage(“rampablue.png”)
        rampablue1.myName = “rampablue”
        physics.addBody(rampablue1,“static”, physicsData:get(“rampablue”))
        rampablue1.x = 1990
        rampablue1.y = 100        
        carro:insert(rampablue1)
        
        acne1 = display.newImage(“acne.png”)
        acne1.myName = “acne”
        physics.addBody(acne1,“static”, physicsData:get(“acne”))
        acne1.x = 3000
        acne1.y = -300
        carro:insert(acne1)
        
        
        

–joints

        backPistonJoint = physics.newJoint( “piston”,  backDamper,camaro, backDamper.x,backDamper.y,0,-1)
        backPistonJoint.isLimitEnabled = true
        backPistonJoint:setLimits(15,20)
        backPistonJoint.isMotorEnabled = true
        backPistonJoint.motorSpeed = 50
        backPistonJoint.maxMotorForce = 10

        frontPistonJoint = physics.newJoint(“piston”, frontDamper, camaro, frontDamper.x, frontDamper.y,0,-1)
        frontPistonJoint.isLimitEnabled = true
        frontPistonJoint:setLimits(15,20)
        frontPistonJoint.isMotorEnabled = true
        frontPistonJoint.motorSpeed = 50
        frontPistonJoint.maxMotorForce = 10

        backPivotJoint = physics.newJoint(“pivot”, rodaT,backDamper,rodaT.x,rodaT.y)
        backPivotJoint.isMotorEnabled = true
        backPivotJoint.maxMotorTorque = 10
        backPivotJoint.motorSpeed = 0

        frontWheelJoint = physics.newJoint(“pivot”, rodaD,frontDamper,rodaD.x,rodaD.y)
        frontWheelJoint.isMotorEnabled = false
        frontWheelJoint.maxMotorTorque = 10
        
      
        
        left = display.newImage(“leftx.png”);
        left.x = 40 ; left.y = 300;

        right = display.newImage(“rightx.png”);
        right.x = 455; right.y = 300;
        
        moverx = 0;

function stop (event)
    if(event.phase == “ended”) then
        motionx = 0;
    end
end
Runtime:addEventListener(“touch”, stop );

function esquerda()
    print(“left”)
moverx = -400;
end
left:addEventListener(“touch”, esquerda);

function direita()
    print(“right”)
moverx = 400;
end
right:addEventListener(“touch”, direita);

function moveRoda(event)
–frontWheel.x = frontWheel.x + motionx
    rodaT:applyAngularImpulse(moverx,0,rodaT.x,rodaT.y)
    --backWheel:applyAngularImpulse( motionx )
–frontWheel.y = frontWheel.y + motionx
end
Runtime:addEventListener(“enterFrame”, moveRoda)

function loop(e)
    
    local alvo = 100 - rodaT.x
    carro.x = carro.x + ((alvo - carro.x) *0.1)
    local alvoy = 200 - rodaT.y
    carro.y = carro.y + ((alvoy - carro.y) *0.1)
    
    
end
Runtime:addEventListener(“enterFrame”,loop)   
        
        ----------------------------------------------------------
                
    
– Speed of Movement with tilt. You can change it ans see the effects.
local tiltSpeed         = 30;
local motionx             = 0;
local motiony             = 0;
local rotation             = 0;

local function onTilt(event)
    – Its for the new position of the bird
    --motionx = tiltSpeed * event.xGravity;
    --motiony = tiltSpeed * event.yGravity;

    – Don’t change this equaton. Its for new Rotation
    rotation = (math.atan2(event.xGravity, event.yGravity) * (180/math.pi));
    if (rotation < 0) then
        rotation = 360 + rotation;
    end

end

local function moveCar (event)
    – Move to new Position
    camaro.x = motionx + camaro.x;
    camaro.y = camaro.y - motiony;
    
    
    if(camaro.x<0)then
        camaro.x = 0;
    end
    if(camaro.x>320)then
        camaro.x = 320;
    end
    if(camaro.y<0)then
        camaro.y = 0;
    end
    if(camaro.y>480)then
        camaro.y = 480;
    end
    --]]
    – Set New Angle
    camaro.rotation = rotation;
end

Runtime:addEventListener(“accelerometer”, onTilt)
Runtime:addEventListener(“enterFrame”, moveCar)

[/lua]

but when run on the device the car disappears on the screen…

Hi everybody
 
try to explain again
 
I would like to rotate a car like this example:

 

 

I created a code based on that post:

 

http://forums.coronalabs.com/topic/33905-how-do-i-rotate-an-object-around-a-specific-axis/?hl=rotate

 

but when i created a car with body joints they doesn´t work

I want to rotate the car around the dot

I use a weld joint to join the body of the car (camaro) and the dot in the center of the car

 

[lua] weldJoint = physics.newJoint(“weld”,dot,camaro,dot.x,dot.y) [/lua]

 

and the function to move the car

 

[lua]local doTurn = function(event)
    camaro.x = dot.x  + Cos(Rad(angle)) * radius
    camaro.y = dot.y  + Sin(Rad(angle)) * radius
    
    local angleBetween = Atan2(dot.y-camaro.y, dot.x-camaro.x)
    camaro.rotation = Deg(angleBetween)
        
    angle = angle + event.yGravity*10    
end        [/lua]

 

and here a the complete code with the images:

 

https://dl.dropboxusercontent.com/u/11765686/rotateCar.rar

 

Please help me :frowning: