Calculating ranges

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
jbmannon
Prole
Posts: 6
Joined: Tue Nov 09, 2010 11:55 pm

Calculating ranges

Post 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.
User avatar
TechnoCat
Inner party member
Posts: 1612
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: Calculating ranges

Post 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
Last edited by TechnoCat on Sat Oct 29, 2011 3:36 pm, edited 1 time in total.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Calculating ranges

Post 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.
When I write def I mean function.
User avatar
Kazagha
Prole
Posts: 40
Joined: Tue Oct 04, 2011 7:05 am

Re: Calculating ranges

Post 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
jbmannon
Prole
Posts: 6
Joined: Tue Nov 09, 2010 11:55 pm

Re: Calculating ranges

Post 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.
User avatar
ivan
Party member
Posts: 1918
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Calculating ranges

Post 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".
jbmannon
Prole
Posts: 6
Joined: Tue Nov 09, 2010 11:55 pm

Re: Calculating ranges

Post by jbmannon »

Ivan,

This should return true if they are within range of each other correct?
User avatar
ivan
Party member
Posts: 1918
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Calculating ranges

Post 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
Post Reply

Who is online

Users browsing this forum: Google [Bot], lrdprdx and 9 guests