Collisions, Jumping and Gravity | Create an Amazing Game with Flutter & Flame

Поділитися
Вставка
  • Опубліковано 2 лип 2024
  • 📺 Checkout the playlist - • Create an Amazing Game...
    📂 Checkout the GitHub project - github.com/Spellthorn/pixel_a...
    🖼️ Game Assets - pixelfrog-assets.itch.io/pixe...
    💻 Tiled - www.mapeditor.org/
    🕒 Timestamps
    0:00 Episode 4 Introduction
    0:12 Refactoring Current Code
    9:53 Updating Tiled Level
    12:33 Grabbing Collisions from Tiled Level
    15:29 Creating Collision Block
    18:08 Adding Collisions to game
    24:00 Checking Horizontal Collisions
    25:50 Creating Collision Detection
    32:02 Updating Horizontal Collisions
    37:55 Adding Gravity
    42:30 Checking Vertical Collisions
    46:43 Adding Jumping
    50:12 Adding Platform Collisions
    54:34 Updating Player State for Falling and Jumping
    57:30 Adding Player Hitbox
    1:06:05 Finished Product
    #flutter #flame #gamedev
    Welcome to episode 4 of our thrilling game development tutorial series, "Collisions, Gravity, and Player Jumping | Create an Amazing Game with Flutter & Flame"! In this action-packed installment, we're delving deeper into the exciting world of game mechanics, taking your game to new heights with Flutter and Flame.
    First up, we tackle the crucial concept of collision detection. You'll learn how to implement collision detection for your player character and other game objects, ensuring they interact seamlessly with the game environment. Watch as we guide you through the process of setting up collision boundaries, enabling your character to navigate through the game world while interacting with platforms, obstacles, and other in-game elements.
    Next, brace yourself for an exhilarating adventure into the realm of gravity. Learn how to apply realistic gravity effects to your player character, making them feel grounded in the virtual world you're creating. With our expert guidance, you'll have your character moving and falling with natural fluidity, creating a sense of immersion that draws players deeper into your game.
    But that's not all-we're just getting started! Get ready to witness the magic of player jumping. You'll discover the mechanics behind adding jumping functionality to your character, allowing them to leap over obstacles, reach new heights, and explore the game world in a whole new way. Our step-by-step instructions and hands-on examples will make sure you grasp this fundamental gameplay element effortlessly.
    As we progress through the video, you'll gain valuable insights into fine-tuning your character's movements and interactions. We'll explore how to control jump heights, implement double jumps, and create dynamic gameplay experiences that keep players engaged and entertained.
    Throughout the tutorial, we provide clear explanations and practical demonstrations, making it easy for you to apply these concepts to your own game development projects. You'll also learn about performance optimization, ensuring your game runs smoothly and efficiently on various devices.
    By the end of this episode, you'll have a fully functional and dynamic game character that can navigate the game world with ease, overcome obstacles, and interact with the environment in exciting ways. You'll be well-versed in collision detection, gravity effects, and player jumping mechanics, armed with the knowledge to create captivating games that leave players wanting more.
    If you're as excited as we are about these new gameplay mechanics, be sure to show your support by liking this video and subscribing to our channel. We have plenty more thrilling tutorials lined up for game development with Flutter and Flame, and we want you to be part of the action. If you have any questions or ideas, share them in the comments below; we value your input!
    Stay tuned for the next video, where we'll dive into level design and start building the world your character will explore. Prepare to unleash your creativity and take your game to the next level. Happy coding, and let's continue our journey to create amazing games together!
    🛡️Join this channel to get access to perks: www.youtube.com/@Spellthorn/join
    Help me reach my sub goal by subscribing youtube.com/@Spellthorn?sub_c...
    ||||||||||||||| 231% ||||||||||||||| 4.62K/2K
    Most recent subscriber: Brett Kromkamp
    @Spellthorn is a talented web/mobile developer who is dedicated to helping others improve their coding skills. Through their UA-cam channel, they create informative and engaging videos that cover a wide range of programming topics. - spellthorn.com/
  • Навчання та стиль

