How to make 8 Direction move at the angle of the gamepad joystick?

Not favoritedFavorited Favorited 0 favourites
  • 12 posts
From the Asset Store
Rotate & Animation for 16 Direction & Mouse Direction
  • I'm struggling to figure out how to move my character at the exact angle of the gamepad joystick using any of the movement options in C3. I'm currently using 8 Direction but it only simulates at up, down, left, or right. Which doesn't support joystick angles very well. I can force the joystick to only move in those cardinal directions but then the joystick feels weird and unresponsive.

    Is there a way to fix this? I've tried MoveTo and Custom movement but none of do all the things I want. I need the movement to handle acceleration, decelleration, and collision detection at a minimum. But the sliding of 8 Direction is a massive bonus.

    If no one can think of anything then please add a thumbs up to my suggestion:

    github.com/Scirra/Construct-feature-requests/issues/493, which is to add "Simulate at angle" to 8 Direction movement.

  • which is to add "Simulate at angle" to 8 Direction movement.

    You don't need it, because there are "Set vector X/Y" actions. You can set it directly to gamepad axis values:

    Player Set vector X to Gamepad.axis(0,0)
    Player Set vector Y to Gamepad.axis(0,1)
    

    Or calculate stick angle and distance:

    set a to angle(0,0, Gamepad.axis(0,0), Gamepad.axis(0,1))
    set d to distance(0,0, Gamepad.axis(0,0), Gamepad.axis(0,1))
    

    Then use these formulas to move the character:

    Set vector X to cos(a)*d
    Set vector Y to sin(a)*d
    
  • Be wary of the distance() from gamepad sticks, as it's returning values in a square-ish shape and not a circle. Meaning distance can be over 100 on diagonals.

    A simple fix would be just limiting distance i.e min(distance, 100)

  • Thanks for the reply dop2000, but that doesn't work well because that will ignore the acceleration and deceleration that 8 Direction handles for you. I would have to code my own accel/decel which is not ideal. I need it for my game cause some characters have tight movement but other characters are made of ice so need low accel/decel.

    I'm hoping for a built in C3 solution cause this is such a common thing in games that we shouldn't need to hack it in ourselves. Even Custom movement can't handle deceleration to a stop which seems like a big oversight.

  • You can use "Simulate control" action. It will only work for 8 directions though, as the behavior name suggests.

    Another option is to use vectors and handle acceleration/deceleration with events.

  • Right, and that's what I'm currently doing. But it feels very weird on joystick. If you make slight movements to the joystick then there's zero response to the character, which feels unresponsive. That's why we need "Simulate at angle" on 8 Direction so I can just pass in the joystick angle. That way I also get all the benefits of 8 Direction like accel/decel, collisions, and sliding.

    Also "Simulate at angle" already technically exists on Custom movement behavior, but it's called "Accelerate towards angle". That's exactly what I want on 8 Direction. BUT the problem with Custom movement behavior is that it doesn't do decelleration to a stop for you nor the sliding.

    So one or the other..... add "Simulate at angle" to 8 Direction, or add decelleration to stop and sliding to Custom movement. Otherwise sounds like I have to handle accel/decel myself.....

  • Well, the 8-direction behavior works by doing the horizontal and vertical axis of motion separately. When a key is pressed it accelerates toward that direction, and if the velocity is opposite that direction, then it adds the deceleration to the acceleration to allow sharper turning and make the motion less floaty. And finally, it applies deceleration if no keys are pressed on a given axis.

    Changing it to work with any angle is similar but different enough to not be a simple tweak.

    Here is one attempt. Acceleration can be applied to any angle. Deceleration is added to the acceleration when the velocity is in the opposite direction to allow faster turning. It also applies deceleration to velocity perpendicular to the acceleration. And finally, it applies deceleration when no acceleration is applied.

    dropbox.com/scl/fi/igrj1axj136hhuycmpp5w/360direction_movement.capx

    I don't have a joystick, so I used a touch screen one. Collision detection and response (wall sliding) is done with an SDF. Player is a circle, and obstacles are any number of rotated boxes.

    Seems to work alright.

    Now you could do a hybrid approach to reuse as much of the 8-direction behavior as possible. Read the velocity and apply an acceleration at an angle. One key idea to make it less floaty and turn faster is to add the deceleration to the acceleration. You can find the velocity along an angle with vx*cos(a)+vy*sin(a) where vx,vy is the velocity and "a" is the angle. It will be negative if "a" is in the opposite direction from the angle of motion. You may also need to decelerate the perpendicular velocity, but after that just set the velocity of the behavior and it will handle slowing down if there are no keys pressed, and it will handle the collision detection and response.

  • Thanks R0J0hound for the response and example. I converted it to work with the gamepad joystick instead of the virtual one and it worked perfect.

    I'm trying to study it a little bit. What does SDF mean? Is that a collision algorithm?

    I think this is complex enough, and also common enough gameplay-wise, that Construct 3 should support it. I know they hate when people say a feature is "easy", but I honestly think it would be easy. They already do it in Custom movement, specifically "Accelerate towards angle"!!!!!

    I worked with it today and came up with a hybrid solution. I still use 8 Direction behavior but simply handle the acceleration/decelleration controlling the VectorX and VectorY myself.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Glad it was somewhat useful. SDF stands for signed distance field, which basically lets you have a formula that gives the closest distance from a point to a shape. In the example the shape is a rotated box. It’s not exactly made for collision detection, but can be used for it. Specifically it’s simple enough to use to find a collision between a circle and a shape. Also it’s fairly simple to use to get the contact point and collision normal.

  • Here is my attempt:

    dropbox.com/scl/fi/vbfmptsy0qdcl823c9kaw/8Direction_Gamepad.c3p

    Sliding, acceleration, deceleration - all work. The speed also changes based on how far you tilt the stick.

  • Very interesting solution dop2000. It's much simpler than mine. Honestly I don't understand why it works. My understanding is if, for example, "simulate right" and "simulate down" at happening at the same time then the character should move down-right at a 45 deg angle. Not sure why your solution doesn't do that.

  • if, for example, "simulate right" and "simulate down" at happening at the same time then the character should move down-right at a 45 deg angle. Not sure why your solution doesn't do that.

    What do you mean? It works like that in my example.

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