So uh I figure out how to make a joystick on monogame. But im just wondering if some people here code a joystick in a more simplistic way.
This is the code I got from a tutorial
mousex = mouseState.X;
mousey = mouseState.Y;
// the formula in here is slope of a line
dx = mousex - sprite.originalPosition.X;
dy = mousey - sprite.originalPosition.Y;
// the difference between atan and atan2 http://stackoverflow.com/questions/283406/what-is-the-difference-between-atan-and-atan2-in-c
angle = (float) Math.Atan(Math.Abs(dy/dx));
c = (float)Math.Sqrt(dx*dx + dy*dy); // pythogrean theorem
if (c > radius)
{
if (dx > 0 && dy > 0) // bottom right(1st quadrant)
{
mousex = (float)sprite.originalPosition.X + (radius * (float)Math.Cos(angle));
mousey = (float)sprite.originalPosition.Y + (radius * (float)Math.Sin(angle));
}else if (dx > 0 && dy < 0) // top right ( 2nd quadrant of circle)
{
mousex = (float)sprite.originalPosition.X + (radius * (float)Math.Cos(angle));
mousey = (float)sprite.originalPosition.Y - (radius * (float)Math.Sin(angle));
}else if (dx < 0 && dy < 0) // top left ( 3rd quadrant of circle)
{
mousex = (float)sprite.originalPosition.X - (radius * (float)Math.Cos(angle));
mousey = (float)sprite.originalPosition.Y - (radius * (float)Math.Sin(angle));
}else if (dx < 0 && dy > 0) // bottom left ( 4th quadrant of circle)
{
mousex = (float)sprite.originalPosition.X - (radius * (float)Math.Cos(angle));
mousey = (float)sprite.originalPosition.Y + (radius * (float)Math.Sin(angle));
}
Im just wondering if you guys implement the same functionality with minimal ifs. and if so how did you do it?
thanks.