halloworld
    • 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 New
    • 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 Note Insights Versions and GitHub Sync 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Separating Axis Theorem(SAT) 分離軸碰撞檢測 ###### tags: `game design` `collision` :::info 參考自:http://davidhsu666.com/archives/gamecollisiondetection/#no1 ::: ## 小知識 - box2d多邊形實作為SAT ## 目的 - 以往檢測碰撞幾乎都是用bounding box,導致有部分位置無法實際計算出碰撞位置 - 透過分離軸,能夠更準確的判斷兩物體是否碰撞 ### 需要的向量知識,將其刻成一個小Lib - 內積 - 正射影(投影向量) - ![](https://i.imgur.com/0ljw3GK.png) - 向量長 - 左法向量 - 右法向量 ## 分離軸檢測原理 - 兩個==凸多邊形==,在任意角度下的投影皆有重疊,代表物體發生碰撞 - **凹多邊形不適用** ![](https://i.imgur.com/pzjyaff.png) ## 分離線、分離軸 - 第一排的物體之間有縫隙,所以能夠輕鬆地畫出一條線來分離它們 - 第二排就不行,因為已經碰撞 - ![](https://i.imgur.com/WBrUEVl.png) ### 分離軸 - 與分離線垂直的一條線,透過分離軸上物體的投影,來判斷是否發生碰撞 - ==可檢查旋轉的物體== - ![](https://i.imgur.com/3CLaZhG.png) - ![](https://i.imgur.com/SeFBQ05.png) --- ## 如何取得分離軸上的投影 - 利用投影公式,把A、B的四個角投影在P向量上就能知道`min`、`max` - ![](https://i.imgur.com/JKrHKIA.png) - 旋轉怎辦? - 兩物體方向不同時,只要在4個角落中選一個最小和最大的,就是min、max - ![](https://i.imgur.com/CZ4t4af.png) - 如何判斷? - 利用正射影(投影向量) - ![](https://i.imgur.com/z8csP2P.png) - code ```js= if (B.min > A.max || A.min > B.max) Console.log("分離");// isSeparated else Console.log("碰撞");// isCollided ``` --- ## 旋轉問題 :::danger 你如果直接帶公式在這四個點,就會變左圖 但是其實你是想要右邊的結果 ::: - ![](https://i.imgur.com/2akFbX3.png) ### 解法 - 將物體平移到原點,旋轉完後再將其平移回來 - ![](https://i.imgur.com/ebWyz7q.png) > ==參考點旋轉就加到向量lib== --- ## 怎找分離軸 - 只需沿著物體==所有邊上的法向量==當作分離軸作檢查 - 綠色那條分離線,可以當作五角形守備的領域,只要有東西進到這裡,就代表碰撞 - ![](https://i.imgur.com/Go9XwoX.png) ### 解法 - 透過頂點來找出所有(左)法向量 - ![](https://i.imgur.com/eJUpEtA.png) --- ## 矩形(只需找兩個法向量,因為另外兩個只是反向) - P、Q - A的法向量 - R、S - B的法向量 1. 找出四個角在法向量的投影、這時候就知道Min、Max 2. 檢查是否分離 - ![](https://i.imgur.com/qGvAzMa.png) ```cpp= bool Box::SAT_collision(Box &other) { this->setVertices(); this->findSAT(); other.setVertices(); other.findSAT(); //分離軸枚舉,只需枚舉兩個向量,因為另外兩個只是反向而已 auto PA = getMinMax(this->SAT[0], Vertices); auto PB = getMinMax(this->SAT[0], other.getVertices()); auto QA = getMinMax(this->SAT[1], Vertices); auto QB = getMinMax(this->SAT[1], other.getVertices()); auto other_sat = other.getSAT(); auto RA = getMinMax(other_sat[0], Vertices); auto RB = getMinMax(other_sat[0], other.getVertices()); auto SA = getMinMax(other_sat[1], Vertices); auto SB = getMinMax(other_sat[1], other.getVertices()); //檢查分離軸上投影區段是否分開 bool sep_P = (PB.first > PA.second) || (PA.first > PB.second); bool sep_Q = (QB.first > QA.second) || (QA.first > QB.second); bool sep_R = (RB.first > RA.second) || (RA.first > RB.second); bool sep_S = (SB.first > SA.second) || (SA.first > SB.second); if (sep_P || sep_Q || sep_R || sep_S) { return false; } else { return true; } } ``` ## 多邊形 (Polygon) - 枚舉所有邊,算出各點在SAT的最大最小值,再去比較 - `setVertices()` - 設定旋轉過後,加上offset後的實際位置 - `findSAT()` - 找出分離軸 - `getMinMax()` - 找出各點在分離軸上的最小值與最大值為多少 ```cpp= bool Polygon::isCollide(Polygon &p) { this->setVertices(); this->findSAT(); auto poly_a_sat = this->getSAT(); p.setVertices(); p.findSAT(); auto poly_b_sat = p.getSAT(); // poly_a_sat check for (int i = 0; i < poly_a_sat.size(); i++) { auto A = getMinMax(poly_a_sat[i], this->Vertices); auto B = getMinMax(poly_a_sat[i], p.getVertices()); // 只要發現有一條分離線,就代表物體沒有發生碰撞 if (B.first > A.second || A.first > B.second) { return false; } } // poly_b_sat check for (int i = 0; i < poly_b_sat.size(); i++) { auto A = getMinMax(poly_b_sat[i], this->Vertices); auto B = getMinMax(poly_b_sat[i], p.getVertices()); // 只要發現有一條分離線,就代表物體沒有發生碰撞 if (B.first > A.second || A.first > B.second) { return false; } } return true; } ``` ## 圓形&圓形 - 判斷兩圓形半徑之和,是否小於與兩個圓心的位置距離,若有則會碰撞 - $A_{r1} + B_{r2} < dis(A,B)$ ## 圓形與多邊形 - 枚舉多邊形的法向量,投影至向量上 - 四邊形點的投影 - 圓心投影,加上半徑,減掉半徑 - ![](https://i.imgur.com/BsO1wDL.png) ## 多個物體的碰撞優化 - $O(N^2)$ - 枚舉所有object,算出結果 ## Author's source code - 我的多邊形碰撞範例: http://davidhsu666.com/downloads/Collision-SAT/ver0.2.1-polygon-merge/ - 向量函式庫 : https://github.com/md9830415/JS-Vector2D ## TODO - AABB Tree 優化

    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