summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authortradam <git.tradam.fyi>2021-08-16 14:09:49 -0400
committertradam <git.tradam.fyi>2021-08-16 14:09:49 -0400
commitebb9245ccef8a6b9c908f82ecea6bea26dde14b5 (patch)
treefdc07b453526028f8d84f10e2e0bb1a22ac6941c
parent30c2e72d5b5558120f67a3b395d06b87c4afc7d9 (diff)
downloadarbitrary-polygon-collision-ebb9245ccef8a6b9c908f82ecea6bea26dde14b5.tar.gz
arbitrary-polygon-collision-ebb9245ccef8a6b9c908f82ecea6bea26dde14b5.zip
it works
-rw-r--r--README.mdown4
-rw-r--r--run.rb39
2 files changed, 26 insertions, 17 deletions
diff --git a/README.mdown b/README.mdown
index 26024b5..bc923e1 100644
--- a/README.mdown
+++ b/README.mdown
@@ -1,7 +1,5 @@
# hints and notes for Debugging
-- Shape A for some reason is not understood by the hitbox detection correctly
-As can be seen with the debug lines, one of the lines is drawn diagnal when it instead should of been drawn online one of the edges. The other line has been determined to be of length 0 so it is as if the points are swapped. Despite both shapes using what appear to be essentially the same code only one of the two shapes are in error. If the a and b shapes are swapped then the issue switches shapes(from a to b) so the issue doesnt appear to be with declaring the shapes but rather something in the hitbox logic.
-- (Maybe??) because of this strange diagnal, it seems that the shapes collision is not correctly determined(try moving the shape just outside the lower left corner of the square and you can see it detects a collision where there is none)
+If the shape is brought to the bottom left corner then the hitbox collision fails
# Links for the math
see the comments at the top of `run.rb` for links that explain the math
diff --git a/run.rb b/run.rb
index 7e28c7b..eea11f1 100644
--- a/run.rb
+++ b/run.rb
@@ -18,6 +18,9 @@ require 'ruby2d/camera'
$colors = %w[blue teal green lime
yellow orange red fuchsia]
+set width: 800
+set height: 800
+
#DEBUG: used to draw, store and update debug lines
class DebugLines
class << self
@@ -32,9 +35,9 @@ class DebugLines
end
# The coordinates of the 2 shapes being tested for collision
-svA = { x1: 200.0, y1: 100.0,
- x2: 200.0, y2: 200.0,
- x3: 100.0, y3: 200.0,
+svA = { x1: 230.0, y1: 100.0,
+ x2: 200.0, y2: 250.0,
+ x3: 50.0, y3: 200.0,
x4: 100.0, y4: 100.0 }
svB = { x1: 275.0 - 275.0, y1: 175.0 - 175.0,
@@ -43,11 +46,13 @@ svB = { x1: 275.0 - 275.0, y1: 175.0 - 175.0,
x4: 250.0 - 275.0, y4: 250.0 - 175.0 }
# The 2 shapes being tested for collision
+
sA = Camera::Quad.new(
**svA,
color: 'olive',
z: 10
)
+
sB = Camera::Quad.new(
**svB,
color: 'aqua',
@@ -57,11 +62,11 @@ sB = Camera::Quad.new(
# The hitbox logic
def hitbox_check(shape_a, shape_b)
# Get normals of both shapes
- inverted = build_inverted_edges(shape_b)
- inverted.concat(build_inverted_edges(shape_a))
+ inverted = build_inverted_edges(shape_a)
+ inverted.concat(build_inverted_edges(shape_b))
#DEBUG
- debug_outer_loop(inverted)
+ #debug_outer_loop(inverted)
inverted.each_with_index do |line, line_index|
# Determine max and min of a and b shapes
@@ -69,14 +74,14 @@ def hitbox_check(shape_a, shape_b)
bmax, bmin = calculate_minmax(shape_b, line)
#DEBUG
- debug_inner_loop(shape_a, shape_b, line_index, amax, amin, bmax, bmin)
+ #debug_inner_loop(shape_a, shape_b, line_index, amax, amin, bmax, bmin)
if ((amin <= bmax) && (amin >= bmin)) || ((bmin <= amax) && (bmin >= amin))
#next
else
# The logic should end the calculations early once it detects lack of collision
# however for debug purposes this is disabled
- #return false
+ return false
end
end
true
@@ -125,12 +130,12 @@ def debug_inner_loop(shape_a, shape_b, line_index, amax, amin, bmax, bmin)
if line_index < shape_a.length
DebugLines[line_index].x1 = shape_a[line_index][0]
DebugLines[line_index].y1 = shape_a[line_index][1]
- if shape_a[line_index].nil?
- DebugLines[line_index].x2 = shape_a[line_index + 1][0]
- DebugLines[line_index].y2 = shape_a[line_index + 1][1]
- else
+ if shape_a[line_index + 1].nil?
DebugLines[line_index].x2 = shape_a[0][0]
DebugLines[line_index].y2 = shape_a[0][1]
+ else
+ DebugLines[line_index].x2 = shape_a[line_index + 1][0]
+ DebugLines[line_index].y2 = shape_a[line_index + 1][1]
end
else
DebugLines[line_index].x1 = shape_b[line_index - shape_a.length][0]
@@ -203,8 +208,8 @@ update do
$i %= 5
# Update shape 1 position to mouse
- sB.x = Camera.coordinate_to_worldspace(get(:mouse_x), 0)[0]
- sB.y = Camera.coordinate_to_worldspace(0, get(:mouse_y))[1]
+ sB.x = Camera.coordinate_to_worldspace(get(:mouse_x), 0)[0] - 25
+ sB.y = Camera.coordinate_to_worldspace(0, get(:mouse_y))[1] - 75
# Check hitboxes
a = hitbox_check(
@@ -218,6 +223,12 @@ update do
[sB.x4 + sB.x, sB.y4 + sB.y]]
)
+ if a
+ sB.color = 'red'
+ else
+ sB.color = 'aqua'
+ end
+
#DEBUG
if $i == 0
pp [[sA.x1, sA.y1],