Calculating ranges
Calculating ranges
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.
- TechnoCat
- Inner party member
- Posts: 1612
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: Calculating ranges
If what you're trying to do is find things within a circle around the actor then: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.
Code: Select all
if euclidean_distance < (actor1.radius + actor2.radius) then
print("COLLISISON!")
end
Last edited by TechnoCat on Sat Oct 29, 2011 3:36 pm, edited 1 time in total.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Calculating ranges
Could you explain what do you mean by "ranges in a radius from my actors"? It's impossible to know without more context.
When I write def I mean function.
Re: Calculating ranges
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.
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
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
http://love2d.org/forums/viewtopic.php?f=3&t=2429
-Kazagha
Re: Calculating ranges
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
Here's an 'optimized' version of the circle vs circle algorithm: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.
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
Re: Calculating ranges
Ivan,
This should return true if they are within range of each other correct?
This should return true if they are within range of each other correct?
Re: Calculating ranges
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
Who is online
Users browsing this forum: Google [Bot] and 7 guests