Hardon Collider shapesInRange()

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
vimes
Prole
Posts: 3
Joined: Mon Jun 23, 2014 4:33 pm

Hardon Collider shapesInRange()

Post by vimes »

I have a question about the shapesInRange() function of the Hardon Collider.

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 what I EXPECTED this program to produce
Image

This is was the ACTUAL output
Image
vimes
Prole
Posts: 3
Joined: Mon Jun 23, 2014 4:33 pm

Re: Hardon Collider shapesInRange()

Post by vimes »

I have been doing some experimenting with this one.
It appears that this is being affected by the Cell Size argument that is passed into Collider.new()

I believe that this value defines the characteristics of the spacial hash that the Collider uses
and that it defines the size of a cell, in pixels, used for course grain object proximity analysis

In my previous post I used a value of 300 and the white band of 'hits' does appear to be approximately 300 wide
(the screen size is 800 x 600)

I tried reducing the Cell Size argument to 100 and generated image A below
I also tried using the default by not including the argument.
The result was the same as Image A, thus I assume that 100 is the default value.

Next I reduced Cell Size further to 10, the diameter of the ball object, which generated image B

I understand from the documentation that Cell Size is a Tuning setting and that there is not a one size fits all value for it.
However, I would like to know if setting Cell Size to such is low number IS what I should be doing to make this work?
or is there something else that I am overlooking?

Image A: Cell Size set to 100
Image

Image B: Cell Size set to 10
Image
caldur
Prole
Posts: 20
Joined: Tue Mar 13, 2012 3:30 pm

Re: Hardon Collider shapesInRange()

Post by caldur »

Well just add HC._hash:draw() to the beginning of your love.draw() and you will see why...

shapesInRange() works as follows: it iterates through all the spatial hash grids the range rectangle covers and simply collects everything within such grids; therefore unless the size of your objects (and in your case the ball) is very close to the cell size, the results will not be accurate.

The function is meant to provide a quick way of filtering objects rather than generating exact results; to achieve what you want simply insert your test area as a new rectangle via HC:addRectangle() and set flags/collect objects through the appropriate callback functions.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 7 guests