Page 1 of 1

[TRICK] Sonic Styled Gradual Movement with Brake

PostPosted: Mon Jan 14, 2013 4:01 pm
by Bee-Ant
This thread is inspired by sonicforvergame's thread about gradual animation here: viewtopic.php?f=5&t=12417
As well as my final answer of that thread.
So here, let's start the development.

GRADUAL MOVEMENT WITH BRAKE ON SUDDEN TURN

PREPARATION
===========

1. Animation
Give the player the following animation set:
- PlayerStopLeft1 ~ 5 fps
- PlayerStopRight1 ~ 5 fps
- PlayerRunLeft1 ~ 10 fps
- PlayerRunLeft2 ~ 20 fps
- PlayerRunLeft3 ~ 30 fps
- PlayerRunRight1 ~ 10 fps
- PlayerRunRight2 ~ 20 fps
- PlayerRunRight3 ~ 30 fps
- PlayerBrakeLeft1 ~ 15 fps
- PlayerBrakeLeft2 ~ 15 fps
- PlayerBrakeLeft3 ~ 15 fps
- PlayerBrakeRight1 ~ 15 fps
- PlayerBrakeRight2 ~ 15 fps
- PlayerBrakeRight3 ~ 15 fps

Note: PlayerRunLeft1, PlayerRunLeft2, and PlayerRunLeft3 are just the same animation file but with different fps setting. The same with the other sets.

2. Variables
Make the following variables (integer) via Script Editor:
- direct
To delcare the player's direction. -1 for facing left, and 1 for facing right
- lastdirect
To record the last direct value. Used when the player make a sudden turn
Sudden turn in this case is when we press Left key while the player is running to the right or vice versa.
- run
To declare the player running state. 0 for stop, 1 for run, 2 for brake, 3 for slow down
- speed
To declare the player running speed
- turn
To declare the player turning state. 0 for not turning, and 1 for turning


CODE
====

1. Player > Create Actor > Script Editor:
Code: Select all
direct=lastdirect=1;

This is for the initial value. We set the player's direction to be facing right

2. Player > KeyDown > Left (repeat: Enable) > Script Editor:
Code: Select all
direct=-1; //face left
if(run>0&&direct!=lastdirect&&turn==0){run=2;turn=1;} //brake when turning
if(turn==0){run=1;lastdirect=direct;} //keep going if not turning
speed+=(turn==0); //increase the speed if not turning

The logic is we set the direct value to be -1, to indicate that the player "should be" facing left, but we pass that value to lastdirect only when the player is not turning. If the player is turning, we don't pass direct to lastdirect (because we want him to still facing to the left when turning to the right) and set turn to be 1 which will slow him down.

3. Player > KeyDown > Right (repeat: Enable) > Script Editor:
Code: Select all
direct=1; //face right
if(run>0&&direct!=lastdirect&&turn==0){run=2;turn=1;} //brake when turning
if(turn==0){run=1;lastdirect=direct;} //keep going if not turning
speed+=(turn==0); //increase the speed if not turning

This code is just the opposite counter for the number 2 code.

4. Player > Key Up > any key > Script Editor:
Code: Select all
run=3; //slow down when any key is released


5. Player > Draw Actor > Script Editor:
Code: Select all
char str[32];
char directText[3][8]={"Left","Nothing","Right"}; //Direction text of the animation
char stateText[4][8]={"Stop","Run","Brake","Run"}; //State text of the animation

speed-=(run==3||run==2||turn==1)*5; //slow down the speed when release the button or turning
speed=max(0,min(speed,59)); //limit the speed value
x+=speed/2*lastdirect; //set the x movement

sprintf(str,"Ize%s%s%i",stateText[run],directText[lastdirect+1],speed/20+1); //get the animation name
ChangeAnimation("Event Actor", str, NO_CHANGE); //change the animation

if(speed<=0){run=0;turn=0;} //stop, if speed is 0 or lower

Ize%s%s%i will return "IzeStopLeft1" if: run is 0, lastdirect is -1, speed is 0
Ize%s%s%i will return "IzeRunLeft1" if: run is 1, lastdirect is -1, speed is 19 or lower
Ize%s%s%i will return "IzeBrakeLeft1" if: run is 2, lastdirect is -1, speed is 19 or lower
Ize%s%s%i will return "IzeRunRight3" if: run is 1, lastdirect is 1, speed is 40 or more
etc...


If it's still hard to imagine, here I included the demo to check the method's final result. :)
I'm so sure you will understand eventually
GradualMovementByBeeAnt.zip
(14.51 KiB) Downloaded 372 times


And I hope there will be someone who will share another method with much simpler and better steps ;)

Re: [TRICK] Sonic Styled Gradual Movement with Brake

PostPosted: Tue Jan 15, 2013 11:37 pm
by Jagmaster
This is absolutely terrific! It feels just like the original Sonic. :D

Now, we just need to be able to control fps via script. That would be nice.



I can only suggest one "enhancement" for this demo.

Code: Select all
PlayMusic2("Guile's Theme", 1.000000, 0, HIGH_PRIORITY_MUSIC);

The music

Bee-Ant's demos are always epic, but Guile's theme makes everything epicer. :P

Thanks for sharing!
Respect ++