Gamepad thumbstick to replace mouse aim

Get help using Construct 2

Post » Wed Dec 18, 2013 12:11 am

For my top down game (twin stick shooter with left stick movement, right stick aiming) I'm trying to replace the mouse with the right thumbstick but can't replicate the following:

On Event -->
Player: Set angle toward (Mouse.X(0), Mouse.Y(0))

On Start Layout -->
Mouse: Set cursor from sprite <Target reticule>

So the left stick controls the usual WASD movement, and the right stick needs to move the cursor around the screen so that the player angle follows the cursor (rather than rotate the player directly). So I need a way of mapping the gamepad axis to an X,Y position for the cursor.

Linked CAPX is based on the example in this thread:

http://www.scirra.com/forum/360-aiming-with-a-gamepad-using-the-right-stick_topic73154.html

CAPX: http://www.sendspace.com/file/52lruh



Any suggestions appreciated.
B
5
S
1
Posts: 44
Reputation: 665

Post » Wed Dec 18, 2013 2:08 am

>So I need a way of mapping the gamepad axis to an X,Y position for the cursor.

I'm not sure why you'd want this instead of the traditional twinstick style..

but anyway, I dont have a gamepad to test this with, but to my knowledge gamepad.axis() returns a value inbetween -1 and 1
so if you can correctly retrieve the width and height of the screen you can try this:

set TargetX to (Width*0.5) + (Gamepad.Axis(0,2)*width*0.5)
set TargetY to (Height*0.5) + (Gamepad.Axis(0,3)*height*0.5)

In place of Width/Height You could try using WindowWidth and WindowHeight, but to my experience they can get a little messed up depending on other settings. I work around it by just creating global variables called BaseWidth and BaseHeight, I set them to the same setting as what I have for 'window size' in the project properties. keepee2013-12-18 02:12:13
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

Post » Wed Dec 18, 2013 11:30 am

That makes sense but doesn't seem to work as it should, using Window Width/Height, global variables or hardcoded values. I seem to get huge X and Y values when the right stick is moved, so the cursor immediately goes off screen.

Updated CAPX: http://www.sendspace.com/file/uljgcd
B
5
S
1
Posts: 44
Reputation: 665

Post » Wed Dec 18, 2013 11:39 am

Why not just create a target reticule sprite and move it using the stick
and instead of setting angle towards mouse.x,mouse.y set angle towards the sprite?
LittleStain2013-12-18 11:39:47
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Wed Dec 18, 2013 11:44 am

@terence
my bad, i just checked the manual and find that gamepad.axis() returns a value between -100 and 100, not -1 and 1 like I assumed.. so of course you just need to divide it
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

Post » Wed Dec 18, 2013 5:39 pm

@LittleStain, that's my goal. The mouse.x/mouse/y movement is what I'm trying to replicate.

@keepee, dividing by 100 works in cases where my layout matches the window size, but I'm facing a few issues:

1. If my layout is much larger than the window size, the target stays within the top left of the layout and doesn't follow the player.

2. When the right stick is not being used the target defaults to a position just off center of the window and the player sprite snaps back to a 0 angle. I'd like the target to stay where the it was last left (just like a mouse cursor) and have the player stay pointing towards that angle.

3. The player angle isn't exactly aligned with the target. To set the target angle I'm using Set Angle To: angle(0,0,Gamepad.Axis(0,2),Gamepad.Axis(0,3))

Any suggestions on how to address these issues?
B
5
S
1
Posts: 44
Reputation: 665

Post » Wed Dec 18, 2013 6:11 pm

ah I see, i misunderstood what you were after.

to solve the first problem would have just been a case of using 'scroll'
set TargetX to scrollx + (Gamepad.Axis(0,2)*width*0.5)

but that's not really relevant now I understand what you want..

if you create some extra vars you can try this:

add Gamepad.Axis(0,2)*sensitivity to OffsetX
add Gamepad.Axis(0,3)*sensitivity to OffsetY

set TargetX to OffsetX + ScrollX
set TargetY to OffsetY + ScrollY

you can use clamp() to limit the Offset inbetween the view edges using the ViewportLeft/Right/Top/Bottom expressions.

if you wish to implement layout scaling or layout rotation to your game, you should create a UI layer with that does not scroll, scale or rotate, then there's no need to add on the ScrollX or ScrollY.

If you do that though, you then need to determine what the TargetX is on the actual gameplay layer, so you may need to use the CanvasToLayer() expression keepee2013-12-18 18:13:23
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

Post » Mon Dec 23, 2013 10:55 pm

@keepee, thanks that addresses points 1 and 3. Any suggestions on point 2? I can't think of any way to get the target to stay in position (and therefore the player angle) when the right stick is released.
B
5
S
1
Posts: 44
Reputation: 665

Post » Tue Dec 24, 2013 1:20 am

did you make sure the Offset vars are global/static?
if not, they will be returning to their initial value (probably 0) every tick
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

Post » Mon Dec 30, 2013 1:45 pm

Yes, have set them to global variables. The target reticule still moves back towards the center the moment the right stick is released, as I guess the global variables are set to 0 as soon as this happens?

When I try to set the player angle toward (Target.TargetX, Target.TargetY) after setting TargetX to OffsetX and TargetY to OffsetY every tick, the player doesn't rotate towards the reticule. The only way I can get this to work is if I use Set Angle To: angle(0,0,Gamepad.Axis(0,2),Gamepad.Axis(0,3)) but this snaps the player angle back to 0 on releasing the right stick.

Not using ScrollX/Y as UI is on a separate layer.
B
5
S
1
Posts: 44
Reputation: 665

Next

Return to How do I....?

Who is online

Users browsing this forum: beohs, brunopalermo, MadSpy and 10 guests