How make object move when tilting iPhone accelerometer
by Neng Thao · in iTorque 2D · 02/20/2009 (4:51 pm) · 9 replies
Hi, my first Post.
How do I make my player/object move right when I tilt the iPhone to the right and left vice versus?
How do I make my player/object move right when I tilt the iPhone to the right and left vice versus?
#2
02/22/2009 (12:52 pm)
Thanks Mat, you definetly got me in the right direction. I'm in testing mode so everything I do will be simple. I tested it with a joystick on my PC and got it to work. But when i ported over to my mac and put it on the iphone, i didn't get the desired results.
#3
02/23/2009 (12:51 pm)
Where is the link to "BehaviorShooter" tutorial, I'm trying to search for it with no luck. Is BehaviorShooter just the regular shooter tutorial or something different?
#4
02/23/2009 (12:56 pm)
It is part of the iTGB distribution. See in the games folder.
#5
04/15/2009 (1:09 pm)
Did anyone ever figure this out? Neng?
#6
Tested in landscape mode:
X Axis = twisting iPhone Left or Right like a steering wheel. (-1 far left, +1 far right);
Y Axis = tilt forward and backwards (tilt back goes towards -1, tilt forward goes towards +1)
Z Axis = lifting up and down quickly (flat on table screen up -1, screen down +1)
imagine having your iphone flat on table screen up landscape, X = 0, Y = 0, and Z = -1; or very close to it.
now lift to make stand on left edge = -1x, stand on right edge +1x val
tilt forward so screen is away from you= +1y, tilt back so screen face you = -1y val
the %val will always have a max of -1 or +1, so you have to figure out your threshold in-between.
Enough acceleration will create artificial gravity. = G-Force.
heres a little sample code I used, hope it helps everyone.
//************************************************************
function startGame(%level)
{
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
// binds axis's to joystick0(accelerometer)
new ActionMap(moveMap);
moveMap.bind(joystick0, xaxis, "joystickMoveX" );
moveMap.bind(joystick0, yaxis, "joystickMoveY" );
moveMap.bind(joystick0, Zaxis, "joystickMoveZ" );
moveMap.push();
$enableDirectInput = true;
activateDirectInput();
enableJoystick();
sceneWindow2D.loadLevel(%level);
}
//---------------------------------------------------------------------------------------------
// endGame
// Game cleanup should be done here.
//---------------------------------------------------------------------------------------------
function endGame()
{
sceneWindow2D.endLevel();
moveMap.pop();
moveMap.delete();
}
function theObject::onLevelLoaded(%this, $sceneGraph)
{
%this.setUsesPhysics(true); // do not forget this
$myObject = %this;
}
// *********** contols how sensitive threshold *******************
// for xaxis -1 is max to Left yaxis 1 is max to right
// same concept for yaxis and zaxis (-1 max oneway +1max otherway)
$joyTwistLeft = -0.2; // controls how sensitive threshold left
$joyTwistRight = 0.2; // controls how sensitve threshold right
$joyLeanForward = 0.2; // controls sensitve thres forward
$joyLeanBack = -0.2; // controls sensitve thres back
// obviously you can save more memory by using one $variable
// but it's for explaining purposes only.
//***************************************************************
// ***** twist iPhone left or right like steering wheel******
function joystickMoveX(%val)
{
xValue.text = %val; //create a text Object to see value for yourself
if(%val < $joyTwistLeft)
{
$myObject.setLinearVelocityX(-20);
}
else if(%val >$joyTwistRight)
{
$myObject.setLinearVelocityX(20);
}
else
{
$myObject.setLinearVelocityX(0);
}
}
// ***** lean iPhone forward or Backwards *********
function joystickMoveY(%val)
{
yValue.text = %val;
if(%val > $joyLeanForward)
{
$myObject.setLinearVelocityY(-20);
}
else if(%val < $joyLeanBack)
{
$myObject.setLinearVelocityY(20);
}
else
{
$myObject.setLinearVelocityY(0);
}
}
// ***** bounce iPhone up and down ( lift up & down quickly)*********
//
// you get the point, use it for anything, use your imagination
function joystickMoveZ(%val)
{
zValue.text = %val;
}
04/18/2009 (1:02 am)
Accelerometer does work.Tested in landscape mode:
X Axis = twisting iPhone Left or Right like a steering wheel. (-1 far left, +1 far right);
Y Axis = tilt forward and backwards (tilt back goes towards -1, tilt forward goes towards +1)
Z Axis = lifting up and down quickly (flat on table screen up -1, screen down +1)
imagine having your iphone flat on table screen up landscape, X = 0, Y = 0, and Z = -1; or very close to it.
now lift to make stand on left edge = -1x, stand on right edge +1x val
tilt forward so screen is away from you= +1y, tilt back so screen face you = -1y val
the %val will always have a max of -1 or +1, so you have to figure out your threshold in-between.
Enough acceleration will create artificial gravity. = G-Force.
heres a little sample code I used, hope it helps everyone.
//************************************************************
function startGame(%level)
{
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
// binds axis's to joystick0(accelerometer)
new ActionMap(moveMap);
moveMap.bind(joystick0, xaxis, "joystickMoveX" );
moveMap.bind(joystick0, yaxis, "joystickMoveY" );
moveMap.bind(joystick0, Zaxis, "joystickMoveZ" );
moveMap.push();
$enableDirectInput = true;
activateDirectInput();
enableJoystick();
sceneWindow2D.loadLevel(%level);
}
//---------------------------------------------------------------------------------------------
// endGame
// Game cleanup should be done here.
//---------------------------------------------------------------------------------------------
function endGame()
{
sceneWindow2D.endLevel();
moveMap.pop();
moveMap.delete();
}
function theObject::onLevelLoaded(%this, $sceneGraph)
{
%this.setUsesPhysics(true); // do not forget this
$myObject = %this;
}
// *********** contols how sensitive threshold *******************
// for xaxis -1 is max to Left yaxis 1 is max to right
// same concept for yaxis and zaxis (-1 max oneway +1max otherway)
$joyTwistLeft = -0.2; // controls how sensitive threshold left
$joyTwistRight = 0.2; // controls how sensitve threshold right
$joyLeanForward = 0.2; // controls sensitve thres forward
$joyLeanBack = -0.2; // controls sensitve thres back
// obviously you can save more memory by using one $variable
// but it's for explaining purposes only.
//***************************************************************
// ***** twist iPhone left or right like steering wheel******
function joystickMoveX(%val)
{
xValue.text = %val; //create a text Object to see value for yourself
if(%val < $joyTwistLeft)
{
$myObject.setLinearVelocityX(-20);
}
else if(%val >$joyTwistRight)
{
$myObject.setLinearVelocityX(20);
}
else
{
$myObject.setLinearVelocityX(0);
}
}
// ***** lean iPhone forward or Backwards *********
function joystickMoveY(%val)
{
yValue.text = %val;
if(%val > $joyLeanForward)
{
$myObject.setLinearVelocityY(-20);
}
else if(%val < $joyLeanBack)
{
$myObject.setLinearVelocityY(20);
}
else
{
$myObject.setLinearVelocityY(0);
}
}
// ***** bounce iPhone up and down ( lift up & down quickly)*********
//
// you get the point, use it for anything, use your imagination
function joystickMoveZ(%val)
{
zValue.text = %val;
}
#7
This was a really useful resource. It got me going.
Does anyone know how to calibrate the accelerometers to ensure that the player does not move anywhere when the phone is held at a particular angle. I have added offset values to the joyLean* values and this works, but I reckon we need to calibrate the angle of the accelerometers to give us zero movement.
Any ideas would be great.
06/05/2009 (6:51 pm)
Thanks guysThis was a really useful resource. It got me going.
Does anyone know how to calibrate the accelerometers to ensure that the player does not move anywhere when the phone is held at a particular angle. I have added offset values to the joyLean* values and this works, but I reckon we need to calibrate the angle of the accelerometers to give us zero movement.
Any ideas would be great.
#8
06/06/2009 (6:57 am)
Unless you have super steady hands, everytime you tilt the iPhone/iPod in any direction, it's gonna effect all the other AXIS's. Not sure exactly at what point you would like to have your player go ZERO, but with the info you gave me, all I can say is it's in the coding. Fun stuff.
#9
06/07/2009 (10:12 am)
Luma, the company behind the awesome Marble Blast Mobile project, are contributing a lot of code they fixed and came up with for their game. I'll look into how they performed their play time calibration for MBM, since it was important that the player be able to control the accelerometer response.
Torque 3D Owner Mat Valadez
Default Studio Name
Set $iPhoneControls = "Accelerometer" instead of "Touch" to play BehaviorShooter with the controls you're asking about.