For collision detection, I can either find out whether or not 2 sprites are in the same rectangular areas (up to 4, since they can be on the edge) at the same time before doing collision detection, or simply check every sprite on the entire map every time. Can anyone tell me which they think would be optimum, or rather at what point (as in after how every many collision detections need to take place) does this start saving speed? Also which is faster == or >,<,<=, and >=?
How many sprites do you have?
I don’t think, that you will recognise any “speedboost”, for example there’s no difference in the “running-time of one loop” of my game if I just have to check 1 or 500 collisions.
It might be worth it, if you got huge polygons and therefore a lot of verticles to check.
What you could do is creating a circle around each sprite and check before if they collide (circle vs circle collision is faster then rect vs rect), but I in my opinion that’s not worth it, too.
best regards
There are many threads on jgo about “not looping through the whole array everytime”
here is one: http://www.java-gaming.org/topics/avoiding-looping-through-everything/24587/view.html
One thing you can use is a quad tree.
Grids are kinda nicer I think and to quote Riven
Click on the quote link to get to a whole thread about this kinda topic
But overall it really depends what you are doing.
For example if you are doing collision detection with static geometry, you can just set a very limited loop in that area.
I think that not even at assembly level, that there is a difference - talking about this in Java is krazy, with a K =D
Oughh… I’d be careful with that. It gives you an incredible performance boost.
(I assume we’re talking about SAT collision testing now)
In case you have lots of polygons, especially polygons with lots of vertices (in my case I had ‘only’ 8), circle pre-checks can improve the performance a lot. They’re pretty much eqivalent to one single, simplified SAT axis test + two multiplications
Also, if you think of using pre-checks I’d really suggest circles instead of rectangles.
Rectangles have the problem, that you usually need to re-compute them if you for example rotate the polygon.
Circles are circular, therefore you don’t need to re-compute the radius, as it is the same all the time
But I agree to Cero: Using some kind of grid will improve the performance a lot as well (after that comes the circle-check).
If we’re not talking about SAT, but AABB collision tests, then you shouldn’t really need to do the circle check, but the space partitioning (Grids, etc, see Cero’s post) should help you.
I don’t know which is the fastest from == >, <, <=, >=.
My guess is, it’s ==. (As far as I know it needs a subtraction and another instruction for testing whether something is 0, but that actually only applies for native code. Java is mostly interpreted, even if everything is JIT-ted. Idk whats the case then.).
I read over that thread a bit, I love the idea of checking within a set rectangular grid, but it becomes a lot more complicated when you try to deal with sprites along the edges of any rectangle in the grid, because a single sprite can be in up to 4 at once (if its in the midddle of them)…
Compares cost are equal. Everything else mentioned…it pretty much depends.