Registrieren Anmelden
  • Startseite
  • Projekte
    • Network Library
      • Overview [DE]
      • Tutorials
      • Forum
      • Dokumentation
      • Downloads
      • Lizenz
    • BoxesWithGuns
      • Forum
      • Leaderboard
      • Achievements
      • Exceptions
    • Warrior King RPG
      • Credits
    • Location Silence
    • Werewolf online
    • PokeMeet
    • KUSSS
      • Privacy Policy
      • Feedback
      • Downloads
  • Reviews
  • Blog
  • 日本語
    • 文法
      • JLPTN3
    • 語彙
    • 日記
  • Forum
  • Impressum
    • Kategorie
    • Schlagwort
    • Autor
    • Datum
    • Suche nach
  • Abbrechen

AABB Collision Detection with Intersection Points (Incl. Rotation)

    • Thomas Christof
    • 13. Januar 2018
    • Januar 13th, 2018
    • Hinterlasse einen Kommentar
    • Blog
    • Algorithm C#

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    public struct Line
    {
        public Vector2 a;
        public Vector2 b;
 
        public Line(float x1, float y1,
            float x2, float y2)
        {
            a = new Vector2(x1, y1);
            b = new Vector2(x2, y2);
        }
 
        public void Rotate(float rad, Vector2 center)
        {
            Rotate(rad, ref a, center);
            Rotate(rad, ref b, center);
        }
 
        private void Rotate(float rad, ref Vector2 point, Vector2 center)
        {
            point = Vector2.Transform(point - center, Matrix.CreateRotationZ(rad)) + center;
        }
 
        public bool Intersect(Line line, out Vector2? intersectionPoint)
        {
            intersectionPoint = null;
 
            float a1 = b.Y - a.Y;
            float b1 = a.X - b.X;
            float c1 = a1 * a.X + b1 * a.Y;
 
            float a2 = line.b.Y - line.a.Y;
            float b2 = line.a.X - line.b.X;
            float c2 = a2 * line.a.X + b2 * line.a.Y;
 
            //If the lines are parallel, no collision possible.
            float det = a1 * b2 - a2 * b1;
            if (det == 0) return false;
 
            float x = (b2 * c1 - b1 * c2) / det;
            float y = (a1 * c2 - a2 * c1) / det;
 
            if (OnLine(this, x, y) && OnLine(line, x, y))
            {
                intersectionPoint = new Vector2(x, y);
                return true;
            }
 
            return false;
        }
 
        public static bool OnLine(Line line, float x, float y)
        {
            return Math.Min(line.a.X, line.b.X) <= x &&
                x <= Math.Max(line.a.X, line.b.X) &&
                Math.Min(line.a.Y, line.b.Y) <= y &&
                y <= Math.Max(line.a.Y, line.b.Y);
        }
    }

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    public struct SRectangle
    {
        public Line left;
        public Line top;
        public Line right;
        public Line bottom;
 
        public void Rotate(float rad, Vector2 center)
        {
            left.Rotate(rad, center);
            top.Rotate(rad, center);
            right.Rotate(rad, center);
            bottom.Rotate(rad, center);
        }
 
        public static SRectangle FromRectangle(Rectangle rectangle, Vector2 origin, float rotation)
        {
            Line left = new Line(rectangle.X, rectangle.Y, rectangle.X, rectangle.Y + rectangle.Height);
            Line top = new Line(rectangle.X, rectangle.Y, rectangle.X + rectangle.Width, rectangle.Y);
            Line right = new Line(rectangle.X + rectangle.Width, rectangle.Y, rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height);
            Line bottom = new Line(rectangle.X, rectangle.Y + rectangle.Height, rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height);
 
            left.a.X -= origin.X;
            left.a.Y -= origin.Y;
            left.b.X -= origin.X;
            left.b.Y -= origin.Y;
            top.a.X -= origin.X;
            top.a.Y -= origin.Y;
            top.b.X -= origin.X;
            top.b.Y -= origin.Y;
            right.a.X -= origin.X;
            right.a.Y -= origin.Y;
            right.b.X -= origin.X;
            right.b.Y -= origin.Y;
            bottom.a.X -= origin.X;
            bottom.a.Y -= origin.Y;
            bottom.b.X -= origin.X;
            bottom.b.Y -= origin.Y;
 
            origin.X += rectangle.X - rectangle.Width / 2;
            origin.Y += rectangle.Y - rectangle.Height / 2;
 
            left.Rotate(rotation, origin);
            right.Rotate(rotation, origin);
            top.Rotate(rotation, origin);
            bottom.Rotate(rotation, origin);
 
            return new SRectangle()
            {
                left = left,
                right = right,
                top = top,
                bottom = bottom
            };
        }
    }

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
        public static bool AABB_AABB(SmartRectangle x, SmartRectangle y, out Vector2[] collisionPoints)
        {
            List<Vector2> cp = new List<Vector2>();
 
            /* Left against y */
            if (x.left.Intersect(y.left, out Vector2? llcp)) cp.Add(llcp.Value);
            if (x.left.Intersect(y.top, out Vector2? ltcp)) cp.Add(ltcp.Value);
            if (x.left.Intersect(y.right, out Vector2? lrcp)) cp.Add(lrcp.Value);
            if (x.left.Intersect(y.bottom, out Vector2? lbcp)) cp.Add(lbcp.Value);
            /* Top against y */
            if (x.top.Intersect(y.left, out Vector2? tlcp)) cp.Add(tlcp.Value);
            if (x.top.Intersect(y.top, out Vector2? ttcp)) cp.Add(ttcp.Value);
            if (x.top.Intersect(y.right, out Vector2? trcp)) cp.Add(trcp.Value);
            if (x.top.Intersect(y.bottom, out Vector2? tbcp)) cp.Add(tbcp.Value);
            /* Right against y */
            if (x.right.Intersect(y.left, out Vector2? rlcp)) cp.Add(rlcp.Value);
            if (x.right.Intersect(y.top, out Vector2? rtcp)) cp.Add(rtcp.Value);
            if (x.right.Intersect(y.right, out Vector2? rrcp)) cp.Add(rrcp.Value);
            if (x.right.Intersect(y.bottom, out Vector2? rbcp)) cp.Add(rbcp.Value);
            /* Bottom against y */
            if (x.bottom.Intersect(y.left, out Vector2? blcp)) cp.Add(blcp.Value);
            if (x.bottom.Intersect(y.top, out Vector2? btcp)) cp.Add(btcp.Value);
            if (x.bottom.Intersect(y.right, out Vector2? brcp)) cp.Add(brcp.Value);
            if (x.bottom.Intersect(y.bottom, out Vector2? bbcp)) cp.Add(bbcp.Value);
 
            collisionPoints = cp.ToArray();
            return cp.Count > 0;
        }

 

