## The most useful function

March 26th, 2016 1:39 pm

I find that this is my most used, most useful function: the simple box collision.

function isCol(thex,they, othx, othy, thel, theh, othl, othh){
return !(they+theh <= othy || they >= othy+othh || thex+thel <= othx || thex >= othx+othl);
}

### 2 Responses to “The most useful function”

1. Snowdrama says:

I like this, It took me a bit to realize you were testing to make sure it was NOT colliding and then flipping the response. Normally I would do it backwards, Test each point to see if it was inside the box, so it was twice as long.

This is a Java syntax flavor of the same function with the names slightly altered to be personally more understandable.

public bool isColliding(float x, float y, float w, float h, float x2, float y2, float w2, float h2){
return !(y+h = y2+h2 || x+w = x2+w2);
}

Nice work though! Hope you’re project goes well!

• Snowdrama says:

EDIT: I have no idea why I lost the rest of the code Hmmmm….

public bool isColliding(float x, float y, float w, float h, float x2, float y2, float w2, float h2){
return !(y+h = y2+h2 || x+w = x2+w2);
}

[cache: storing page]