Collisions?

Monkey Forums/Monkey Programming/Collisions?

Amon(Posted 2012) [#1]
What is everyone else using for collisions?


Tri|Ga|De(Posted 2012) [#2]
fantomEngine works like a charm.
And a lot of other stuff.


Amon(Posted 2012) [#3]
A non framework method would be nice!


therevills(Posted 2012) [#4]
Mostly rect collisions via hitboxes and ray casting for fast moving objects.


Amon(Posted 2012) [#5]
Example?


therevills(Posted 2012) [#6]
Check out Diddy's platformer example :P

http://code.google.com/p/diddy/source/browse/#svn%2Ftrunk%2Fexamples%2FPlatformer


Amon(Posted 2012) [#7]
Have no idea wots goin on Phil!


NoOdle(Posted 2012) [#8]
I've just implemented SAT Separating Axis Theorem in monkey. Works for all convex shapes including circles. It was fairly easy to implement and is really quick due to the algorithms early exit. I've also implemented AABB Axis Aligned Bounding Box and Orientated Bounding Box OBB for intersection tests. I'll be posting the code soon when I have cleaned it up and made some better examples showing how to use it. I will also make it completely framework independent to allow for easier integration into your projects.

There is also Box2D, this can be used for collision detection, you don't have to use the physics if you don't wan't to.


therevills(Posted 2012) [#9]
Heres a really quick ray cast example:



Nobuyuki(Posted 2012) [#10]
AABB collision is pretty straightforward to implement. If you want a very simple concept of collision to use, circle-to-circle collision is easy. If the distance between 2 circles is less than their combined radius lengths, then you can assume they're colliding. It's not the fastest thing in the world but it's easy to implement and can be explained in one sentence.

You'll need some faster stuff later on, so look into AABB collision. There are examples for polygon, ray-cast, and pixel-mask collision in the code examples, too, but they're a bit more complicated and cost more per frame to perform checks with.


Samah(Posted 2012) [#11]
@Nobuyuki: It's not the fastest thing in the world but it's easy to implement and can be explained in one sentence.

How is that not fast?
[monkeycode]dx = x1-x2
dy = y1-y2
r = r1+r2
If dx*dx+dy*dy <= r*r Then ' collided[/monkeycode]


Amon(Posted 2012) [#12]
How come after more than a year we still don't have an official solution regarding collisions for Monkey?

I know 3rd party methods do the job fine but an official solution would be better.


Raz(Posted 2012) [#13]
I think collisions are too subjective to be included as a "thing" within Monkey.


Amon(Posted 2012) [#14]
Well, similar collisions to blitzmax would be welcome.


therevills(Posted 2012) [#15]
IMO there are too many different collision routines to be able to add them to Monkey. Use the right tool for the right job.

For general use rectangles overlap and circle-to-circle collisions should be fine for "most" games, and they are not hard to use and are very quick.


Amon(Posted 2012) [#16]
Without using a framework like diddy or some other, how would I proceed to having these 2 recommended collision routines placed in a module that can be used much like AutoFit or the delta timer?

Erm! Yes! I need everyone to do it for me because I am lazy. :)


therevills(Posted 2012) [#17]
2 recommended collision routines

I guess you are talking about rectangles overlap and circle-to-circle(?), these are two very simple functions:

[monkeycode]Function RectsOverlap:Bool(x1#, y1#, w1#, h1#, x2#, y2#, w2#, h2#)
If x1 > (x2 + w2) Or (x1 + w1) < x2 Then Return False
If y1 > (y2 + h2) Or (y1 + h1) < y2 Then Return False
Return True
End[/monkeycode]

[monkeycode]Function CircleOverlap:Bool(x1#, y1#, r1#, x2#, y2#, r2#)
dx = x1-x2
dy = y1-y2
r = r1+r2
If dx*dx+dy*dy <= r*r Then ' collided
Return True
End
Return False
End
[/monkeycode]


Amon(Posted 2012) [#18]
Thanks Dude! Just tested this and it works a treat! :)


therevills(Posted 2012) [#19]
And if you want you can do Circle-to-Rects too:

[monkeycode]Function CircleRectsOverlap:Bool(x1:Float, y1:Float, w1:Float, h1:Float, cx:Float, cy:Float, r:Float)
Local testX:Float = cx
Local testY:Float = cy
If testX < x1 Then testX = x1
If testX > (x1 + w1) Then testX = (x1 + w1)
If testY < y1 Then testY = y1
If testY > (y1 + h1) Then testY = (y1 + h1)
Return ((cx-testX) * (cx-testX) + (cy-testY) * (cy-testY)) < r * r
End
[/monkeycode]


Amon(Posted 2012) [#20]
Lovely! Thanks Dude!


Nobuyuki(Posted 2012) [#21]
@Samah: Oh, oops. I guess I could be doing a comparison without a square root operation, huh?


Samah(Posted 2012) [#22]
@Nobuyuki Oh, oops. I guess I could be doing a comparison without a square root operation, huh?

Sqrt is eeeeeeeeevil! ;-)