Ryu204
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Hymn mini-devlog (Part 2) **Game link:** [Hymn](https://labg.itch.io/hymn) **Other parts:** [Part 1](https://hackmd.io/@4bNcOyqaSZ-TSF0OdM7r2Q/rywobJTVh) ![Thumbnail](https://i.imgur.com/Uxw0wGw.png) In last devlog, I wrote about how I framed the game's basic structure and collision detection and resolution. Collision, however important, is not all game's physics about. Today we will talk about another aspect of physics in Hymn down to the nitty-girtty. It is character's movement. ## I. Setup - Level editor While I was working on the barebone of our game, my teamates had been finishing off the arts and game mechanic drafts. The art is great and nothing much to say about . Game mechanic, on the other hand, proved to be a bit controversal between our group. Basically, the character is tasked to pass through several rooms. Each room is a level consisting of platform elements like walls and spikes. The player had freedom to move left and right, as well as jumping. We then decided some sort of power up would be the signature of our liltle game. For short, at the start of a level, players are given a fixed amount of *boost-s*, and they must use them wisely to jump over the platform to get to the exit door. Don't worry, there will be a lot of explaining in this devlog. To make a fine-tuned game mechanic, a lot of testing by the developers before the beta testing stage is required. I decided I needed to work on the level editor for that purpose first. A level editor should have these features at minimum: * Fast level building (can draw as efficiently as MSPaint pencil and shapes) * Multiple layers support * Customizable export * Quick pipeline when integrating to real game design A while back I made a study project featuring a map editor that allowed user to draw blocks in a texture into the canvas and export the data. The UI is super weird and UX is even worse: ![Protomap](https://i.imgur.com/58Ow974.png) I'm a bit dissapointed that this version does not satisfy any features I mentioned. However, having something to fix is better than building from source. So after a day of tweaking I eventually get to this: ![MapEditor](https://i.imgur.com/bZ4R95G.png) It does not look much visually, but I added customizable export, single-stroke brush style, and most importantly, collision blocks export. The output data is writtern in raw text file. For customising I would write a converter to my desired output. The game read files writtern in Lua. I chose it because it allows me to be more flexible when editing files, and already has C API. An output example: ```lua entities = 2 _1 = { c = 2, _1 = { t = "Transform", v = {box = {l = 128, t = 144, w = 16, h = 16}, rotation = 0} }, _2 = { t = "Render", v = {txtr = "Map", texcord = {l = 16, t = 112, w = 16, h = 16}} }, } _2 = { c = 2, _1 = { t = "Transform", v = {box = {l = 144, t = 144, w = 16, h = 16}, rotation = 0} }, _2 = { t = "Render", v = {txtr = "Map", texcord = {l = 16, t = 112, w = 16, h = 16}} }, } ``` Every entites are represented by a table, consisting of 3 elements: * `c`: number of components * Each component has: * `t`: component name * `v`: component data With all of that hooked up, I created a static test scene: ![MapTest](https://i.imgur.com/QSIa5cm.png) The jaggy lines in the background were used to see if the sprites are fit into tiles. Because of OpenGL rasterizing, not all of the lines are shown, but it was such minor problem so I did not try to fix it (it's only used in development phase anyway). ## II. Character base movement: Okay, we got everything ready for the most interesting part of the game: mechanics. Firstly, I implemented player's movements. Remember that Hymn's backbone is ECS system. So we need system to do the movement for us. With that in mind, I created `Movement` component: ```cpp struct Movement { float run_acceleration; float jump_height; // this variable is used for collision resolution bool on_ground; }; ``` Then, I will have a `PlayerControlSystem` that will update `Movement` component: ```cpp void PlayerControlSystem::update(sf::Time dt) { for (auto e : m_entities) { // Get components auto& movement = m_ecs->get_component<Movement>(e); auto& rigid = m_ecs->get_component<RigidBody>(e); // Update movement rigid.acc.x = 0; if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) rigid.acc.x = -movement.run_acceleration; if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) rigid.acc.x = movement.run_acceleration; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) { float gravity = std::abs(rigid.acc.y); // Set velocity so the jump will be at exact height rigid.vel.y = -std::sqrt(2 * movement.jump_height * gravity); } } } ``` This way, when the player presses movement buttons, the character will start to speed up and move to desired location. So we need a way to limit their speed, otherwise the player would just move faster and faster. One way to do it is to introduce friction in `RigidBody` component, then let the friction limits the player's speed in `RigidSystem`: ```cpp void RigidSystem::update(sf::Time dt) { for(auto& e : m_entities) { auto& tf = m_ecs->get_component<Transform>(e); auto& rg = m_ecs->get_component<Rigid>(e); rg.vel += rg.acc * dt.asSeconds(); rg.vel.x *= std::pow(rg.friction.x, dt.asSeconds()); rg.vel.y *= std::pow(rg.friction.y, dt.asSeconds()); } } ``` This code is closer to the physics in real life, however it's not always desired in 2D games. The problem is it lacks the *snappy-ness* that most 2D platformers provide. This is what I meant: ![Difference](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExZDAxYWQ5ZjVmMjYzMzdiNGY2OTUxMzBhMmMyZDJlZGNhMmQxNzlkZCZlcD12MV9pbnRlcm5hbF9naWZzX2dpZklkJmN0PWc/XxqW1VdAXlhqLe7mMi/giphy.gif) Imagine your character is rushing and then breaks immediately because of the black pitch ahead. If they decrease their velocity by time (like in second figure), there is a chance that you will fall into the pitch without consent. I wanted to avoid that. So, I decided to just modify the velocity directly: ```cpp struct Movement { float speed; // ... }; void PlayerControlSystem::update(sf::Time dt) { for (auto e : m_entities) { // Get components auto& movement = m_ecs->get_component<Movement>(e); auto& rigid = m_ecs->get_component<RigidBody>(e); // Update movement rigid.vel.x = 0; if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) rigid.vel.x = -movement.speed; if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) rigid.vel.x = movement.speed; // ... } } ``` ## III. The boost

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully