Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Emulating arcade game joystick movement with Joysticks n’ Sliders (or any slider)

  • Emulating arcade game joystick movement with Joysticks n’ Sliders (or any slider)

    Posted by Will Poillion on August 27, 2019 at 3:02 pm

    Greetings all!

    I’d like to hear what you guys think is the best way to what I’m trying to achieve. I have a simple pixel animation of a ship in 2d space in the style of an 80s arcade game, see gif below. What I’d like to do is emulate what the joystick on a real game would do. If I move it slightly to the side, it moves ~20px/sec in that direction. This speed would potentially increase to lets say ~100px/sec when at the farthest position in that direction.

    I know the obvious thing to do would be create another joystick that represented the exact position on the screen (ex. top left corner is the top left corner of the screen). Instead of speed it’s literally just the position of the ship. But the speed of movement would be a slave to the interpolation between positions and not consistent.

    I’d really like to know what you guys think the best way to do this would be.

    Please pardon the bad quality of the gif I threw it together for demonstration purposes.

    Thanks guys!

    Will

    Alex Printz replied 6 years, 8 months ago 2 Members · 2 Replies
  • 2 Replies
  • Alex Printz

    August 29, 2019 at 3:58 pm

    The expression would go on the ship’s position.

    for each frame you would need to calculate the ship’s position for all previous frames and add them together.

    1. start from frame 0
    2. grab the joystick’s position
    3. linear that to a pixel offset ratio(per frame)
    4. add that to the current value
    3. clap to your screen limits.

    Loop to next frame, until you get to current frame.

    Like this:

    //you should change these:
    buffer = [250,100]; //EDGE SCREEN BUFFER [X,Y]
    joystickRange = 200; //MAX ABSOLUTE RANGE OF JOYSTICK FROM CENTER
    joystickMaxRate = 250; //MAX PIXELS PER SECOND MOVE

    //you can change these if you want, but don't necessarly need to:
    shipPosition = [thisComp.width, thisComp.height]/2; //ship start's in center screen;
    compMin = buffer; //top-left corner;
    compMax = [thisComp.width, thisComp.height] - buffer; //bottom-right corner;

    for(f=0; f<=timeToFrames(time); f++){ //for each previous frame
    joystickMove = thisComp.layer("joystick").position.valueAtTime(framesToTime(f)); //get joystick amount;
    for(i=0;i<2;i++) joystickMove[i] = easeOut(Math.abs(joystickMove[i]),0,joystickRange,0,joystickMaxRate/(1/thisComp.frameDuration)) * (joystickMove[i]<0?-1:1); //convert joystick position to offset rate;
    shipPosition+= joystickMove; //move ship amount;
    for(i=0;i<2;i++) shipPosition[i] = clamp(shipPosition[i],compMin[i],compMax[i]); //clamp ship within screen;
    }

    shipPosition //final call

    Alex Printz
    Mograph Designer

  • Alex Printz

    August 29, 2019 at 4:00 pm

    looks like the ‘less than’ symbols in that code got messed up, might need to change that when you paste the code.

    Alex Printz
    Mograph Designer

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy