summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--run.rb92
1 files changed, 39 insertions, 53 deletions
diff --git a/run.rb b/run.rb
index cb9329f..7e28c7b 100644
--- a/run.rb
+++ b/run.rb
@@ -15,12 +15,12 @@ require 'ruby2d/camera'
# https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection
#DEBUG: used to colour all debug lines unique colours
-$colors = ['blue', 'teal', 'green', 'lime',
- 'yellow', 'orange', 'red', 'fuchsia']
+$colors = %w[blue teal green lime
+ yellow orange red fuchsia]
#DEBUG: used to draw, store and update debug lines
class DebugLines
- class <<self
+ class << self
def [](index)
data[index] ||= Camera::Line.new(color: 'red', z: 99, width: 5)
end
@@ -32,24 +32,22 @@ 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,
- x4: 100.0, y4: 100.0}
+svA = { x1: 200.0, y1: 100.0,
+ x2: 200.0, y2: 200.0,
+ x3: 100.0, y3: 200.0,
+ x4: 100.0, y4: 100.0 }
-svB = {x1: 275.0 - 275.0, y1: 175.0 - 175.0,
- x2: 375.0 - 275.0, y2: 225.0 - 175.0,
- x3: 300.0 - 275.0, y3: 350.0 - 175.0,
- x4: 250.0 - 275.0, y4: 250.0 - 175.0}
+svB = { x1: 275.0 - 275.0, y1: 175.0 - 175.0,
+ x2: 375.0 - 275.0, y2: 225.0 - 175.0,
+ x3: 300.0 - 275.0, y3: 350.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',
@@ -58,7 +56,6 @@ 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))
@@ -67,7 +64,6 @@ def hitbox_check(shape_a, shape_b)
debug_outer_loop(inverted)
inverted.each_with_index do |line, line_index|
-
# Determine max and min of a and b shapes
amax, amin = calculate_minmax(shape_a, line)
bmax, bmin = calculate_minmax(shape_b, line)
@@ -75,7 +71,7 @@ def hitbox_check(shape_a, shape_b)
#DEBUG
debug_inner_loop(shape_a, shape_b, line_index, amax, amin, bmax, bmin)
- if (((amin <= bmax) && (amin >= bmin)) || ((bmin <= amax) && (bmin >= amin)))
+ if ((amin <= bmax) && (amin >= bmin)) || ((bmin <= amax) && (bmin >= amin))
#next
else
# The logic should end the calculations early once it detects lack of collision
@@ -90,11 +86,11 @@ end
def build_inverted_edges(shape)
edges = []
shape.each_with_index do |vertex_start, index|
- if index == shape.length - 1
- vertex_end = shape[0]
- else
- vertex_end = shape[index + 1]
- end
+ vertex_end = if index == shape.length - 1
+ shape[0]
+ else
+ shape[index + 1]
+ end
edges.push [vertex_end[1] - vertex_start[1],
-(vertex_end[0] - vertex_start[0])]
end
@@ -102,15 +98,15 @@ def build_inverted_edges(shape)
end
# Dot product
-def vecDotProd(a,b)
- return (a[0] * b[0]) + (a[1] * b[1])
+def vecDotProd(a, b)
+ (a[0] * b[0]) + (a[1] * b[1])
end
# Calculates the minimum point and maximum point projected onto the line
def calculate_minmax(shape, line)
min = vecDotProd(shape.first, line)
max = vecDotProd(shape.first, line)
- shape.each_with_index do |vertex, vertex_index|
+ shape.each_with_index do |vertex, _vertex_index|
dot = vecDotProd(vertex, line)
if dot > max
max = dot
@@ -160,47 +156,38 @@ def debug_inner_loop(shape_a, shape_b, line_index, amax, amin, bmax, bmin)
puts "y2 #{DebugLines[line_index].y2}"
puts "(((#{amin} < #{bmax}) && (#{amin} > #{bmin})) || ((#{bmin} < #{amax}) && (#{bmin} > #{amin})))"
end
- if (((amin <= bmax) && (amin >= bmin)) || ((bmin <= amax) && (bmin >= amin)))
- DebugLines[line_index].color.a = 0.2
- else
- DebugLines[line_index].color.a = 1.0
- end
+ DebugLines[line_index].color.a = if ((amin <= bmax) && (amin >= bmin)) || ((bmin <= amax) && (bmin >= amin))
+ 0.2
+ else
+ 1.0
+ end
#DEBUG: make the debug lines effectively infinitly long
tempx1 = DebugLines[line_index].x1
tempx2 = DebugLines[line_index].x2
tempy1 = DebugLines[line_index].y1
tempy2 = DebugLines[line_index].y2
- DebugLines[line_index].x1 = (tempx1 *(1+1000)/2) + (tempx2 * (1-1000)/2)
- DebugLines[line_index].y1 = (tempy1 *(1+1000)/2) + (tempy2 * (1-1000)/2)
- DebugLines[line_index].x2 = (tempx2 *(1+1000)/2) + (tempx1 * (1-1000)/2)
- DebugLines[line_index].y2 = (tempy2 *(1+1000)/2) + (tempy1 * (1-1000)/2)
+ DebugLines[line_index].x1 = (tempx1 * (1 + 1000) / 2) + (tempx2 * (1 - 1000) / 2)
+ DebugLines[line_index].y1 = (tempy1 * (1 + 1000) / 2) + (tempy2 * (1 - 1000) / 2)
+ DebugLines[line_index].x2 = (tempx2 * (1 + 1000) / 2) + (tempx1 * (1 - 1000) / 2)
+ DebugLines[line_index].y2 = (tempy2 * (1 + 1000) / 2) + (tempy1 * (1 - 1000) / 2)
end
# Displays debug info(used outside the inverted.each loop)
def debug_outer_loop(inverted)
if $i == 0
puts
- puts "debug of inverted edges:"
+ puts 'debug of inverted edges:'
pp inverted
end
end
-
# Move camera
on :key_held do |event|
- if event.key == 'w'
- Camera.y -= 5
- end
- if event.key == 's'
- Camera.y += 5
- end
- if event.key == 'a'
- Camera.x -= 5
- end
- if event.key == 'd'
- Camera.x += 5
- end
+ Camera.y -= 5 if event.key == 'w'
+ Camera.y += 5 if event.key == 's'
+ Camera.x -= 5 if event.key == 'a'
+ Camera.x += 5 if event.key == 'd'
end
# Initialize frame counter
@@ -216,7 +203,7 @@ update do
$i %= 5
# Update shape 1 position to mouse
- sB.x = Camera.coordinate_to_worldspace(get(:mouse_x),0)[0]
+ sB.x = Camera.coordinate_to_worldspace(get(:mouse_x), 0)[0]
sB.y = Camera.coordinate_to_worldspace(0, get(:mouse_y))[1]
# Check hitboxes
@@ -225,10 +212,10 @@ update do
[sA.x2, sA.y2],
[sA.x3, sA.y3],
[sA.x4, sA.y4]],
- [[sB.x1 + sB.x, sB.y1 + sB.y],
- [sB.x2 + sB.x, sB.y2 + sB.y],
- [sB.x3 + sB.x, sB.y3 + sB.y],
- [sB.x4 + sB.x, sB.y4 + sB.y]],
+ [[sB.x1 + sB.x, sB.y1 + sB.y],
+ [sB.x2 + sB.x, sB.y2 + sB.y],
+ [sB.x3 + sB.x, sB.y3 + sB.y],
+ [sB.x4 + sB.x, sB.y4 + sB.y]]
)
#DEBUG
@@ -242,7 +229,6 @@ update do
[sB.x3 + sB.x, sB.y3 + sB.y],
[sB.x4 + sB.x, sB.y4 + sB.y]]
end
-
end
show