Multi-Resolution Game Development With GameBuilder Studio Check it out!
NEW Version 0.9.7 has arrived! New Blazing Fast Particle Engine & More Game Actions. Check it out
How do I create a rolling ball Accelerometer based game?
  • Hi,
      I'm planning to create a simple Accelerometer based demo game wherein a ball is controlled as the player tilts the device in various directions. 0.9.3 version of GBS has a support for Accelerometer but I am not able to figure out how to use it and there are not tutorials available for this. Can someone point me in the right direction on how to accomplish this.

  • 15 Comments sorted by
  • Hi @Varinder it should be very simple. Just use an expression to constrain the balls x and y values by the Accelerometer x and y values. Something like this:

    Self.BasicSpatial.x - ( Game.Accelerometer.x * Self.Properties.ballSpeed)


    Self.BasicSpatial.y + ( Game.Accelerometer.y * Self.Properties.ballSpeed)

    Use a constrain property action to constantly have these values updated. Properties.ballSpeed would have to be created on the same entity of course with the property ballSpeed set on it or you could just place a value directly instead of referencing one. 

    If you want to determine the direction angle of the tilt to point an object in that direction you could use the following expression

    acos( Game.Accelerometer.x / ( sqrt(pow(Game.Accelerometer.x,2) + pow(Game.Accelerometer.y,2)) ) ) * 180/PI 

    GameBuilder Studio is built on top of the AS3 language so a majority of calculations that you may need can be found by doing a quick google search on the topic and you can take the concepts and implement them inside the editor.

    EDITED: 10 / 22 / 2014
  • Thanks Lavon,
      This should help immensly..

  • hi Lavon and Varinder. Can you explain a little further? I mean, we should put the constraint into a rules map and run it on level load? or how exactly
  • @kikubu yes you would use a constraint action for both x and y coordinates. Constraint actions are run every frame unless you have it under a condition that evaluates to false. So put it on a rules component without any conditions and it will continuously run.
  • Thanks Lavon though I´m having some trouble getting it right.
    what I´ve done is:
    1 Put a data container on an entity, and create a property called ballSpeed (with a value of 1) 
    2 Put a rules map on the same entity, and trigger a constrain property (type expression) without asigning any condition to it. 
    3 expression looks like this:
    @BasicSpatial.x = BasicSpatial.x - ( Game.Accelerometer.x * Properties.ballSpeed) 

    when I run it, nothing moves as I move my surface pro which has accelerometer

    what am I doing wrong?
  • @kikubu your expression can not have an equal sign in it. Only the:

    BasicSpatial.x - ( Game.Accelerometer.x * Properties.ballSpeed)

    part of it needs to be in the expression field. If you want to set it to the x property of the spatial then put the reference to the x property in the property reference input of the action.
  • Thanks Lavon. My expression doesn't have an equal sign. I just wrote it all in one line. what I really have on the constraint property is: 

    Constraint Property [@BasicSpatial.x] = Type |Expression| [BasicSpatial.x - ( Game.Accelerometer.x * Properties.ballSpeed) ] 

    where [ ] means input field and | | means drop down list. I wrote it like this so I hadn't to upload an image.

    So in the first input field I have:

    in the dropdown list I've selected Type Expression 

    And in the second input field I have:
    BasicSpatial.x - ( Game.Accelerometer.x * Properties.ballSpeed)

    I hope I have explained myself good enough for you to understand me.
  • @kikubu ok I see, well the expression you wrote:

    BasicSpatial.x - ( Game.Accelerometer.x * Properties.ballSpeed) 

    should be:

    Self.BasicSpatial.x - ( Game.Accelerometer.x * Self.Properties.ballSpeed)

    When referencing a component on the same entity you need to have the "Self." keyword in front of the component name for expressions. If you want to reference an entity directly then you need to use the "Entity." keyword. For property references the syntax is "@" for components and "#" for entity references.
  • I've tried but got no results, maybe somebody has a working sample that I could take a look to, or check out my file, I would be very grateful:

    the entity where I put the constraint is the tree
  • @kikubu It looks like in your project you are trying to change the Accelerometer value. This can not be done. The "Globals" and "Game" property reference addresses in a property reference or an expression are only read only values they can not be set/changed. You must use the Accelerometer changes in value to calculate the direction your game object should move. So the ToProperty field in your constrain property action should be the x value of a spatial and the value property should be an expression with the below equation in it.:

    Self.BasicSpatial.x - ( Game.Accelerometer.x * 50)

    This will subtract the last Accelerometer value change on the X axis from the current X value of the spatial which should move the object.
  • Hi again Lavon! after several tests and tries, we've been unable to make the accelerometer work on a Surface Pro and a Surface Pro 3. Is it possible that the current method for accessing the x, y and z axis values don't work for Microsoft's devices?
  • @kikubu we haven't been able to test on a surface tablet but if you can access the Game.Accelerometer.x property it should be supported. If that property is not available at runtime then the Accelerometer support is not there.
  • Ok, I know what is happening, I published and installed a test project on my phone and the accelerometer works, the problem seems to be in the flash player, when I test on my surface, it cant read te accelerometer data. I have tested online java accelerometer enabled web aplications and they work perfectly on my surface.
    It will be cumbersome to develop accelerometer enabled apps if we have to install the app each time we want to try or fine tune something. Is it a limitation on the flash player?
  • @kikubu totally understand but this is a device based feature similar to ads that can only be tested on device. See

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion