Page 1 of 1
Calculating ranges
Posted: Sat Oct 29, 2011 11:10 am
by jbmannon
Hey I'm working on a project and I'm trying to calculate ranges in a radius from my actors. I'm having difficulty working out what to do. Any advice would be great.
Re: Calculating ranges
Posted: Sat Oct 29, 2011 3:32 pm
by TechnoCat
jbmannon wrote:Hey I'm working on a project and I'm trying to calculate ranges in a radius from my actors. I'm having difficulty working out what to do. Any advice would be great.
If what you're trying to do is find things within a circle around the actor then:
Code: Select all
if euclidean_distance < (actor1.radius + actor2.radius) then
print("COLLISISON!")
end
Re: Calculating ranges
Posted: Sat Oct 29, 2011 3:33 pm
by kikito
Could you explain what do you mean by "ranges in a radius from my actors"? It's impossible to know without more context.
Re: Calculating ranges
Posted: Sat Oct 29, 2011 10:15 pm
by Kazagha
That is good thinking TechnoCat.
You could find the regular distance between two points with the following function(euclidean distance?). Where x1/y1 is one actor, and y1/y2 is the other.
Code: Select all
-- Returns the distance between two points.
function math.dist(x1,y1, x2,y2) return ((x2-x1)^2+(y2-y1)^2)^0.5 end
Taken from Taehl's post on the Share You Utils thread, it's well worth checking out;
http://love2d.org/forums/viewtopic.php?f=3&t=2429
-Kazagha
Re: Calculating ranges
Posted: Mon Oct 31, 2011 11:00 am
by jbmannon
Thank you everyone. Obviously calculating a circle is beyond my skill level. I may be useing Kazagha's bit of code, or more likely something similar.
Re: Calculating ranges
Posted: Mon Oct 31, 2011 11:20 am
by ivan
jbmannon wrote:Thank you everyone. Obviously calculating a circle is beyond my skill level. I may be useing Kazagha's bit of code, or more likely something similar.
Here's an 'optimized' version of the circle vs circle algorithm:
Code: Select all
function circleIntersect ( x1, y1, r1, x2, y2, r2 )
local dx, dy = x1 - x2, y1 - y2
local d = dx * dx + dy * dy -- squared distance
local radii = r1 + r2
return d < ( radii * radii ) -- squared distance vs squared radii sum
end
It avoids ^ and math.sqrt operations which are considered "cpu costly".
Re: Calculating ranges
Posted: Mon Oct 31, 2011 2:04 pm
by jbmannon
Ivan,
This should return true if they are within range of each other correct?
Re: Calculating ranges
Posted: Mon Oct 31, 2011 3:16 pm
by ivan
Yes, where x1, y1 is the position of circle 1, x2, y2 is the position of circle 2 and r1 and r2 are their radii respectively