I need to detect if a object is hiting a obstacle from X axis or Y axis. The motion works with a Vector that stores 2 variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

I need to detect if a object is hiting a obstacle from X axis or Y axis. The motion works with a Vector that stores 2 variables

the object moves in both axis x and y, but i need to figure if it hits the goal coming mostly from X OR Y, so it triggers different reactions. vector(x,y) -x is left to right x is right to left -y is top to bottom y is bottom to top

9th Jan 2017, 3:59 PM
Igor Busquets
Igor Busquets - avatar
6 Answers
+ 2
don't know about the framework you are using, but if its a game framework it will have collisions by itself, wich you pass 2 objects. If not, you must check object1 x position with object2 x position (and Y also). Try to check a few numbers less or more, because the x or y represent usually the center, and not the borders of the object/graphics.
9th Jan 2017, 4:04 PM
Nahuel
Nahuel - avatar
+ 2
im using p5.js which is based on Processing. the thing is, it doesn't have a collision class, i succeeded in detecting colision but cant figure out how to give different reactions from different angles of collision, i think i need to write a algorithm for it
9th Jan 2017, 4:07 PM
Igor Busquets
Igor Busquets - avatar
+ 1
if u can, and have the time, check my code, blubot, thanks a lot
9th Jan 2017, 4:08 PM
Igor Busquets
Igor Busquets - avatar
+ 1
thanks uran, that i got working, but how do i figure where is the object coming from?
9th Jan 2017, 4:11 PM
Igor Busquets
Igor Busquets - avatar
0
you can do something like this if (object1.x == (object2.x - object2.width/2) && object1.y == (object2.y - object2.height/2)) { //Collision happens. }
9th Jan 2017, 4:09 PM
Uran Kajtazaj
Uran Kajtazaj - avatar
0
try something like this from the sides if ((object2.x - object2.width/2) == (object1.x + object1.width/2)) { // from the left } else if ((object2.x + object2.width/2) == (object1.x - object1.width/2)) { // from the right }
9th Jan 2017, 4:18 PM
Uran Kajtazaj
Uran Kajtazaj - avatar