|
[java] Collision detection
HI everybody, I have a somewhat simple question I think.. I have an algorithym that I am using for my collision detection but I recently discovered that it was one of the major hits on my frames per second, if I have alot of objects on the screen and if my objects are relatively large.. I do collision detection every frame and all across the board( meaning if the object is alive I detect a collision for it). Anyway I was wondering if there was some way I could speed it up so I can have more objects and larger ones also.. Here is the code that I use for collision detection. any help would be appreciated thanks in advance!
code:
War doesn''t determine who is right, war determines who is left.
Well, that''s not the speed issue. The real speed issue is checking every particle against every other particle when you are looking for collisions. What you need to do is find a way to limit the number of checks you make, by only comparing against particles you''re close to.
Yeah I was thinking about it and that algo isn''t that intensive.. But any other type of comparing for proximity might as well be the coll dectect routine I have...
War doesn''t determine who is right, war determines who is left.
War doesn''t determine who is right, war determines who is left.
How about doing a rough grid collision detection? I''m working on such solution where you have a grid (its squares are about 46x46 pixels) and I place all the objects there (special ModelGridItem objects storing the needed info). When moving some object, first check is done against the grid squares.
If there is some other object in the same square, then more precise collision detection is done (simply by finding a radius for both objects, then calculating the distance of the objects'' center points, comparing that to summed radiuses...and if the distance is less than the sum, I get a hit).
Bullets/projectiles are not in the grid - as I move bullet, I check from the grid if there is something in the area. Bullets are not colliding with eachother.
Haven''t implemented this totally yet, but so far it looks ok. Performance is not put into hard test yet.
One problem still is there, as most likely there is need to place object into several grid squares (it might be in such place that its boundaries cover several grid squares - if we approximate the object only to a single grid square, we lack the collision detection in the other squares). Either we need to place the object into all grid squares it covers (means 4 checks / object when placing it to grid), or we scan also the neighbouring grid squares when moving object (again adds the check count to even 9).
Also this grid provides relatively easy way to determine what is drawn to screen in case of big game area (that does not fit into screen at once). Haven''t worked on this aspect so no experiences here...
Credit for this goes to javanerd - we''ve had some long discussions about this issue. :-)
If there is some other object in the same square, then more precise collision detection is done (simply by finding a radius for both objects, then calculating the distance of the objects'' center points, comparing that to summed radiuses...and if the distance is less than the sum, I get a hit).
Bullets/projectiles are not in the grid - as I move bullet, I check from the grid if there is something in the area. Bullets are not colliding with eachother.
Haven''t implemented this totally yet, but so far it looks ok. Performance is not put into hard test yet.
One problem still is there, as most likely there is need to place object into several grid squares (it might be in such place that its boundaries cover several grid squares - if we approximate the object only to a single grid square, we lack the collision detection in the other squares). Either we need to place the object into all grid squares it covers (means 4 checks / object when placing it to grid), or we scan also the neighbouring grid squares when moving object (again adds the check count to even 9).
Also this grid provides relatively easy way to determine what is drawn to screen in case of big game area (that does not fit into screen at once). Haven''t worked on this aspect so no experiences here...
Credit for this goes to javanerd - we''ve had some long discussions about this issue. :-)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement