AlfonsAberg


Is there a way to get the angle from the thumbsticks of a XBOX360 controller

If up return 0, right 90, down 180, left 270.

this is how I crudely did it:

if (currentGamePadState.ThumbSticks.Right.X != 0 || currentGamePadState.ThumbSticks.Right.Y != 0)
{
if (projectileTime == 0)
{
Vector2 vel = new Vector2(400, 0);
float rad = 0;

if (currentGamePadState.ThumbSticks.Right.Y == 0)
{
if (currentGamePadState.ThumbSticks.Right.X > 0)
{
vel = new Vector2(400, 0);
}
else
{
vel = new Vector2(-400, 0);
}
}
else if (currentGamePadState.ThumbSticks.Right.X == 0)
{
if (currentGamePadState.ThumbSticks.Right.Y > 0)
{
vel = new Vector2(0, -400);
}
else
{
vel = new Vector2(0, 400);
}
}
else
{
if (currentGamePadState.ThumbSticks.Right.X > 0)
{
rad = (float)Math.Atan((double)currentGamePadState.ThumbSticks.Right.Y / currentGamePadState.ThumbSticks.Right.X);
vel = new Vector2((float)Math.Cos(rad) * 400, (float)Math.Sin(rad) * -400);
}
else
{
rad = (float)Math.Atan((double)currentGamePadState.ThumbSticks.Right.Y / currentGamePadState.ThumbSticks.Right.X);
vel = new Vector2((float)Math.Cos(rad) * -400, (float)Math.Sin(rad) * 400);
}
}

ProjectileManager.AddProjectile(Game1.Players[0].playerSprite.Position, vel, Color.Red);
projectileTime = 200;


}


Re: How to get Angle from Thumbstick?

waruwaru


Check out the Jan 3 entry "Rotating a sprite using an Xbox 360 ThumbStick" on

http://xnagamer.spaces.live.com/






Re: How to get Angle from Thumbstick?

AlfonsAberg

Thanks alot!

this is how my code looks like now:

if (currentGamePadState.ThumbSticks.Right.X != 0 || currentGamePadState.ThumbSticks.Right.Y != 0)
{
if (projectileTime == 0)
{
//float tan = XInputHelper.GamePads[PlayerIndex.One].ThumbStickRightY / XInputHelper.GamePads[PlayerIndex.One].ThumbStickRightX;

float rad = UpdateThumbStickAngle(currentGamePadState.ThumbSticks.Right);
Vector2 vel = new Vector2((float)Math.Sin(rad) * 400, (float)Math.Cos(rad) * -400);


ProjectileManager.AddProjectile(Game1.Players[0].playerSprite.Position, vel, Color.Red);
projectileTime = 200;


}
}

and


public float UpdateThumbStickAngle(Vector2 stick)
{
float angle = 0.0f;
stick.Normalize();
if ((stick.X >= 0 || stick.X <= 0) || (stick.Y >= 0 || stick.Y <= 0))
{
angle = (float)Math.Acos(stick.Y);
if (stick.X < 0.0f)
angle = -angle;
}
return angle;
}






Re: How to get Angle from Thumbstick?

waruwaru

NP, but you forgot to tell us if it works now :) The code certainly looks simpler... :)




Re: How to get Angle from Thumbstick?

Brian Bening

Calculating the angle of the thumbstick using a single axis can be imprecise.

Use Math.Atan2 instead. It uses both axes to calculate an angle:
float angle = (float) Math.Atan2(stick.Y, stick.X);


Although, for the code snippet you have listed, the angle is not needed at all. The following code could be used instead:

if ( currentGamePadState.ThumbSticks.Right.X != 0 || currentGamePadState.ThumbSticks.Right.Y != 0 )
{
if ( projectileTime == 0 )
{
Vector2 vel = currentGamePadState.ThumbSticks.Right;
vel.Normalize();
vel.X *= 400f;
vel.Y *= -400f;

ProjectileManager.AddProjectile( Game1.Players[0].playerSprite.Position, vel, Color.Red );
projectileTime = 200;
}
}




Re: How to get Angle from Thumbstick?

AlfonsAberg

Thanks Brian! Your tips works great, and are easy too.