I had been using the Collider to generate collision callbacks in a space shooter game
I have had no problems with accurately detecting collisions between objects
I recently tried to add a function to determine if a portion of my screen is void of collidable objects
I attempted to do this using HC:shapesInRange().
When i did this i got some weird results.
I was getting shapes from this shapesInRange() that were clearly NOT within my bounding box
I wrote a stand alone example of this scenario to try to figure out what I am doing wrong
I was able to reproduce the results, and demonstrate them graphically but I still not sure why it is doing what it is doing.
Can someone please take a look and provide a corrective suggestion?
Code: Select all
--[[
Test for the function HC:shapesInRange()
--]]
Collider = require 'hardoncollider'
function love.load()
screenMaxX, screenMaxY = love.window.getDimensions()
x1 = screenMaxX/2-25
y1 = screenMaxY/2-30
width = 80
height = 90
x2 = x1 + width
y2 = y1 + height
ball = {}
ball.radius = 5
ball.x = 0
ball.y = 0
HC = Collider.new(300)
ball.colliderShape = HC:addCircle( ball.x, ball.y, ball.radius )
hits = {}
numHits = 0
end
function love.update()
-- Move the ball forward one radius length
ball.x = ball.x + ball.radius
if ball.x > screenMaxX then --wrap to the next row
ball.x = 0
if ball.y == screenMaxY then
ball.y = 0
else
ball.y = ball.y + ball.radius
end
end
-- Update the balls collider shape position
ball.colliderShape:moveTo(ball.x, ball.y)
-- Determine if the ball is within the target box
for shape in pairs(HC:shapesInRange(x1, y1, x2, y2)) do
numHits = numHits + 1
hits[numHits] = {x = ball.x, y = ball.y}
end
end
function love.draw()
-- Draw all of the hits that have been detected by the HC:shapesInRange() function
love.graphics.setColor( 255, 255, 255 )
for key, shape in pairs(hits) do
love.graphics.circle("fill", shape.x, shape.y, ball.radius)
end
-- Draw the target box
-- This box SHOULD be the same as the area being watched by our calls to shapesInRange()
-- Note: we would like to see all of the hits land within this box
love.graphics.setColor( 255, 0, 0 )
love.graphics.rectangle( "line", x1, y1, x2-x1, y2-y1 )
-- Draw the ball as it moves across the screen
love.graphics.circle("line", ball.x, ball.y, ball.radius)
end
This is was the ACTUAL output