КОМЕНТАРІ • 123

  • @cf6979
    @cf6979 11 місяців тому +13

    You are breaking ground in the Flutter/Flame community and showcasing it in an easy to learn way.👍 Thank you!

    • @Spellthorn
      @Spellthorn  11 місяців тому +1

      Glad you think so! Trying my best to figure it out and help other 😁

  • @user-gg2pz1zk6z
    @user-gg2pz1zk6z 9 місяців тому +1

    ちょうどFlutter/Flameの勉強をしたかったので、とても参考になりました!
    ありがとうございます。
    これからも動画楽しみにしています

  • @emadeddineibo1842
    @emadeddineibo1842 11 місяців тому

    Finally! I was eager to see this part 😍
    I't gonna be a great evening today 😁

    • @Spellthorn
      @Spellthorn  11 місяців тому +1

      Yes, it was a long time coming 🤣 hoping the rest will be easier 😁

  • @arnovanzyl5976
    @arnovanzyl5976 11 місяців тому +2

    This series is absolutely BRILLIANT! Thank you

    • @Spellthorn
      @Spellthorn  11 місяців тому

      Thank you 😊 glad you're enjoying it. Next episode will be out tomorrow 😁

  • @hilgerto
    @hilgerto 3 місяці тому

    Thank you very much for the effort you put in

    • @Spellthorn
      @Spellthorn  3 місяці тому

      Glad you enjoyed it! 😊

  • @Isabel-ll7im
    @Isabel-ll7im 11 місяців тому

    This was definitely a long one but i'm really glad you were able to figure it out!

    • @Spellthorn
      @Spellthorn  11 місяців тому

      Yes, I'm glad too 😁

  • @Zerocchi
    @Zerocchi 9 днів тому

    I'm late to the party but great tutorial so far! I've learnt a lot about Flame from your videos. If you want to shorten your code a bit in 19:00, now you can make use of extension helper getters provided
    _final platform = CollisionBlock(position: collision.position, size: collision.size, isPlatform: true,);_
    Thank you for this tutorial :)

  • @ioexeption
    @ioexeption 8 місяців тому +2

    This is a great series, I´m really enjoying how simple and practical your approach is, coding along line by line without crazy copy pasting or pre-coded files. I am already a Flutter developer, so focusing only on the Flame specific features like you´ve been doing resonates with my needs. Thanks and keep on the good work!

    • @Spellthorn
      @Spellthorn  4 місяці тому +1

      Thanks, yea I'm not a fan of copy pasting tutorials either 😊

  • @gamerguide325
    @gamerguide325 11 місяців тому +1

    I was waiting for this video, Amazing work. There are some videos available for flame and flutter together but most of them are outdated. Iam working on a 2D game and this series has been a very helpful tool for my work. Thanks 🙏

    • @Spellthorn
      @Spellthorn  11 місяців тому

      I'm glad they have been helpful, that's why I wanted to make the series as I felt stuck when trying to learn flame myself

  • @minseokjeong1881
    @minseokjeong1881 11 місяців тому

    So goooooooood. Thnx for upload about flame video things.❤

    • @Spellthorn
      @Spellthorn  11 місяців тому +1

      Glad you enjoy it! We have a lot more to do 😁

  • @Amankumar_Raone
    @Amankumar_Raone Місяць тому

    just wow,thanx for shearing❤❤

  • @muhammadissasabbagh1540
    @muhammadissasabbagh1540 11 місяців тому

    Finally 🎉🎉🎉🎉 this is better than expected

    • @Spellthorn
      @Spellthorn  11 місяців тому

      Sorry for the delay. Definitely was a rough road. But feel great coming out on the otherside. 😁

  • @tima9738
    @tima9738 9 місяців тому +2

    Thank you for that videos! It's easy to learn by them!
    Just a tip: Flutter has geometry classes like Rect that has a method called 'overlaps()', and you could create rects and check if they overlap each other easily.

    • @Spellthorn
      @Spellthorn  2 місяці тому

      Hmmm I wonder how that would work, thanks 😊

  • @informativeinformations9580
    @informativeinformations9580 11 місяців тому

    Just amazing ❤❤

    • @Spellthorn
      @Spellthorn  11 місяців тому +1

      Glad you enjoy them

  • @YaxineZone
    @YaxineZone 7 місяців тому +1

    the issue of Player getting pushed outside the game frame was resolved by checking the update order. Proper ordering of update operations is crucial in game development to ensure that each step is executed in the correct sequence.

  • @whatsonrightnow
    @whatsonrightnow 3 місяці тому +1

    Nice series. Just a comment about using super in the constructor. If you are just passing the value through to the super constructor, you can just use *super.position* as the parameter to the constructor. So for example the entire CollisionBlock constructor can just be: *CollisionBlock({super.position, super.size, this.isPlatform = false});*

    • @Spellthorn
      @Spellthorn  3 місяці тому

      That's good to know. Thank you 😊

  • @mohammednasir2064
    @mohammednasir2064 4 дні тому

    Hello sir. Thank you for the great tutorial..but..how can I do a player double jump? I tried to figure this out, but when the player pressed the "space" button to trigger the jump function...the game engine continued counting until the player returned and touched the ground...so I was not able to add an int variable to count the number of presses on the "space" button. Although in the "hardware_keyboard" class
    There are Event like "KeyDownEvent" and "KeyUpEvent" but I couldn't get any of them to work.
    thank you

  • @shaiherman9872
    @shaiherman9872 7 місяців тому

    Great tutorial series I'm really enjoying it! I have an issue when running it as a macos app. When I use the keyboard I hear a system sound on every click. Any suggestion how to overcome this?

    • @Spellthorn
      @Spellthorn  4 місяці тому

      Hmm I'm not sure about Mac sorry

  • @Squinnmb
    @Squinnmb 9 місяців тому

    I don't have exp in game development but I can't image it's normal to have the _checkvertical/horizontalCollision() type methods where you loop through ever collision block to check if it's overlapping the character. Isn't there an event listener/callback style way to achieve?

    • @Spellthorn
      @Spellthorn  4 місяці тому

      Yea it could be bad but haven't found a better way to check it. It has built in collection ability but that caused way too many issues so went this approach

  • @jhovblack
    @jhovblack 11 місяців тому +2

    Hi! First of all, thank you very much for this tutorial, it has been great so far. I played around with the Godot engine but I felt without control of many things so I'm giving a try to Flame.
    My question is about the collisions, I'm wondering why didn't you use the HasCollisionDetection mixin from Flame. Maybe I missed the explanation from the video, sorry if that was the case.
    Thanks again for your effort and for sharing it!

    • @Spellthorn
      @Spellthorn  11 місяців тому +3

      I'm glad you're enjoying the series. I've used godot before but yea definitely liking Flame.
      So the reason that we didn't use the built in collisions is because you're always colliding which causes many errors so it was easier to check the horizontal collision first, then apply the gravity then check vertical collision rather than the built in collision always checking it. There's probably a way to use the built in collision but I tried figuring it out for weeks so just gave up 🤣 in the future episodes we do use the built in collision though 😁

    • @jhovblack
      @jhovblack 11 місяців тому +1

      @@Spellthorn Thanks for your answer! Glad to hear that you are using it in following episodes (because I paused your video and went to the docs to use the mixin and now I'm kind of stuck too 😂the player stops but just for a while).
      Keep up your good work! 💪

    • @Spellthorn
      @Spellthorn  11 місяців тому +1

      Yea there is a tutorial to use it with a circle hitbox and it works well. But for rectangle hit box it just doesn't work right. So not worth the hassle to figure it out 🤣

    • @jhovblack
      @jhovblack 11 місяців тому +1

      @@Spellthorn Finally I made it work well with the rectangle hitbox reducing the box size and a couple of flags to avoid changing the position on the collision. I'll continue with the gravity part hoping that my code will not be that different to yours 🤞

    • @Spellthorn
      @Spellthorn  11 місяців тому

      Thats awesome, yea gravity is what breaks everything with the built in collision, let me know if you find a way 😁

  • @EdsonAmorinaJunior
    @EdsonAmorinaJunior 4 місяці тому

    Hi, I have 2 issues: the movement does not work when i press A or D; and may arrows is wrong (its like my keyboard was Landescaped... lol), my Up arrow acts like Right arrow, my Down arrow as Left arrow, my Right arrow as Down, and the Left as Up.
    Anyone knows how to solve? :) thanks

  • @sitdownandcode
    @sitdownandcode 8 місяців тому +1

    you can use null aware operator instead of if statement like this:
    for (final spawnPoint in spawnPointsLayer?.objects ?? [])

    • @Spellthorn
      @Spellthorn  4 місяці тому

      Great to know thanks 😊

  • @cyrilchyi
    @cyrilchyi 7 місяців тому

    Thanks for your great video! I've learned a lot from your video. However I think I've found a bug about the gravity. You've updated the velocity according to the gravity. `velocity.y += gravity;`. However, just like you mentioned in your previous videos, you should multiply the gravity by dt. Otherwise, our player will jump differently in diffrent fps.

    • @Spellthorn
      @Spellthorn  4 місяці тому

      Yea gravity caused me a lot of issues. Never could find out why

  • @gogu2120
    @gogu2120 10 місяців тому +1

    thank you for your video!!! but i have a question about my bug. When i start the game, if i press leftarrow button, the player teleport to the lefttop corner ( x: 48 y: 48) and when i press rightarrow button, player suddenly goes out of the map and it falls down out of the screen. I compared all of the codes of mine with your codes on the github but everything is fine. I don't know the reason why this bug happens. Do you know any solutions??

    • @Spellthorn
      @Spellthorn  10 місяців тому +1

      This could be a collision issue as if it collides with bottom first it then moced you to the ends. Make sure your checking horizontal collision first then vertical otherwise this happens

    • @gogu2120
      @gogu2120 10 місяців тому

      @@Spellthorn In player.dart file, i changed the order of _applyGravity function and _checkVerticalCollisions function in update function and it worked. the _applyGravity function should be the first. Thank you for your answer :)

    • @Spellthorn
      @Spellthorn  10 місяців тому

      No problem, I'm glad you were able to fix it

  • @MmUQue
    @MmUQue 8 місяців тому

    Great work man! Thank you.
    I just had a problem, i got same constants than you but my character jumps really high, i dont know why :(
    I mean, i adjust the constants but i wonder why is different?
    Maybe its because behaves different depending each device, if so, how can we avoid that?

    • @shaiherman9872
      @shaiherman9872 7 місяців тому +1

      In the video he set the jumpForce to 460 but somewhere along the way (maybe he edited the video) he changed it to 260, i saw the value it in the github project.

    • @Spellthorn
      @Spellthorn  4 місяці тому

      I'm pretty sure I break the gravity here so different on different screens etc

    • @anandyerawar6473
      @anandyerawar6473 3 місяці тому

      i was stuck on it too

  • @WayneRenbjor
    @WayneRenbjor 9 місяців тому +2

    Here is how you would use the built in collision detection without making your own: void _checkHorizontalCollisions() {
    final playerBounds = toRect();
    for (final block in collisionBlocks) {
    if (playerBounds.overlaps(block.toRect())) {
    if (horizontalMovement < 0) {
    position.x = block.toRect().right + playerBounds.width;
    } else if (horizontalMovement > 0) {
    position.x = block.toRect().left - playerBounds.width;
    }
    }
    }
    }

    • @Spellthorn
      @Spellthorn  9 місяців тому

      I'll have to try that out thank you

    • @codemoor
      @codemoor 8 місяців тому

      its working thanks

    • @BrianMantel
      @BrianMantel 3 місяці тому

      Works well. Thanks. Just a note that although you are using built-in overlap detection, there is a real collision detection system that uses callbacks.

  • @emadeddineibo1842
    @emadeddineibo1842 11 місяців тому

    Just a tip, try to discover BonFire, it's completely compatible with Flame, and it provides many helpful functions. It's most used in RPG games, but still it might be useful.

    • @Spellthorn
      @Spellthorn  11 місяців тому +1

      Yea I've seen that, I'm not a huge fan of using other stuff inside other stuff if I can avoid it, too much to keep track of 🤣

    • @emadeddineibo1842
      @emadeddineibo1842 11 місяців тому

      @@Spellthorn I'm really excited to see the end version of this game, I'm sure you'll reach a place where you need State Management, storage, etc, that's what I'm looking for.
      Also, it would be nice if you present any idea how to make scrollable background, like in your game, imagine if the character can walk left infinitely.

    • @Spellthorn
      @Spellthorn  11 місяців тому +1

      I've implemented scrolling background today actually 😁 also I've done the fruit too. 😁

    • @emadeddineibo1842
      @emadeddineibo1842 11 місяців тому

      @@Spellthorn Can't wait to see it 😍

    • @Spellthorn
      @Spellthorn  11 місяців тому +1

      Yea trying to figure out the Saws next 😁

  • @ajitkrmaurya
    @ajitkrmaurya 11 місяців тому +1

    While testing the application properly, the character is getting away from the box. It seems it's working perfectly in the video, but in my application, it getting away from the box.
    Any suggestions?

    • @Spellthorn
      @Spellthorn  11 місяців тому

      When you say getting away from the box what do you mean? What box? Like the player is farther from the object?

    • @YaxineZone
      @YaxineZone 7 місяців тому +1

      its about methods ordering in the update

    • @user-mh4bt9ge9r
      @user-mh4bt9ge9r 4 місяці тому +1

      @@YaxineZone thanks a lot! it's working for me. I didn't notice that🤣

  • @nixonqqqqqqqqqqqwiguna8772
    @nixonqqqqqqqqqqqwiguna8772 3 місяці тому +1

    Is there a tutorial for use on mobile for the jump section so you can jump using the jump button?

    • @kyawnai
      @kyawnai 2 місяці тому

      You can find it on episode 8

    • @Spellthorn
      @Spellthorn  Місяць тому +1

      thanks for the help 😊

    • @Spellthorn
      @Spellthorn  Місяць тому +1

      like the other view stated in episode 8 😊

  • @tfozo
    @tfozo 11 місяців тому

    😮I was working on a thrust method of flying a player, I use a joystick component, however the Player doesn't fly towards the initial direction for a little while like any projectile then fall parabolically...but mine is completely stopped when I let go the joystick and gravity takes his soul almost instantly, how do u think I should approach this?

    • @Spellthorn
      @Spellthorn  11 місяців тому

      Hmm. To me it sounds like you'd want to decrease your y velocity while holding the joystick so it keeps subtracting more and more. And then if let go then have gravity start. Which should start it at a lower number and increasing making it more smooth. Also clamping the velocity so don't fall too fast

    • @denilsonwashuma1987
      @denilsonwashuma1987 11 місяців тому +1

      hey man, did you figure out the thrust method? could really use some help

    • @Spellthorn
      @Spellthorn  11 місяців тому +1

      Thrusting up should just be as easy as keep subtracting from your why, while button is pressed. Then stop when letting go and have gravity do the rest.
      Something like
      If(thrust) {
      position.y -= thrustAmount;
      }

    • @tfozo
      @tfozo 11 місяців тому

      @@denilsonwashuma1987 nah I didn't solve it, I left another comment in this section.it seems, We kinda need to be the first to integrate our own ironman logic from scratch to Flame

    • @denilsonwashuma1987
      @denilsonwashuma1987 11 місяців тому

      @@Spellthorn Thanks will try it out

  • @crazyvolt1660
    @crazyvolt1660 9 місяців тому +1

    The video was amazing, but Im not a good student of physics, velocity, gravity and other stuff are hard to understand. 😄
    Im still try to understand them.

    • @Spellthorn
      @Spellthorn  9 місяців тому +1

      I'm glad it was helpful, you'll get the hang of it 😁

  • @Pridzoyo
    @Pridzoyo 2 місяці тому

    I did everything like you did in the video, but for some reason my character falls down and out of the level. There is no collision.

    • @Pridzoyo
      @Pridzoyo 2 місяці тому

      Nevermind, I fixed the problem.

    • @Spellthorn
      @Spellthorn  2 місяці тому +1

      Glad you were able to fix it 🙂

  • @GarthFransman
    @GarthFransman 10 місяців тому

    I think the guys at Flame could use some of your input 🙂

    • @Spellthorn
      @Spellthorn  10 місяців тому

      I'm in their discord, they have been a great help so far 😁

  • @informativeinformations9580
    @informativeinformations9580 11 місяців тому

    Is it possible to create 3d games i.g. FPS games in flutter?

    • @gamerguide325
      @gamerguide325 11 місяців тому

      You can make 3D games with flutter although it requires unity. Flame engine is not enough for 3D, it was designed to handle 2D game development.

    • @Spellthorn
      @Spellthorn  11 місяців тому +1

      Flame is mainly for 2d games. You could probably make a 3d looking 2d game though

    • @informativeinformations9580
      @informativeinformations9580 11 місяців тому

      @@gamerguide325 means I will have to create 3d models in Unity and logic in flutter insted of C# right?

    • @Spellthorn
      @Spellthorn  11 місяців тому

      They mean if you want 3d you're better off using a game engine for that like unity. You wouldn't use flutter/flame for that

    • @informativeinformations9580
      @informativeinformations9580 11 місяців тому

      @@Spellthorn oh, thanks understood

  • @theanimeclan27
    @theanimeclan27 10 місяців тому

    My player is not spawning inside the game frame its getting pushed outside the game frame😥😥
    need help it is spawning on top of the game frame

    • @Spellthorn
      @Spellthorn  10 місяців тому

      Is gravity causing it when it touches something to move outside the frame? Or when it first spawns is it spawning outside the frame? If its the first one turn off gravity and see if still happens. If its the 2nd one try a different position point

    • @theanimeclan27
      @theanimeclan27 10 місяців тому

      @@Spellthornits the second and i tried different positions but it keeps spawning right above the spawn point on the frame

    • @Spellthorn
      @Spellthorn  10 місяців тому

      Do you have the level.dart file correct, where you pass in the player position? Also make sure your passing your position to the super

    • @theanimeclan27
      @theanimeclan27 10 місяців тому

      @@Spellthorn i am having problem like the portion below the vertical frames have become colliding like my player can't go below the frame too. like where ever i keep my vertical frame the portion below i collide

    • @Spellthorn
      @Spellthorn  10 місяців тому

      Okay so it seems the error is definitely somewhere with the collisions, I have my code on github, it is a further version then where you are at but I would consider viewing that and seeing where your code may be different at least for the sections to where you are now

  • @alimurtaza7438
    @alimurtaza7438 10 місяців тому

    How can I play this game on mobile phone

    • @Spellthorn
      @Spellthorn  10 місяців тому

      You can build for my mobile by running flutter build apk then plug in phone and do flutter install

  • @coderlyfe1023
    @coderlyfe1023 10 місяців тому

    your tutorials are great however refatoring in the middle of the series is extremely annoying

    • @Spellthorn
      @Spellthorn  10 місяців тому +1

      Glad you're enjoying them. Sometimes when coding refactoring will need to be done. If there's a better way to do something, you should do it as early as possible otherwise you wont want to at the end or break stuff later if you do.

  • @dmitryandco.528
    @dmitryandco.528 6 місяців тому

    Nice series! 🩵
    But why do you use switch statements when you don't need to and don't use them when you need to?
    And don't ignore type safety please, it hurts to see your dynamic parameters 🙏

    • @Spellthorn
      @Spellthorn  4 місяці тому +2

      Well, I'm a self taught coder so ... I don't always use good practices, I use what works in the moment, and what I feel makes sense. This leads me to doing things wrong a lot in terms of common practices as I don't know what they are, but hey if it works it works right? 😂

    • @dmitryandco.528
      @dmitryandco.528 4 місяці тому

      @@Spellthorn sure, the main thing is to make it all work, and you do it 🙌

  • @ricardomejias7957
    @ricardomejias7957 10 місяців тому +1

    This is a great series, I´m really enjoying how simple and practical your approach is, coding along line by line without crazy copy pasting or pre-coded files. I am already a Flutter developer, so focusing only on the Flame specific features like you´ve been doing resonates with my needs. Thanks and keep on the good work!

    • @Spellthorn
      @Spellthorn  10 місяців тому +1

      I'm glad you're enjoying the series so far. Yea I always hated tutorials where they copy paste a lot, like umm what does that do and why. I'm still working on having a good structure, I work out this code before hand sometimes takes hours to figure out something then struggle to remember the order I did it, so I've started taking notes while coding

    • @ricardomejias7957
      @ricardomejias7957 10 місяців тому +1

      ​@@Spellthorn another thing I forgot to mention: displaying your keyboard input on the recording while typing is a great touch, really helps catching those keyboard shortcuts

    • @Spellthorn
      @Spellthorn  10 місяців тому

      Yea I was glad I found that tool definitely helps out as I do use a lot of short cuts, sometimes I forget to turn it on though and realize later