# Collision Detection - A Jeff Thompson web book

#### dawlane

##### Active Member
CX Code Contributor
Both Cerberus and MonkeyX do not support any built in collision detection. It's up to the programmer to have to write their own.
For those that are having trouble, here's a web book/tutorial by Jeff Thompson. The code is pretty straight forward and should be easy enough for someone with a bit of understanding to port to Cerberus.

The only issue would be the licence. Which if I understand, could be a bit of a problem converting the code into a module for distribution, but not if you write your own module for personal use. I've done my own port of this with addition classes representing the shape objects. If it wasn't for the licence I would have uploaded it.

Last edited:

#### Dubbsta

##### Active Member
Thx havnt seen this before!

#### Phil7

##### Moderator
Thanks. This looks like a really good summary of different collision solutions well explained.
And it is also an example that licencing can really be a pain in the neck.
You can only licence/ have copyright on stuff that has a certain level of creativity in it and this tutorial goes all the way from a nearly no brainer to an advanced level (which still has been invented and used somewhere else before) The problem is, where to draw the line between trivial/widely known stuff including its straight forward implementation and his undoubtably usefull and easy to grasp way of summing it up.

To me it is like: You can make and sell your homemade tomato soup as long as you didn't reallize it is the same recipe as of one of the widely known cooking chefs out there...

#### Rich

##### Active Member
3rd Party Module Dev
3rd Party Tool Dev

My implementation for BOX rectangle/point to rectangle/point collision which Ive used since forever is as follows .
Is it more optimised as it fails faster or is it just the same and doesnt use ANDs?

Cerberus X:
``````Method Collide:Bool(x1:float, y1:float, w1:float, h1:float, x2:float, y2:float, w2:float, h2:float)
If x1+w1 < x2 Then Return False
If x1> x2+w2 Then Return False
If y1+h1 < y2 Then Return False
If y1> y2+h2 Then Return False

Return True
End

' 2 rects colliding
If Collide(50,50,100,100,75,75,200,200)

' mouse collision
If Collide(MouseX(),MouseY(),1,1,100,100,200,200)``````

#### dawlane

##### Active Member
CX Code Contributor
I think it would depend of how the back-end compiler optimises conditional expressions.
The version in the book can be compressed to
Cerberus X:
``````Function Rect2Rect:Bool(rectangle1X:Float, rectangle1Y:Float, rectangle1Width:Float, rectangle1Height:Float, rectangle2X:Float, rectangle2Y:Float, rectangle2Widht:Float, rectangle2Height:Float)
Return (rectangle1X+rectangle1Width >= rectangle2X And rectangle1X <= rectangle2X+rectangle2Widht And rectangle1Y+rectangle1Height >= rectangle2Y And rectangle1Y <= rectangle2Y+rectangle2Height)
End Function``````
You would have to compiler the code to C++ and then to Assembly for a comparison and then count the number of clock-cycles each instruction takes.

#### Rich

##### Active Member
3rd Party Module Dev
3rd Party Tool Dev
You would have to compiler the code to C++ and then to Assembly for a comparison and then count the number of clock-cycles each instruction takes
Not sure i have the time for that ;-) Maybe one day......

#### Phil7

##### Moderator
I think optimisation can be done later. More important IMHO seems to be the naming and the parameters for calling.
When I have some time I will come up with some suggestions. Somewhere in the forums there was a discussion about that.

#### Rich

##### Active Member
3rd Party Module Dev
3rd Party Tool Dev
More important IMHO seems to be the naming and the parameters for calling.
Are you suggesting creating some built in functions for collision into CX?
That'd be nice.

#### Phil7

##### Moderator
Absolutely. I would even prefer to have a built in framework/engine, but that has to be the last one.
For now I'd like to have the building blocks for a framework like collisions, tweening etc.
What do you think could be helpful, without limiting your way of setting things up.
Because that is one issue that comes up when having a built in framework with animations, transitions, scenes ....