How do I create smooth 360 degree movement on a controller?

0 favourites
  • 10 posts
From the Asset Store
JPEG format 364x HD backgrounds in 2688 x 1536 Resolution
  • I'm currently trying to work out the basics of a twin stick shooter (Geometry Wars, for example) in Construct, but it feels clunky and unwieldy.

    Ideally, the way this would work is that pushing a direction on the controller's analog stick would change the angle of movement (or shoot) in the exact direction that the player is pushing toward. I've managed to set up the left stick to correspond to an 8-direction movement behavior, which is bearable, but using the same methods for shooting on the right stick is not. I've managed to rig it up with a second object pinned to the player object (position only) and set the angle with the same code I used to create player movement, but it feels terribly inaccurate when I'm trying to shoot at anything with only 8 directional aim. It's also frustrating trying to figure out just how far I need to nudge the stick to get it to "lock" to the right direction.

    My question is: How can I smooth out these controls? Rather than have 8-direction movements mapped to the sticks, is it possible to convert the X,Y axis into reporting a 360 degree angle? I've been searching for Construct-specific help on the topic, but came up empty. I did, however find this Corona tutorial ( ... -and-axis/) that seems like it would have put me on the right track...but trying to replicate it in Construct ended up being an hour wasted.

    Can anyone point me in the right direction on this? It seems like there's a way I could use some clever math to convert the axis into a 360 degree angle that would solve all my problems, but I'm neither clever nor good enough at math to understand how.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I'm getting help on this on another site, but since I don't have the rep to post URL's or send private messages I'm not sure what I can do. Also, the link in your post doesn't work since it was converted to plaintext and shortened.

    Try searching google for "physicsforums converting 2 axis input to an angle". My thread should be the first hit.

  • bump... this is a question i have too

  • Solved it. Create the following events:

    System: Every tick

    ----Gamepad: Gamepad 0 Right Analog X axis > 0

    --------Gamepad: Gamepad 0 Right Analog Y Axis < 0 : Player: Set Angle to 360+atan((Gamepad.Axis(0,3))/(Gamepad.Axis(0,2)))

    --------System: Else : Player: Set angle to atan((Gamepad.Axis(0,3))/(Gamepad.Axis(0,2)))

    ----Gamepad: Gamepad 0 Right Analog X Axis < 0

    --------Gamepad: Gamepad 0 Right Analog Y Axis Not Equal To 0 : Player: 180+atan((Gamepad.Axis(0,3))/(Gamepad.Axis(0,2)))

    Note that the parameters for Gamepad.Axis(A,B) may be different for you. These were for an XBOX One controller who's index was 0 (first controller connected), making the A value 0 (it would be 1 if it were the second of two controllers connected). The value B is 2 for the right stick's X-Axis and 3 for the right stick's Y axis. My controller is also set up so that the analog stick Y-Axis is a negative value when pressed up and positive when down. Your controller may be different. To check, I went to the controller settings in windows and could see the Y rotation bar increase as the stick was pushed down, and decreased as the stick was pressed up.

    For those interested in what this is actually doing:

    The atan((Gamepad.Axis(0,3))/(Gamepad.Axis(0,2))) expression performs an arctangent function on a Y and an X variable to get a degree value as follows: arctangent (Y/X), where Y is the position of the right analog stick along the Y axis and X is the position of the stick along the X axis. Because of how the arctangent function works, it is necessary to set this up into different events because different combinations of + or - stick positions along each axis can return the same value.

    Adding the result from the either 360 or 180 is necessary to get the angle we want since the arctangent function only returns a value between -90 and + 90 degrees. In the first event we add the arctangent result to 360 because performing the function on any of the possible combinations of X and Y values for this event will always equal between -90 and 0 degrees. For example, if the stick position is 50,-50, diagonally right and up, the function arctangent (-50/50) gives us -45 degrees. Adding 360 to -45 gives us 315 degrees, which makes the player face up and right, just like the stick.

    You can do the math for the other events if you want to.

    Edit: Corrected a mistake in the events list. Turns out I accidentally set one of my conditions to check the Left Analog Y-Axis instead of the Right Analog Y-Axis. I was able to merge two events into one, reducing the number of events required.

  • Had an error in my events list. I edited the above post to correct it.

  • Drakkith: Read your reply a little late, but it came in handy for my new project. Thank you so much!

  • Nice, but does anyone know a good solution to keep the player from moving instantly from left to right or up to down if you change directions on the controller? It should rotate towards the pushed direction and not just jump into the direction immediately.

  • Any other ideas? I tried some things like :

    Gamepad 0 left analog x axis > 25 | Player rotate 3 degrees toward Angle 0

    -> System Player.Angle is between 330 and 30 degrees | Player Simulate 8Direction pressing Right for all directions but this does not seem to work as solution.

  • I use a separate object and pin it to the player, which works for my needs. The "firing" object, which is invisible can free rotate while the player moves independently on the other stick (which I just use 4 directions mapped to the 4 axis, like Gamepad 0 Left analog X axis < -25 | Simulate control left).

  • Drakkith THANK YOU! ... not just for the code, but for the explanation... love when i get to solve a problem AND learn something!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)