•

Check out our Community Blogs

# RikRoy

Member Since 09 Feb 2014
Offline Last Active Mar 16 2017 10:59 AM

### In Topic: Javascript Is/If Collision Functions (My Functions)

15 March 2017 - 06:11 AM

First, it is a bit confusing whether x1, y1, x2, y2  are  the  and Y components of center of the object or of its upper-left corner. Also if the w1, h1, w2, h2 are absolute co-ordinates or are the length from the upper-left corner as different platforms and programmers prefer to use them in different ways. So, you should mention that in your further tutorials

Next up, the function are a bit defective I think because the if conditions are not satisfied unless the first object is halfway through the second object (I may have misinterpreted it and in that case, please correct me). I think a better version of the code would be:

```function collisionDown(x1,y1,w1,h1,x2,y2,w2,h2){ //Only for objects2 below object1
if( ((x1+w1)>x2) && (x1<(x2+w2)) && ((y1+h1)>y2) && (y1<y2) ){
return true;
}else{
return false;
}
}

function collisionRight(x1,y1,w1,h1,x2,y2,w2,h2){ //Only for objects2 on right of object1
if( ((y1+h1)>y2) && (y1<(y2+h2)) && ((x1+w1)>x2) && (x1<x2) ){
return true;
}else{
return false;
}
}
```

where x1, y1, x2, y2 are upper-left co-ordinates of the objects and w1, h1, w2, h2 are the relative heights and widths, that is the distance of the bottom right point to the top-left point

if there are any mistakes in my code, please point out. I've come back in the game after very long. Please forgive any mistakes

and

no offense!

Cheerio

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download