Share this:

  • Klick, um über Twitter zu teilen (Wird in neuem Fenster geöffnet)
  • Klick, um auf Facebook zu teilen (Wird in neuem Fenster geöffnet)
  • Zum Teilen auf Google+ anklicken (Wird in neuem Fenster geöffnet)

Ähnliche Beiträge

Supported Frameworks

Hinterlasse eine Antwort Antwort abbrechen

Du musst angemeldet sein, um einen Kommentar abzugeben.

Anmelden

Tags

.net Algorithm Attachment Benchmark Bit BWG C# ChangeLog Converter Decoding Desura DevLog DynamicStringSearch Encoding Gamepad Input IPv4 Iteration Java Keyboard LazyGuysBundle Links MonoGame Mouse Network Library Passive form Performance Plugin Release Report Review Snippet Sorting algorithm Stats Steam Touch Tutorial Userinterface Visual Studio VS VS13 VS15 XAML XNA 受身形

Last posts

  • AABB Collision Detection with Intersection Points (Incl. Rotation) 13. Januar 2018
  • Supported Frameworks 22. Oktober 2017
  • Switching to Linux – Roadmap 18. Oktober 2017
  • WPF ObservableCollection – Add millions of data within milliseconds 26. März 2017
  • WPF Scrollable + Zoomable + Dragable Control 21. März 2017
  • ABP – Alpha-Beta Pruning 16. März 2017
  • DLS – Depth-Limited Search 16. März 2017
  • GBFS – Greedy Best-First Search 16. März 2017
  • MM – MinMax Search 16. März 2017
  • A* – AStar 16. März 2017
  • UCS – Uniform Cost-Search 16. März 2017
  • BFS – Breadth-First Search 16. März 2017
  • Search Algorithms 26. Januar 2017
  • SSL 9. Januar 2017
  • Windows Phone, goodbye after 4 years 6. Januar 2017

Partners

Arengu - 2D RPG

. Copyright Ⓒ 2018. Alle Rechte vorbehalten.
Erstellt mit Wordpress. Thema von Sanjagh

Schließen
  • Benutzername
  • Passwort
  • Passwort vergessen?