CPU-light collision detection!

BlitzMax Forums/BlitzMax Programming/CPU-light collision detection!

Matt McFarland(Posted 2006) [#1]
Hello,

I'm currently using a rect collision detection system which doesnt really work to well considering the shapes of the objects involved!

Is the standard blitzmax collision system (Imagecollide) CPU intensive?

Would it be better to use an oval collision detection system?

I'm sorta lost atm, not sure which to use best.


FlameDuck(Posted 2006) [#2]
Is the standard blitzmax collision system (Imagecollide) CPU intensive?
Compared to what? It more intensive than overlapping rectanges, but also more accurate. Unless you absolutely must check collisions between thousands of objects at once, I wouldn't sweat it.

Would it be better to use an oval collision detection system?
That depends on whether the shapes of the objects involved are mostly oval or not.

BlitzMAX's built-in collision system is pixel accurate.


Matt McFarland(Posted 2006) [#3]
pixel accurate with loads of cpu usage in comparison to oval or rect right?


JazzieB(Posted 2006) [#4]
If you have a lot flying around on screen, use a rect collision system first and then use pixel perfect collision detection methods when there is a rect collision.


ImaginaryHuman(Posted 2006) [#5]
You might consider breaking the rectangle for a given image down into a grid of smaller rectangles, then you can do away with any rectangles that don't contain pixels. The smaller the grid the better. Then you can use the simple rectangular collision checks. ... maybe keep a large one for a bounding box, then if something is inside it check for more detail.


MrHanson(Posted 2006) [#6]
Here is a method I use in my gameobject class:
I first do a radius collision first (without using square roots of course) then use the CollideImage function. Seems to work pretty well. Even runs smoothly on a PII 400 Mhz without slowdown. I have about 20-30 objects on screen at one time.




Method hasCollided(obj:gameobject)
Local xdiff:Float=Abs(worldx-obj.worldx)
Local ydiff:Float=Abs(worldy-obj.worldy)
If ((xdiff*xdiff)+(ydiff*ydiff)) > ((radius*radius)+ (obj.radius*obj.radius))
Return 0
EndIf
SetRotation(angle)
CollideImage (getImage(),worldx,worldy,0,0,1)
SetRotation(obj.angle)
If CollideImage(obj.getImage(),obj.worldx,obj.worldy,0,1,0)
Return 1
EndIf
Return 0
EndMethod


FlameDuck(Posted 2006) [#7]
loads of cpu usage
Define "loads of CPU usage"? You're not very specific. If you have less than 1000 colliding objects on screen at any given time, then built-in collision detection is fine, even on the TabletPC. Don't optimize unless you have to, you'll put yourself in an early grave.


TartanTangerine (was Indiepath)(Posted 2006) [#8]
Why don't you construct a convex hull. Check for collisions against that rather than doing pixel stuff.





Beaker(Posted 2006) [#9]
Some of those look like concave hulls!!


skn3(Posted 2006) [#10]
i was just about to say that. haha


Matt McFarland(Posted 2006) [#11]
First I'd like to thank those who have replied tot his post!
Well, common sense seems to tell me that checking per-pixel collisions would be CPU intensive, especially on a P2 or P3. I'm trying to make sure my game will play well on slower machines (because its shareware) and I really want to make sure that I dont do anything too CPU intensive.

@ FlameDuck: I'm leary of your judgement when you say 1000's of objects, because really, your evidence is only in existance on your uber machine. Even your tablet PC seems pretty powerful in comparison to people that purchase casual games' PCs.

@ IndiePath, that looks harder than what I'm already doing! Would be pretty difficult (for me atleast) to implement a vector library just for collision checks. Although the Vectors look really cool, I just dont think that would really "fit" - Thanks though!

@ MrHanson that looks like a solid plan! I think I'll give that one a try. Thanks!


Tom Darby(Posted 2006) [#12]
Matt, FlameDuck didn't say that BMX's built-in collision detection system would handle "thousands of objects"--quite the opposite. He said "under 1000 objects" (in bold, no less) and while I haven't tested his assertion, I'm inclined to believe it is accurate. Assuming you're using decent algorithms, under 1000 seems pretty reasonable.

If you're looking for a simple system, then I strongly recommend JazzieB's earlier recommendation of using a bounding box/pixel perfect combination system.

Your typical PII system is going to be 6-8 years old at this point--beyond ancient. While there are still some alive and kicking out there, it's going to be a pretty small percentage of total systems available, and those that do still run will be running on hardware that is well past the typical MTBF for consumer-grade stuff. You may want to consider setting your base expectations a little higher, for your own sake...


tonyg(Posted 2006) [#13]
If you have a HUGE number of images you might want to port this...
shifted grid


smilertoo(Posted 2006) [#14]
Ive found the bmax collision system peforms very badly once youre over about 30-40 objects.


Matt McFarland(Posted 2006) [#15]
Thank you John Devoy, I was really thinking that would make a little more sense.. Anyways, I think I'm going to use the standard rect collision detection and then once that happens I'll do the bmax collision detection. Thanks everyone!!!