Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
GYROSCOPE — Gideros Forum

GYROSCOPE

victor_cappavictor_cappa Member
edited August 2013 in General questions
Hello everybody!
I was searching in the forum for a answer, but I didn't find it, so...
I'm trying to make a character move to one side of screen to the other using the gyroscope, but I'm not able to find the right code to do this.
I'm newbie, sorry about the stupid question :p

Thank you!

Comments

  • @victor_cappa I could not say it should be done with gyroscope, better with accelerometer. Check out the Accelereometer example in the Hardware section of examples inside Gideros Studio :)

    Likes: victor_cappa

    +1 -1 (+1 / -0 )Share on Facebook
  • victor_cappavictor_cappa Member
    edited August 2013
    Oh thank you!
    I'm trying to make a doodle jump kind of game (learning purposes).
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Then this example should be exactly what you need, just adjust some ratios to your feel and you can use only x axis.
    And here is also a Doodle Jump template:
    http://giderosmobile.com/forum/discussion/473/let039s-jump-d/p1
    ;)

    Likes: victor_cappa

    +1 -1 (+1 / -0 )Share on Facebook
  • Nice! I saw that example, but it uses mouse_down and mouse_up events. I'm trying to replace that with the accelerometer.
    You were really helpful, thank you again! :D
  • Hi ar2rsawseen! I erase the 'onMouseDown' event and I put the code below, but I'm getting this error:

    "character.lua:50: attempt to index global 'self' (a nil value)
    stack traceback:
    character.lua:50: in main chunk"


    here is the whole code:

    require "box2d"
    b2World = b2.World.new(0, 15, true);





    GCchar = gideros.class(Bitmap)

    function GCchar:init()
    self:setAnchorPoint(0.5, 0.5);
    self.body = b2World:createBody{type = b2.DYNAMIC_BODY,
    };
    self:setPosition(160, 492);
    self.body:setPosition(self:getPosition());
    local blockShape = b2.PolygonShape.new();
    blockShape:setAsBox(self:getWidth()*0.5, self:getHeight()*0.5);
    self.body:createFixture{shape = blockShape,
    friction = 0,
    restitution = 0,
    density = 5,
    isSensor = false
    };
    self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self);
    b2World:addEventListener(Event.BEGIN_CONTACT, self.onBeginCollision, self);
    self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self);
    self.body:setLinearVelocity(0, -15);
    end

    function GCchar:onEnterFrame()
    b2World:step(1/60, 8, 3);
    self:getParent():addChild(self);
    self:setPosition(self.body:getPosition());
    local selfX, selfY = self:getParent():localToGlobal(self:getPosition());
    if selfY < 250 then
    self:getParent():setY(250 -self:getY());
    end
    if selfX < 0 then -- para o char poder ir da esquerda para a direita e vice versa
    local bodyX, bodyY = self.body:getPosition();
    self.body:setPosition(320, bodyY);
    elseif selfX > 320 then
    local bodyX, bodyY = self.body:getPosition();
    self.body:setPosition(0, bodyY);
    end
    end

    ------------------------include accelerometer------------------------
    require "accelerometer"
    accelerometer:start()
    self.filter = 0.5
    self.fx = 0
    self.gravityScale = 10
    -- get accelerometer values
    local x, y = accelerometer:getAcceleration()
    -- apply IIR filter
    self.fx = x * self.filter + self.x * (1 - self.filter)
    self.world:setGravity(self.fx*self.gravityScale)


    function GCchar:onEnterFrame(event)
    local xSpeed, ySpeed = self.body:getLinearVelocity();
    if event.x < 160 then
    self.body:setLinearVelocity(-8, ySpeed);
    else
    self.body:setLinearVelocity(8, ySpeed);
    end
    event:stopPropagation();
    end
    function GCchar:onMouseUp(event)
    local xSpeed, ySpeed = self.body:getLinearVelocity();
    self.body:setLinearVelocity(0, ySpeed);
    event:stopPropagation();
    end

    function GCchar:onBeginCollision(event) -- colisão
    local bodyA, bodyB = event.fixtureA:getBody(), event.fixtureB:getBody();
    if bodyA == self.body or bodyB == self.body then
    local xSpeed, ySpeed = self.body:getLinearVelocity();
    if ySpeed > 0 then
    self.body:setLinearVelocity(xSpeed, -15);
    end
    end
    end
  • @victor_cappa you just need to include your accelerometer code into function GCchar:onEnterFrame() and not outside of it ;)
  • I can't figure out what I'm doing wrong :/ ... Now I'm getting this error

    character.lua:52: attempt to perform arithmetic on field 'x' (a nil value)
    stack traceback:
    character.lua:52: in function

    I include de acceleromater code into function GCchar:onEnterFrame()

    require "box2d"
    b2World = b2.World.new(0, 15, true);


    GCchar = gideros.class(Bitmap)

    function GCchar:init()
    self:setAnchorPoint(0.5, 0.5);
    self.body = b2World:createBody{type = b2.DYNAMIC_BODY,
    };
    self:setPosition(160, 492);
    self.body:setPosition(self:getPosition());
    local blockShape = b2.PolygonShape.new();
    blockShape:setAsBox(self:getWidth()*0.5, self:getHeight()*0.5);
    self.body:createFixture{shape = blockShape,
    friction = 0,
    restitution = 0,
    density = 10,
    isSensor = false
    };
    self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self);
    b2World:addEventListener(Event.BEGIN_CONTACT, self.onBeginCollision, self);
    self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self);
    self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self);
    self.body:setLinearVelocity(0, -13);
    end

    function GCchar:onEnterFrame()
    b2World:step(1/60, 8, 3);
    self:getParent():addChild(self);
    self:setPosition(self.body:getPosition());
    local selfX, selfY = self:getParent():localToGlobal(self:getPosition());
    if selfY < 240 then
    self:getParent():setY(240-self:getY());
    end
    if selfX < 0 then
    local bodyX, bodyY = self.body:getPosition();
    self.body:setPosition(320, bodyY);
    elseif selfX > 320 then
    local bodyX, bodyY = self.body:getPosition();
    self.body:setPosition(0, bodyY);
    end
    ------------------------include accelerometer------------------------
    require "accelerometer"
    accelerometer:start()
    self.filter = 0.5
    self.fx = 0
    self.gravityScale = 10
    -- get accelerometer values
    local x, y = accelerometer:getAcceleration()
    -- apply IIR filter
    self.fx = x * self.filter + self.x * (1 - self.filter)
    self.world:setGravity(self.fx*self.gravityScale)
    end

    function GCchar:onMouseDown(event)
    local xSpeed, ySpeed = self.body:getLinearVelocity();
    if event.x < 160 then
    self.body:setLinearVelocity(-4, ySpeed);
    else
    self.body:setLinearVelocity(4, ySpeed);
    end
    event:stopPropagation();

    end

    function GCchar:onMouseUp(event)
    local xSpeed, ySpeed = self.body:getLinearVelocity();
    self.body:setLinearVelocity(0, ySpeed);
    event:stopPropagation();
    end

    function GCchar:onBeginCollision(event)
    local bodyA, bodyB = event.fixtureA:getBody(), event.fixtureB:getBody();
    if bodyA == self.body or bodyB == self.body then
    local xSpeed, ySpeed = self.body:getLinearVelocity();
    if ySpeed > 0 then
    self.body:setLinearVelocity(xSpeed, -13);
    end
    end
    end

    I guess I need to study more theory before study 'real codes'...
  • the line
    self.fx = x * self.filter + self.x * (1 - self.filter)
    was supposed to be
    self.fx = x * self.filter + self.fx * (1 - self.filter)
    change self.x to self.fx ;)
  • I don't know how i missed that :P
    But now it says that 'character.lua:52: attempt to index field 'world' (a nil value)
    stack traceback:
    character.lua:52: in function '

    about this line of the code

    self.fx = x * self.filter + self.fx * (1 - self.filter)
    self.world:setGravity(self.fx*self.gravityScale)
  • well instead self.world you need to use the instance you've created: b2World
  • It worked!
    Now I'm trying to figure out how to only use x axis... When I take the

    self.fy*self.gravityScale

    from here

    b2World:setGravity(self.fx*self.gravityScale, - self.fy*self.gravityScale)

    I get an error "character.lua:45: bad argument #2 to 'setGravity' (number expected, got no value)"

    But it was really helpful to my learnig process :D
  • For the y vector of gravity you just use same constant you used when creating the world:
    b2World:setGravity(self.fx*self.gravityScale, 15)
    but setting gravity is only one way, you could also experiment with applying impulses or setting linear velocity. Just make sure it won't go off your screen ;)

    Likes: victor_cappa

    +1 -1 (+1 / -0 )Share on Facebook
  • I'm always impressed about the patience that everybody in this forum have with newbies... I can't be more thankful ar2rsawseen!
Sign In or Register to comment.