I was trying my best to learn about collision, ( http://love2d.org/forum/viewtopic.php?f ... lide#p8490 ) but how can I formulate colision suitable for circles? As you can tell I have no intention of using love.physics.
TIA
Collision with CIRCLES?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Collision with CIRCLES?
Well, you could check for the distance from the center of the circle and see if it's less than the radius.
Where x1, y1 is the center of the circle and x2, y2 is the point you are checking. If the result is less than the radius then the point is inside the circle.
Don't know how fast this is in practice though.
Code: Select all
function distanceFrom(x1,y1,x2,y2)
return math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
end
Don't know how fast this is in practice though.
- nevon
- Commander of the Circuloids
- Posts: 938
- Joined: Thu Feb 14, 2008 8:25 pm
- Location: Stockholm, Sweden
- Contact:
Re: Collision with CIRCLES?
Fast enough, unless you have like a bazillion circles, or really, really tiny ones.Jasoco wrote:Don't know how fast this is in practice though.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Collision with CIRCLES?
If you want to optimize the number of checks, you could always try reducing the number of objects checked by using a QuadTree... just give me a couple days so I can test the query() method.
When I write def I mean function.
Re: Collision with CIRCLES?
You can also square the whole equation and thus save the square-root. You will only benefit by a quadtree, if you do lot's off collision tests in a frame or do other stuff with it - the thing needs time to build itself.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Collision with CIRCLES?
You only benefit from that if radii are constant (even more if all circles have the same radius).vrld wrote:You can also square the whole equation and thus save the square-root.
Code: Select all
circle = {x = 0, y = 100, r = 10}
circle.r2 = circle.r^2
Help us help you: attach a .love.
Re: Collision with CIRCLES?
A square root takes more time to compute than computing a multiplication, so it would save time even if it was done at every timestep.Robin wrote:You only benefit from that if radii are constant (even more if all circles have the same radius).
Code: Select all
> sqrt = math.sqrt
> pow = math.pow
> s = os.clock() for i=1,1000000 do t = sqrt(i) end print(string.format("sqrt: %f ms", (os.clock() - s)*1000))
sqrt: 130.000000 ms
> s = os.clock() for i=1,1000000 do t = pow(i,2) end print(string.format("pow: %f ms", (os.clock() - s)*1000))
pow: 150.000000 ms
> s = os.clock() for i=1,1000000 do t = i^2 end print(string.format("^: %f ms", (os.clock() - s)*1000))
^: 70.000000 ms
> s = os.clock() for i=1,1000000 do t = i*i end print(string.format("mul: %f ms", (os.clock() - s)*1000))
mul: 50.000000 ms
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Collision with CIRCLES?
Making sqrt and pow locals shaves about 50 ms off, but yeah, i*i is still definitely the fastest.
Help us help you: attach a .love.
- rhezalouis
- Party member
- Posts: 100
- Joined: Mon Dec 07, 2009 10:27 am
- Location: Indonesia
- Contact:
Re: Collision with CIRCLES?
per 10000 iteration * 100 sample,
average exec time = 4921.17µs [control]
average exec time = 1874.78µs [approx. 62%faster]
average exec time = 976µs [approx. 80%faster]
I used the application posted on the topic [RLC]"Regarding Time Measurement" and simply average it using MsExel. Is there something wrong with the data collection method?
Code: Select all
local b = math.sqrt;
a = b(2);
Code: Select all
a = math.sqrt(2);
Code: Select all
a = 2^0.5;
I used the application posted on the topic [RLC]"Regarding Time Measurement" and simply average it using MsExel. Is there something wrong with the data collection method?
Last edited by rhezalouis on Thu Apr 29, 2010 3:40 pm, edited 2 times in total.
Aargh, I am wasting my posts! My citizenshiiiip...
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Collision with CIRCLES?
How does the outer code look like?
A few things to consider:
A few things to consider:
- If the local b ... part was inside the loop, it is logical it is slower: it gives something like GETGLOBAL, GETDOTTED, SETLOCAL, GETLOCAL, CALL in byte code rather than GETGLOBAL, GETDOTTED, CALL. There should be an improvement in performance if you move the local b = math.sqrt outside the loop.
- I'm not sure about this, but the Lua compiler might optimize constant expressions like 2^0.5. Change it to x^0.5 (where x is defined earlier to be a local with value 2) to see if it changes the outcome.
Help us help you: attach a .love.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests