Actions

Difference between revisions of "SPG:Running"

From Sonic Retro

(Running Animation)
(Re-designed, easy access to variables used on the page)
Line 1: Line 1:
Notes:
+
''Notes:''
  
 
Research applies to all four of the [[Sega Mega Drive|Genesis/Mega Drive]] games, and [[Sonic CD]].
 
Research applies to all four of the [[Sega Mega Drive|Genesis/Mega Drive]] games, and [[Sonic CD]].
Line 5: Line 5:
 
The following only applies when [[Sonic]] is on flat, dry land with no special power-ups.  Curves, water physics, [[Super Sonic]], and [[Speed Shoes]] will be covered in separate guides.
 
The following only applies when [[Sonic]] is on flat, dry land with no special power-ups.  Curves, water physics, [[Super Sonic]], and [[Speed Shoes]] will be covered in separate guides.
  
==Acceleration==
+
==Variables==
  
Sonic accelerates at a speed of 0.046875 (''acc'').  When you first press {{right}} from a standstill, his X speed increases by ''acc'' every step.  When you first press {{left}} from a standstill, his X speed decreases by ''acc'' every step.
+
The following variables/constants will be referenced frequently in this section.
 +
   
 +
<asm>
 +
//variables
 +
xsp: the speed in which sonic is moving horizontally
 +
ysp: the speed in which sonic is moving vertically
 +
 +
//constants
 +
acc: 0.046875
 +
dec: 0.5
 +
frc: 0.046875 (same as acc)
 +
top: 6
 +
</asm>
  
==Deceleration==
+
==Movement Rules==
  
If Sonic is already moving when you press {{left}} or {{right}}, rather than at a standstill, the computer checks whether you are pressing the joypad in the direction he's already moving.  If so, ''acc'' is added to his X speed as normal.  However, if you are pressing in the opposite direction than he's already moving, the deceleration constant (''dec''), a value of 0.5, is added instead.  Thus Sonic can turn around quickly.  If no distinction is made between ''acc'' and ''dec'', Sonic takes too long to overcome his current velocity, frustrating the player.  A good engine must not make such a day one mistake.
+
===Acceleration===
  
One might think that if X speed happened to equal 0.1, and you pressed {{left}}, ''dec'' would be subtracted, resulting in an X speed value of -0.4.  Oddly, this is not the case in any of the Mega Drive games.  Instead, at any time an addition or subtraction of ''dec'' results in X speed changing sign, X speed is set to ''dec''.  For example, in the instance above, X speed would become -0.5.  The bizarre result of this is that you can press {{left}} for one step, and then press {{right}} (or vice versa), and start running faster than if you had just pressed {{right}} alone!  Now, the resulting speed is still lower than one pixel per step, so it isn't very noticeable, but nonetheless it is true.  You may not want to bother emulating this anomaly.
+
When you hold {{right}} from a standstill, his ''xsp'' increases by ''acc'' every stepWhen you hold {{left}} from a standstill, his xsp decreases by ''acc'' every step.
  
==Friction==
+
===Deceleration===
  
If you are not pressing {{left}} or {{right}}, friction (''frc'') kicks inThe value of ''frc'' is 0.046875 - the same, incidentally, as ''acc''.  In any step in which the game recieves no horizontal joypad input, ''frc'' times the sign of X speed is subtracted from X speed, unless absolute X speed is less than ''frc'', in which case X speed is simply set to zero.
+
If Sonic is already moving when you press {{left}} or {{right}}, rather than at a standstill, the computer checks whether you are pressing the joypad in the direction he's already movingIf so, ''acc'' is added to his ''xsp'' as normalHowever if you are pressing in the opposite direction than he's already moving, the deceleration constant (''dec'') is added instead.  Thus Sonic can turn around quickly.  If no distinction is made between ''acc'' and ''dec'', Sonic takes too long to overcome his current velocity, frustrating the player.  A good engine must not make such a day one mistake.
  
  {
+
One might think that if ''xsp'' happened to equal 0.1, and you pressed {{left}}, ''dec'' would be subtracted, resulting in an ''xsp'' value of -0.4. Oddly, this is not the case in any of the Mega Drive games.  Instead, at any time an addition or subtraction of ''dec'' results in xsp changing sign, ''xsp'' is set to ''dec''.  For example, in the instance above, ''xsp'' would become -0.5.  The bizarre result of this is that you can press {{left}} for one step, and then press {{right}} (or vice versa), and start running faster than if you had just pressed {{right}} alone!  Now, the resulting speed is still lower than one pixel per step, so it isn't very noticeable, but nonetheless it is true.  You may not want to bother emulating this anomaly.
  ; friction, only if player isn't pressing left or right
 
  X speed = X speed - minimum(absolute(X speed),frc) * sign(X speed)
 
  }
 
  
