ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Emulating arcade game joystick movement with Joysticks n' Sliders (or any slider)

FAQ   •   VIEW ALL
 Emulating arcade game joystick movement with Joysticks n' Sliders (or any slider) on Aug 27, 2019 at 3:02:39 pmLast Edited By Will Poillion on Aug 28, 2019 at 2:44:28 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

`<img src="https://i1.creativecow.net/u/332431/spaceship.gif" border="0" />`

 Re: Emulating arcade game joystick movement with Joysticks n' Sliders (or any slider)on Aug 29, 2019 at 3:58:03 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&lt;=timeToFrames(time); f++){ //for each previous frame joystickMove = thisComp.layer("joystick").position.valueAtTime(framesToTime(f)); //get joystick amount; for(i=0;i&lt;2;i++) joystickMove[i] = easeOut(Math.abs(joystickMove[i]),0,joystickRange,0,joystickMaxRate/(1/thisComp.frameDuration)) * (joystickMove[i]&lt;0?-1:1); //convert joystick position to offset rate; shipPosition+= joystickMove; //move ship amount; for(i=0;i&lt;2;i++) shipPosition[i] = clamp(shipPosition[i],compMin[i],compMax[i]); //clamp ship within screen; } shipPosition //final call```

Alex Printz
Mograph Designer