فهرست منبع

fix CollisionShape2D.

SakiKawasaki 4 سال پیش
والد
کامیت
239c0eaae5
3فایلهای تغییر یافته به همراه56 افزوده شده و 69 حذف شده
  1. 36 1
      src/nodesnim/core/polygon2.nim
  2. 19 67
      src/nodesnim/nodes2d/collision_shape2d.nim
  3. 1 1
      tests/test33.nim

+ 36 - 1
src/nodesnim/core/polygon2.nim

@@ -1,7 +1,9 @@
 # author: Ethosa
 ## Provides primitive 2d polygon.
 import
-  vector2
+  vector2,
+  rect2,
+  circle2
 {.used.}
 
 
@@ -75,3 +77,36 @@ proc intersects*(self, other: Polygon2Ref): bool =
         d = other.positions[othernext]
       if intersects(a, b, c, d):
         return true
+
+proc intersects*(self: Polygon2Ref, r: Rect2Ref): bool =
+  ## Returns true, if rect intersect with polygon.
+  var next = 1
+  let length = self.positions.len()
+
+  for i in 0..<length:
+    inc next
+    if next == length: next = 0
+    let
+      a = self.positions[i]
+      b = self.positions[next]
+    if r.contains(a, b):
+      return true
+
+proc intersects*(self: Polygon2Ref, circle: Circle2Ref): bool =
+  ## Returns true, if circle intersect with polygon.
+  var next = 1
+  let length = self.positions.len()
+
+  for i in 0..<length:
+    inc next
+    if next == length: next = 0
+    let
+      a = self.positions[i]
+      b = self.positions[next]
+    if circle.contains(a, b):
+      return true
+
+proc move*(self: Polygon2Ref, vec2: Vector2Ref) =
+  ## Moves polygon.
+  for i in 0..self.positions.high:
+    self.positions[i] += vec2

+ 19 - 67
src/nodesnim/nodes2d/collision_shape2d.nim

@@ -9,6 +9,7 @@ import
   ../core/input,
   ../core/enums,
   ../core/circle2,
+  ../core/polygon2,
 
   ../nodes/node,
   node2d
@@ -230,17 +231,9 @@ method isCollide*(self, other: CollisionShape2DPtr): bool {.base.} =
       of COLLISION_SHAPE_2D_POLYGON:
         var
           rect = Rect2(self.global_position, self.rect_size)
-          next = 1
-        let length = other.polygon.len()
-
-        for i in 0..<length:
-          inc next
-          if next == length: next = 0
-          let
-            a = other.polygon[i] + other.global_position
-            b = other.polygon[next] + other.global_position
-          if rect.contains(a, b):
-            return true
+          a = Polygon2(other.polygon)
+        a.move(other.global_position)
+        return a.intersects(rect)
   of COLLISION_SHAPE_2D_CIRCLE:
     case other.shape_type:
       of COLLISION_SHAPE_2D_CIRCLE:
@@ -256,68 +249,27 @@ method isCollide*(self, other: CollisionShape2DPtr): bool {.base.} =
       of COLLISION_SHAPE_2D_POLYGON:
         var
           circle = Circle2(self.global_position.x + self.x1, self.global_position.y + self.y1, self.radius)
-          next = 1
-        let length = other.polygon.len()
-
-        for i in 0..<length:
-          inc next
-          if next == length: next = 0
-          let
-            a = other.polygon[i] + other.global_position
-            b = other.polygon[next] + other.global_position
-          if circle.contains(a, b):
-            return true
-        return false
+          a = Polygon2(other.polygon)
+        a.move(other.global_position)
+        return a.intersects(circle)
   of COLLISION_SHAPE_2D_POLYGON:
     case other.shape_type:
       of COLLISION_SHAPE_2D_RECTANGLE:
         var
           rect = Rect2(other.global_position, other.rect_size)
-          next = 1
-        let length = self.polygon.len()
-
-        for i in 0..<length:
-          inc next
-          if next == length: next = 0
-          let
-            a = self.polygon[i] + self.global_position
-            b = self.polygon[next] + self.global_position
-          if rect.contains(a, b):
-            return true
+          a = Polygon2(self.polygon)
+        a.move(self.global_position)
+        return a.intersects(rect)
       of COLLISION_SHAPE_2D_POLYGON:
-        let
-          length = self.polygon.len()
-          otherlength = other.polygon.len()
-        var next = 0
-
-        for i in 0..<length:
-          inc next
-          if next == length: next = 0
-          let
-            a = self.polygon[i] + self.global_position
-            b = self.polygon[next] + self.global_position
-
-          var othernext = 0
-          for i in 0..<otherlength:
-            inc othernext
-            if othernext == otherlength: othernext = 0
-            let
-              c = other.polygon[i] + other.global_position
-              d = other.polygon[othernext] + other.global_position
-            if intersects(a, b, c, d):
-              return true
+        var
+          a = Polygon2(self.polygon)
+          b = Polygon2(other.polygon)
+        a.move(self.global_position)
+        b.move(other.global_position)
+        return a.intersects(b)
       of COLLISION_SHAPE_2D_CIRCLE:
         var
           circle = Circle2(other.global_position.x + other.x1, other.global_position.y + other.y1, other.radius)
-          next = 1
-        let length = self.polygon.len()
-
-        for i in 0..<length:
-          inc next
-          if next == length: next = 0
-          let
-            a = self.polygon[i] + self.global_position
-            b = self.polygon[next] + self.global_position
-          if circle.contains(a, b):
-            return true
-        return false
+          a = Polygon2(self.polygon)
+        a.move(self.global_position)
+        return a.intersects(circle)

+ 1 - 1
tests/test33.nim

@@ -1,4 +1,4 @@
-# --- Test 32. use KinematicBody2D node. --- #
+# --- Test 33. use KinematicBody2D node. --- #
 # Please, compile with `--define:debug` or with `-d:debug` for see collision shapes.
 import nodesnim