==Top Speed==
+
===Friction===
  
Sonic can only accelerate so long.  At some point, he reaches top speed and can no longer move any faster under his own power.  This maximum speed (''max'') is 6 pixels per stepSo, after ''acc'' is added to X speed, the computer checks to see if X speed exceeds ''max''.  If it does, it's set to ''max''.
+
If you are not pressing {{left}} or {{right}}, friction (''frc'') kicks inIn any step in which the game recieves no horizontal joypad input, ''frc'' times the sign of ''xsp'' is subtracted from ''xsp'', unless absolute ''xsp'' is less than ''frc'', in which case ''xsp'' is simply set to zero.
  
This means, of course, that if Sonic is somehow running at a higher speed than he can possibly achieve on his own (perhaps by way of having been impelled by a spring), if you press in the direction he's moving, the computer will add ''acc'', notice that X speed exceeds ''max'', and set X speed to ''max''.  Thus it is possible to curtail your forward momentum by pressing in the very direction of your motion.  That's just not right!  This can be solved in your engine by checking to see if X speed is less than ''max'' before adding ''acc''.  Only if X speed is less than ''max'' does the computer add ''acc'' to it and check if X speed exceeds ''max''.  Problem solved.
+
===Top Speed===
  
  {
+
Sonic can only accelerate up to a certain point.  At some point, he reaches top speed and can no longer move any faster under his own power. So, after ''acc'' is added to ''xsp'', the computer checks to see if ''xsp'' exceeds ''top''.  If it does, it's set to ''top''.
  if the player is pressing left
+
 
 +
This means, of course, that if Sonic is somehow running at a higher speed than he can possibly achieve on his own (perhaps by way of having been impelled by a spring), if you press in the direction he's moving, the computer will add ''acc'', notice that ''xsp'' exceeds ''top'', and set ''xsp'' to ''top''.  Thus it is possible to curtail your forward momentum by pressing in the very direction of your motion.  That's just not right!  This can be solved in your engine by checking to see if ''xsp'' is less than ''top'' before adding ''acc''.  Only if ''xsp'' is less than ''top'' does the computer add ''acc'' to it and check if ''xsp'' exceeds ''top''.  Problem solved.
 +
 +
[[Sonic CD]] actually uses a fix like this because Sonic can perform the "[[Super Peel Out]]" (or "Dash", in Japan, which is what I'll call it here), which launches him forward at a speed of 12 pixels per step.  Sonic can't accelerate to this speed under normal conditions, but if he reaches it from a Dash, he can continue to run at such a speed without slowing down, as long as you continue to press in the direction of his motion.  If you should release the button, friction will take over.  If you press again, friction will cease, and ''xsp'' will remain constant, but it will not rebuild to 12 without another Dash.
 +
 
 +
However, the programmers of Sonic CD neglected to apply the fix while Sonic is in the air, so if Sonic were to Dash off of a cliff, while you held in the direction of his motion, ''xsp'' would cut to 6 as he leaves the ground, regardless of how much higher it was at the time.  Again, that's just not right.
 +
 
 +
Here's some code logic that can accurately emulate movement and friction:
 +
 
 +
<asm>
 +
if (the player is pressing left)
 +
{
 +
    if (xsp > 0)
 
     {
 
     {
    if X speed > 0
+
        xsp -= dec;
      {
 
      X speed -= dec
 
      }
 
    else
 
    if X speed > -max
 
      {
 
      X speed = X speed - acc
 
      }
 
      else X speed = -max
 
 
     }
 
     }
  else
+
    else (if xsp > -top)
  if the player is pressing right
 
 
     {
 
     {
    if X speed < 0
+
        xsp = xsp-acc;
      {
 
      X speed += dec
 
      }
 
    else
 
    if X speed < max
 
      {
 
      X speed = X speed + acc
 
      }
 
      else X speed = max
 
 
     }
 
     }
  }
+
    else xsp = -top;
 
+
}
[[Sonic CD]] actually uses a fix like this because Sonic can perform the "[[Super Peel Out]]" (or "Dash", in Japan, which is what I'll call it here), which launches him forward at a speed of 12 pixels per step.  Sonic can't accelerate to this speed under normal conditions, but if he reaches it from a Dash, he can continue to run at such a speed without slowing down, as long as you continue to press in the direction of his motion.  If you should release the button, friction will take over.  If you press again, friction will cease, and X speed will remain constant, but it will not rebuild to 12 without another Dash.
+
else if (the player is pressing right)
 +
{
 +
    if (xsp < 0)
 +
    {
 +
        xsp += dec;
 +
    }
 +
    else if (xsp < top)
 +
    {
 +
        xsp = xsp+acc;
 +
    }
 +
    else xsp = top;
 +
}
 +
else xsp = xsp-minimum(absolute(xsp), frc)*sign(xsp);
 +
</asm>
  
However, the programmers of Sonic CD neglected to apply the fix while Sonic is in the air, so if Sonic were to Dash off of a cliff, while you held in the direction of his motion, X speed would cut to 6 as he leaves the ground, regardless of how much higher it was at the time.  Again, that's just not right.
+
==Animation Rules==
  
==Running Animation==
+
===Running Animation===
  
If you include Sonic CD in the mix, Sonic has 3 different running animations. He is depicted as standing still only if X speed is exactly zero. If he has any X speed whatsoever, he enters his walking animation, the frame advences in relation to his ground speed.  
+
If you include Sonic CD in the mix, Sonic has 3 different running animations. He is depicted as standing still only if ''xsp'' is exactly zero. If he has any ''xsp'' whatsoever, he enters his walking animation, the frame advences in relation to his ground speed.  
  
Once his X speed equals (or exceeds) 6, he enters his running animation, with the whirling feet. Once his X speed equals (or exceeds) 10, he enters his Dashing animation, with the figure-eight feet. Of course, the Dashing animation is only in Sonic CD.
+
Once his ''xsp'' equals (or exceeds) 6, he enters his running animation, with the whirling feet. Once his ''xsp'' equals (or exceeds) 10, he enters his Dashing animation, with the figure-eight feet. Of course, the Dashing animation is only in Sonic CD.
  
==Braking Animation==
+
===Braking Animation===
  
Sonic enters his braking animation when you turn around only if his absolute X speed is equal to or more than 4.5. In Sonic 1 and Sonic CD, he then stays in the braking animation until X speed reaches zero or changes sign. In the other 3 games, Sonic returns to his walking animation after the braking animation finishes displaying all of its frames.
+
Sonic enters his braking animation when you turn around only if his absolute ''xsp'' is equal to or more than 4.5. In Sonic 1 and Sonic CD, he then stays in the braking animation until ''xsp'' reaches zero or changes sign. In the other 3 games, Sonic returns to his walking animation after the braking animation finishes displaying all of its frames.
  
==Other Characters==
+
===Character Specific===
  
 
All the characters - [[Sonic]], [[Tails]], and [[Knuckles]] - have the same acceleration, deceleration, top speed, running and braking values.  They handle identically, with no difference at all besides their special moves and their sprites (and the annoying fact that Knuckles jumps lower than the other two).
 
All the characters - [[Sonic]], [[Tails]], and [[Knuckles]] - have the same acceleration, deceleration, top speed, running and braking values.  They handle identically, with no difference at all besides their special moves and their sprites (and the annoying fact that Knuckles jumps lower than the other two).
  
 
[[Category:Sonic Physics Guide|Running]]
 
[[Category:Sonic Physics Guide|Running]]

Revision as of 16:04, 28 August 2014

Notes:

Research applies to all four of the Genesis/Mega Drive games, and Sonic CD.

The following only applies when Sonic is on flat, dry land with no special power-ups. Curves, water physics, Super Sonic, and Speed Shoes will be covered in separate guides.

Variables

The following variables/constants will be referenced frequently in this section.

<asm> //variables xsp: the speed in which sonic is moving horizontally ysp: the speed in which sonic is moving vertically

//constants acc: 0.046875 dec: 0.5 frc: 0.046875 (same as acc) top: 6 </asm>

Movement Rules

Acceleration

When you hold Right from a standstill, his xsp increases by acc every step. When you hold Left from a standstill, his xsp decreases by acc every step.

Deceleration

If Sonic is already moving when you press Left or Right, rather than at a standstill, the computer checks whether you are pressing the joypad in the direction he's already moving. If so, acc is added to his xsp as normal. However if you are pressing in the opposite direction than he's already moving, the deceleration constant (dec) is added instead. Thus Sonic can turn around quickly. If no distinction is made between acc and dec, Sonic takes too long to overcome his current velocity, frustrating the player. A good engine must not make such a day one mistake.

One might think that if xsp happened to equal 0.1, and you pressed Left, dec would be subtracted, resulting in an xsp value of -0.4. Oddly, this is not the case in any of the Mega Drive games. Instead, at any time an addition or subtraction of dec results in xsp changing sign, xsp is set to dec. For example, in the instance above, xsp would become -0.5. The bizarre result of this is that you can press Left for one step, and then press Right (or vice versa), and start running faster than if you had just pressed Right alone! Now, the resulting speed is still lower than one pixel per step, so it isn't very noticeable, but nonetheless it is true. You may not want to bother emulating this anomaly.

Friction

If you are not pressing Left or Right, friction (frc) kicks in. In any step in which the game recieves no horizontal joypad input, frc times the sign of xsp is subtracted from xsp, unless absolute xsp is less than frc, in which case xsp is simply set to zero.

Top Speed

Sonic can only accelerate up to a certain point. At some point, he reaches top speed and can no longer move any faster under his own power. So, after acc is added to xsp, the computer checks to see if xsp exceeds top. If it does, it's set to top.

This means, of course, that if Sonic is somehow running at a higher speed than he can possibly achieve on his own (perhaps by way of having been impelled by a spring), if you press in the direction he's moving, the computer will add acc, notice that xsp exceeds top, and set xsp to top. Thus it is possible to curtail your forward momentum by pressing in the very direction of your motion. That's just not right! This can be solved in your engine by checking to see if xsp is less than top before adding acc. Only if xsp is less than top does the computer add acc to it and check if xsp exceeds top. Problem solved.

Sonic CD actually uses a fix like this because Sonic can perform the "Super Peel Out" (or "Dash", in Japan, which is what I'll call it here), which launches him forward at a speed of 12 pixels per step. Sonic can't accelerate to this speed under normal conditions, but if he reaches it from a Dash, he can continue to run at such a speed without slowing down, as long as you continue to press in the direction of his motion. If you should release the button, friction will take over. If you press again, friction will cease, and xsp will remain constant, but it will not rebuild to 12 without another Dash.

However, the programmers of Sonic CD neglected to apply the fix while Sonic is in the air, so if Sonic were to Dash off of a cliff, while you held in the direction of his motion, xsp would cut to 6 as he leaves the ground, regardless of how much higher it was at the time. Again, that's just not right.

Here's some code logic that can accurately emulate movement and friction:

<asm> if (the player is pressing left) {

   if (xsp > 0)
   {
       xsp -= dec;
   }
   else (if xsp > -top)
   {
       xsp = xsp-acc;
   }
   else xsp = -top;

} else if (the player is pressing right) {

   if (xsp < 0)
   {
       xsp += dec;
   }
   else if (xsp < top)
   {
       xsp = xsp+acc;
   }
   else xsp = top;

} else xsp = xsp-minimum(absolute(xsp), frc)*sign(xsp); </asm>

Animation Rules

Running Animation

If you include Sonic CD in the mix, Sonic has 3 different running animations. He is depicted as standing still only if xsp is exactly zero. If he has any xsp whatsoever, he enters his walking animation, the frame advences in relation to his ground speed.

Once his xsp equals (or exceeds) 6, he enters his running animation, with the whirling feet. Once his xsp equals (or exceeds) 10, he enters his Dashing animation, with the figure-eight feet. Of course, the Dashing animation is only in Sonic CD.

Braking Animation

Sonic enters his braking animation when you turn around only if his absolute xsp is equal to or more than 4.5. In Sonic 1 and Sonic CD, he then stays in the braking animation until xsp reaches zero or changes sign. In the other 3 games, Sonic returns to his walking animation after the braking animation finishes displaying all of its frames.

Character Specific

All the characters - Sonic, Tails, and Knuckles - have the same acceleration, deceleration, top speed, running and braking values. They handle identically, with no difference at all besides their special moves and their sprites (and the annoying fact that Knuckles jumps lower than the other two).