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

Get help using Construct 2

Post » Mon Sep 15, 2014 1:06 am

Message: noisetank can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
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 (http://coronalabs.com/blog/2013/09/24/t ... -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.
B
5
S
2
Posts: 37
Reputation: 699

Post » Fri Oct 03, 2014 9:00 pm

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.
B
8
S
2
Posts: 35
Reputation: 572

Post » Sat Oct 04, 2014 7:55 am

bump... this is a question i have too
B
23
S
6
G
3
Posts: 316
Reputation: 3,461

Post » Sun Oct 05, 2014 7:38 am

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.
B
8
S
2
Posts: 35
Reputation: 572

Post » Mon Oct 06, 2014 3:09 am

Had an error in my events list. I edited the above post to correct it.
B
8
S
2
Posts: 35
Reputation: 572

Post » Tue Nov 04, 2014 3:01 pm

Drakkith: Read your reply a little late, but it came in handy for my new project. Thank you so much!
B
5
S
2
Posts: 37
Reputation: 699

Post » Sun Nov 30, 2014 2:06 pm

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.
Bl4ckSh33p-Soft - Custom Software and Indie Games
B
58
S
18
G
6
Posts: 756
Reputation: 9,591

Post » Sat Dec 06, 2014 5:06 pm

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.
Bl4ckSh33p-Soft - Custom Software and Indie Games
B
58
S
18
G
6
Posts: 756
Reputation: 9,591

Post » Sun Dec 07, 2014 3:15 am

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).
B
5
S
2
Posts: 37
Reputation: 699

Post » Tue Oct 04, 2016 3:12 pm

@drakkith THANK YOU! ... not just for the code, but for the explanation... love when i get to solve a problem AND learn something!
B
28
S
6
G
1
Posts: 39
Reputation: 1,586


Return to How do I....?

Who is online

Users browsing this forum: Ryan Kocourek and 23 guests