Actionscript Sonic Engine

Discussion in 'Engineering & Reverse Engineering' started by Elratauru, Oct 26, 2010.

Thread Status:
Not open for further replies.
  1. Elratauru

    Elratauru

    Little Shiny Emurralds Member
    <div align='center'>[​IMG]</div>

    Yay! My first topic on Engineering & Reverse Engineering! :v:

    So, yeah, this is a proof of concept I did a while ago on Actionscript, using Mercury's lovely Sonic Physics Guide, and as you guys can see, this was made in Actionscript 2.0, using Flash CS5.

    I learned flash last year on my college as part of my web design career, and I always wanted to try to replicate the physics from the old and mighty classic games, so I gave it a try, this engine was written from the ground up using the SPG as my main reference. There are some bugs on it (most of them are visual) but the important thing is that it kinda works.

    Already implemented:
    - Walking
    - Running
    - Braking
    - Max Speed limited
    - Jumping
    - Variable Jumps
    - Rolling

    List of Bugs:
    - There seems to be a problem with the HitTest collision with the floor, nothing really important but we all know that flash is a bitch with it.
    - When braking you can release the button, hold it again, release it, and the animation will change, that's just visual. Its mainly because of the IF's I used to show the braking animation.

    For these that would like to edit the engine, or try it on their own flash, Sonic's AS file its here:
    <a href="http://www.mediafire.com/?05h4gi2ew8mt8n3" target="_blank">http://www.mediafire.com/?05h4gi2ew8mt8n3</a>

    I commented the whole AS file, Its divided on every action and its all controlled using If's and Else's along variables, remember to add a floor before trying it :P

    <span style="font-size:14pt;line-height:100%">So finally, here it is:<!--sizec--></span><!--/sizec-->
    <a href="http://alforata.deviantart.com/art/Classic-Sonic-Physics-183975969" target="_blank">http://alforata.deviantart.com/art/Classic...ysics-183975969</a>

    Controls: Left and Right to move, Up to Jump, Down to Roll.

    So...that's it! I know its nothing awesome but I'd like to hear your opinions about it people!
     
  2. Felik

    Felik

    Member
    1,706
    0
    16
    What's the point of it with no loop-de-loops?
     
  3. Lapper

    Lapper

    Member
    1,642
    378
    63
    England
    Sonic Studio, Sonic Physics Guide, Kyle & Lucy: WW, Freedom Planet 2
    Wow, flash Sonic games with good physics can actually happen now.

    Thank you, sir, thank you.

    My only complaint is that it seems either slow, or icy to walk...probably just me though.

    Any chance of slopes, I think the Physics Guide covers them enough.
     
  4. Elratauru

    Elratauru

    Little Shiny Emurralds Member
    <!--quoteo(post=522345:date=Oct 26 2010, 09:31 AM:name=Felik)--><div class='quotetop'>QUOTE (Felik @ Oct 26 2010, 09:31 AM) <a href="index.php?act=findpost&pid=522345">[​IMG]</a></div><div class='quotemain'><!--quotec-->What's the point of it with no loop-de-loops?<!--QuoteEnd--></div><!--QuoteEEnd-->

    ...Sigh, its a proof of concept of Sonic movement. I haven't touched any of the level parts yet (Slopes and shit).

    Sorry for trying to learn while I'm doing it.

    <!--quoteo(post=522346:date=Oct 26 2010, 09:32 AM:name=Sonica)--><div class='quotetop'>QUOTE (Sonica @ Oct 26 2010, 09:32 AM) <a href="index.php?act=findpost&pid=522346">[​IMG]</a></div><div class='quotemain'><!--quotec-->Wow, flash Sonic games with good physics can actually happen now.

    Thank you, sir, thank you.

    My only complaint is that it seems either slow, or icy to walk...probably just me though.

    Any chance of slopes, I think the Physics Guide covers them enough.<!--QuoteEnd--></div><!--QuoteEEnd-->

    Oh, yeah... Last time I saw that Sonic Flash game made by Sega I was like "FFFFF" :P

    About the Slow...or icy, it may be, I just used the values on Mercury's guide =P...But it may be wrong, since Flash "number" units may be different. I believe its using pixels right now so the accel is equals to 0.46875 pixels while on the floor, remember that when airborn, it multiplies by 2 so that's why It feels a little faster when jumping.

    Or...it may be the animation (I used a walk loop for it, so its not "that correct" compared to the real thing). That's just visual anyways =P

    Well, the slopes...Lets say that collision detection in flash is a pain in the ass, and I don't have a fucking clue aside from using HitTest (its kinda basic, for example: "If x object collides with another x object it does something"), and as you can see, the collision to the floor is kinda buggy that way.

    I'd love to find a way to do proper collision, but all the guides and help I found about it is on AS3.0 (but I haven't learned that yet, I'll do that next year in my career :v: )

    ----

    So yeah, I'd like to ask to any AS2.0 veteran, if they know any way to do a proper collision detection :v:
     
  5. Blue Blood

    Blue Blood

    Member
    5,173
    247
    43
    Neat stuff. I look forward to seeing how this goes. Right now though you need to fix Sonic's inability to jump while rolling.
     
  6. Elratauru

    Elratauru

    Little Shiny Emurralds Member
    <!--quoteo(post=522362:date=Oct 26 2010, 11:52 AM:name=Blue Blood)--><div class='quotetop'>QUOTE (Blue Blood @ Oct 26 2010, 11:52 AM) <a href="index.php?act=findpost&pid=522362">[​IMG]</a></div><div class='quotemain'><!--quotec-->Neat stuff. I look forward to seeing how this goes. Right now though you need to fix Sonic's inability to jump while rolling.<!--QuoteEnd--></div><!--QuoteEEnd-->

    Actually...I forgot to "unlock it", I locked the ability to test something and I forgot about it :v:
    I mean you can only jump if rolling = false. That's what locks it right now ^^

    Thanks for pointing that out!
     
  7. steveswede

    steveswede

    Member
    5,032
    1
    16
    Ask my hand
    Fighting against the Unitary State of Europe
    Nice work Elratauru. I hope you do push through with perfect or near perfect physics of the classics.
     
  8. Skeledroid

    Skeledroid

    Member
    227
    0
    0
    you could remove half the features and call it a sonic 4 engine

    ZING
     
  9. theocas

    theocas

    Tech Member
    346
    0
    16
    Very nice. I might take a look at it to figure out a way to make the collision work properly. If I can find my Flash Games Programming Boog for AS 2.0 that is.
     
  10. Spanner

    Spanner

    The Tool Member
    3,309
    113
    43
    United Kingdom
    Sonic Hacking Contest
    Good progress. Perhaps we'll get Flash-based fangames with decent controls now instead of the messy physics they usually have.
     
  11. Black Squirrel

    Black Squirrel

    furthermore known as the JAMs Wiki Sysop
    6,411
    619
    93
    Northumberland, UK
    wiki wild wild west
    Holding both left and right while at a decent speed will make you skid without slowdown. This plagued your earlier engine test too.
     
  12. Lapper

    Lapper

    Member
    1,642
    378
    63
    England
    Sonic Studio, Sonic Physics Guide, Kyle & Lucy: WW, Freedom Planet 2
    I have noticed, that there is no code for the dec...
    I have replicated the code from here: <a href="http://info.sonicretro.org/SPG:Running" target="_blank">http://info.sonicretro.org/SPG:Running</a> to AS, it includes the dec movement.
    Code (Text):
    1. &nbsp;&nbsp;&nbsp;&nbsp;if (Key.isDown(37))
    2. &nbsp;&nbsp;&nbsp;&nbsp;{
    3. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (xspeed > 0)
    4. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    5. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xspeed -= dec;
    6. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    7. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
    8. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (xspeed > -max)
    9. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    10. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xspeed = xspeed - acc;
    11. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (xspeed < -max)
    12. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    13. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xspeed = -max;
    14. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    15. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    16. &nbsp;&nbsp;&nbsp;&nbsp;}
    17. &nbsp;&nbsp;&nbsp;&nbsp;else
    18. &nbsp;&nbsp;&nbsp;&nbsp;if (Key.isDown(39))
    19. &nbsp;&nbsp;&nbsp;&nbsp;{
    20. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (xspeed < 0)
    21. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    22. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xspeed += dec;
    23. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    24. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
    25. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (xspeed < max)
    26. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    27. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xspeed = xspeed + acc;
    28. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (xspeed > max)
    29. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    30. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xspeed = max;
    31. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    32. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    33. &nbsp;&nbsp;&nbsp;&nbsp;}
    34. &nbsp;&nbsp;&nbsp;&nbsp;else if (xspeed < 0 && !Key.isDown())
    35. &nbsp;&nbsp;&nbsp;&nbsp;{
    36. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xspeed = xspeed + frc;
    37. &nbsp;&nbsp;&nbsp;&nbsp;}
    38. &nbsp;&nbsp;&nbsp;&nbsp;else if (xspeed > 0 && !Key.isDown())
    39. &nbsp;&nbsp;&nbsp;&nbsp;{
    40. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xspeed = xspeed - frc;
    41. &nbsp;&nbsp;&nbsp;&nbsp;}
    Takes up less space too (well... I think it does). With decceleration, it takes away any floaty/icy feel.

    It also seems to work if the variables are litterally the correct values like so:
    Code (Text):
    1. &nbsp;&nbsp;&nbsp;&nbsp;var acc = 0.046875;
    2. &nbsp;&nbsp;&nbsp;&nbsp;var dec = 0.5;
    3. &nbsp;&nbsp;&nbsp;&nbsp;var frc = 0.046875;
    4. &nbsp;&nbsp;&nbsp;&nbsp;var max = 6;
    5. &nbsp;&nbsp;&nbsp;&nbsp;var grav = 0.21875;
    Just trying to help!

    Oh, does air drag work, or is there no fancy calculations? I can't see any evidence if there is.
     
  13. Elratauru

    Elratauru

    Little Shiny Emurralds Member
    <!--quoteo(post=522384:date=Oct 26 2010, 02:03 PM:name=olaf)--><div class='quotetop'>QUOTE (olaf @ Oct 26 2010, 02:03 PM) <a href="index.php?act=findpost&pid=522384">[​IMG]</a></div><div class='quotemain'><!--quotec-->you could remove half the features and call it a sonic 4 engine

    ZING<!--QuoteEnd--></div><!--QuoteEEnd-->

    ZING! :v:

    But that would defeat the purpose of the thing :P


    <!--quoteo(post=522387:date=Oct 26 2010, 02:12 PM:name=Black Squirrel)--><div class='quotetop'>QUOTE (Black Squirrel @ Oct 26 2010, 02:12 PM) <a href="index.php?act=findpost&pid=522387">[​IMG]</a></div><div class='quotemain'><!--quotec-->Holding both left and right while at a decent speed will make you skid without slowdown. This plagued your earlier engine test too.<!--QuoteEnd--></div><!--QuoteEEnd-->

    Oh, I just have to add an if saying that you cant press left and right at the same time. I forgot about it! :v:

    Thanks for pointing that! :D

    <!--quoteo(post=522400:date=Oct 26 2010, 03:04 PM:name=Sonica)--><div class='quotetop'>QUOTE (Sonica @ Oct 26 2010, 03:04 PM) <a href="index.php?act=findpost&pid=522400">[​IMG]</a></div><div class='quotemain'><!--quotec-->I have noticed, that there is no code for the dec...
    I have replicated the code from here: <a href="http://info.sonicretro.org/SPG:Running" target="_blank">http://info.sonicretro.org/SPG:Running</a> to AS, it includes the dec movement.
    Code (Text):
    1. CODE[code]
    2. Takes up less space too (well... I think it does). With decceleration, it takes away any floaty/icy feel.
    3.  
    4. It also seems to work if the variables are litterally the correct values like so:
    5. [code]&nbsp;&nbsp;&nbsp;&nbsp;var acc = 0.046875;
    6. &nbsp;&nbsp;&nbsp;&nbsp;var dec = 0.5;
    7. &nbsp;&nbsp;&nbsp;&nbsp;var frc = 0.046875;
    8. &nbsp;&nbsp;&nbsp;&nbsp;var max = 6;
    9. &nbsp;&nbsp;&nbsp;&nbsp;var grav = 0.21875;
    Just trying to help!

    Oh, does air drag work, or is there no fancy calculations? I can't see any evidence if there is.<!--QuoteEnd--></div><!--QuoteEEnd-->

    Oh, how could I forget about it!...Ok

    You did quite a mess in the code back there, It may be shorter, but its all disorganized to me =P

    Code (Text):
    1. if ((xspeed > 0) && (rolling = false)) {
    2. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (Key.isDown(Key.LEFT)) {
    3. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xspeed -= dec;
    4. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    5. &nbsp;&nbsp;&nbsp;&nbsp;}
    6. if ((xspeed < 0) && (rolling = false)) {
    7. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (Key.isDown(Key.RIGHT)) {
    8. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xspeed += dec;
    9. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    10. &nbsp;&nbsp;&nbsp;&nbsp;}
    Im adding that right now that... (Its the same thing) but it doesnt wants to kick in. I don't know why... There may be something Im missing =P...Oh hell.

    Yeah, I havent added air drag, but Im trying to find a way to fix everything else :P

    Thanks for helping out by the way! ^^
     
  14. Lapper

    Lapper

    Member
    1,642
    378
    63
    England
    Sonic Studio, Sonic Physics Guide, Kyle & Lucy: WW, Freedom Planet 2
    I think air drag is this:
    Code (Text):
    1. if (yspeed < 0 and yspeed > -4)
    2. {
    3. &nbsp;&nbsp;&nbsp;&nbsp;xspeed = xspeed - ( ( xspeed / 0.125 ) / 256 );
    4. }
    Actually, does Actionscript have a 'div' equivilent? (I'm currently messing around with the engine because it's cool).
     
  15. Sintendo

    Sintendo

    Member
    249
    0
    16
    I noticed one issue that hasn't been addressed yet. When you tap the jump button, Sonic will raise slightly and remain there until you jump again.
     
  16. DigitalDuck

    DigitalDuck

    Arriving four years late. Member
    5,038
    195
    43
    Lincs, UK
    TurBoa, S1RL
    <!--quoteo(post=522451:date=Oct 26 2010, 08:18 PM:name=Sintendo)--><div class='quotetop'>QUOTE (Sintendo @ Oct 26 2010, 08:18 PM) <a href="index.php?act=findpost&pid=522451">[​IMG]</a></div><div class='quotemain'><!--quotec-->I noticed one issue that hasn't been addressed yet. When you tap the jump button, Sonic will raise slightly and remain there until you jump again.<!--QuoteEnd--></div><!--QuoteEEnd-->

    That's the aforementioned collision errors at work.
     
  17. Lapper

    Lapper

    Member
    1,642
    378
    63
    England
    Sonic Studio, Sonic Physics Guide, Kyle & Lucy: WW, Freedom Planet 2
    <!--quoteo(post=522451:date=Oct 26 2010, 07:18 PM:name=Sintendo)--><div class='quotetop'>QUOTE (Sintendo @ Oct 26 2010, 07:18 PM) <a href="index.php?act=findpost&pid=522451">[​IMG]</a></div><div class='quotemain'><!--quotec-->I noticed one issue that hasn't been addressed yet. When you tap the jump button, Sonic will raise slightly and remain there until you jump again.<!--QuoteEnd--></div><!--QuoteEEnd-->
    I don't get that. My jumping is fine.

    The only problem I get with collision is the fact that he land at different heights each time, some sort of (when not airborne, snap y to...) code is needed for the moment.
     
  18. Elratauru

    Elratauru

    Little Shiny Emurralds Member
    <!--quoteo(post=522444:date=Oct 26 2010, 05:07 PM:name=Sonica)--><div class='quotetop'>QUOTE (Sonica @ Oct 26 2010, 05:07 PM) <a href="index.php?act=findpost&pid=522444">[​IMG]</a></div><div class='quotemain'><!--quotec-->I think air drag is this:
    Code (Text):
    1. if (yspeed < 0 and yspeed > -4)
    2. {
    3. &nbsp;&nbsp;&nbsp;&nbsp;xspeed = xspeed - ( ( xspeed / 0.125 ) / 256 );
    4. }
    Actually, does Actionscript have a 'div' equivilent? (I'm currently messing around with the engine because it's cool).<!--QuoteEnd--></div><!--QuoteEEnd-->

    div? You mean, division?

    Well, it has a whole library of math functions and operators and things. Just open flash, press F9 and in the left part of the actionscript panel you'll find each library there.

    Or, you can check an Actionscript 2.0 reference guide online too :v:

    I'll try adding air drag later, Im in php class right now :D

    <!--quoteo(post=522451:date=Oct 26 2010, 05:18 PM:name=Sintendo)--><div class='quotetop'>QUOTE (Sintendo @ Oct 26 2010, 05:18 PM) <a href="index.php?act=findpost&pid=522451">[​IMG]</a></div><div class='quotemain'><!--quotec-->I noticed one issue that hasn't been addressed yet. When you tap the jump button, Sonic will raise slightly and remain there until you jump again.<!--QuoteEnd--></div><!--QuoteEEnd-->

    That's the work of the HitTest. I hate how buggy it is.

    <!--quoteo(post=522458:date=Oct 26 2010, 06:02 PM:name=Sonica)--><div class='quotetop'>QUOTE (Sonica @ Oct 26 2010, 06:02 PM) <a href="index.php?act=findpost&pid=522458">[​IMG]</a></div><div class='quotemain'><!--quotec--><!--quoteo(post=522451:date=Oct 26 2010, 07:18 PM:name=Sintendo)--><div class='quotetop'>QUOTE (Sintendo @ Oct 26 2010, 07:18 PM) <a href="index.php?act=findpost&pid=522451">[​IMG]</a></div><div class='quotemain'><!--quotec-->I noticed one issue that hasn't been addressed yet. When you tap the jump button, Sonic will raise slightly and remain there until you jump again.<!--QuoteEnd--></div><!--QuoteEEnd-->
    I don't get that. My jumping is fine.

    The only problem I get with collision is the fact that he land at different heights each time, some sort of (when not airborne, snap y to...) code is needed for the moment.
    <!--QuoteEnd--></div><!--QuoteEEnd-->

    Yeap. The collision really sucks. That's why I said we need to look for a different colission model, I know there are some other ways to do it, but I never tried them, since these are a little...harder :v:
     
  19. VB.NET

    VB.NET

    Member
    230
    0
    0
    United States
    Learning C++
    This might be intentional but if you hold the jump button down you can keep on jumping without Sonic coming out of a ball for the split second he lands on the ground.
     
  20. Blue Blood

    Blue Blood

    Member
    5,173
    247
    43
    Keeping a hold of down and left/right from a standstill makes Sonic snap very quickly back and forth between rolling and walking.
     
Thread Status:
Not open for further replies.