Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are some points that could be optimized:</p> <p>You could calculate ball collision with just the radius. Distance between balls = sqrt((x1-x2)^2 + (y1-y2)^2), now if distance between balls &lt; (radius1 + radius2) then they collide.</p> <p>To calculate that efficiently you should keep distance squared (no sqrt) and compare with (radius1 + radius2)^2. That way might be faster than what you currently do.</p> <p>If you have n Balls you currently have to do like n^2 comparisons which gets quite a lot for many balls. So that would be a good point to look for optimizations:</p> <p>There are really advanced "spatial data structures" (google that) that would allow you to compare Balls with less then all Balls (just with balls close enough so it's likely that they collide). The problematic thing with those is that you have to maintain that structure (update it whenever your balls move) which is usually quite a lot of calculations and might end up slower that your approach. </p> <p>Example: <a href="http://www.gamerendering.com/2008/11/17/uniform-grid/" rel="nofollow">http://www.gamerendering.com/2008/11/17/uniform-grid/</a> You take a grid with cellsize = the max diameter a ball can have. Whenever you move a ball you update it's position in the grid (that's a quite simple divide by gridwidth). Then you just need to compare a Ball with Balls inside this or directly adjacent cells.</p> <blockquote> <p>I've tried using an ArrayList but that is even worse. Is there something I can do to speed up this code? perhaps another data structure? It seems like a really long time to me. Should I try reduce the number of member variables in Ball?</p> </blockquote> <p>There is pretty much nothing you can do by using different classes/members. Your Ball[] array with public members is the most efficient way to do that. The amount of members of Ball should have no effect on the speed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload