+ 1

Collision detection made more simple?

When I try to program collision detection in games, I always end up doing it in quite a cumbersome way. Consider the following code: //Up } else if(mouseX > 60 && mouseX < 100 && mouseY > 360 && mouseY < 400 && this.y > wall.y + wall.h || mouseX > 60 && mouseX < 100 && mouseY > 360 && mouseY < 400 && this.y < wall.y || mouseX > 60 && mouseX < 100 && mouseY > 360 && mouseY < 400 && this.x < wall.x - 20 || mouseX > 60 && mouseX < 100 && mouseY > 360 && mouseY < 400 && this.x > wall.x + wall.w) { this.y -= 5; That is my code for making sure my player doesn’t move through a wall object when pressing the up key(!). This I have to repeat for every key (oh gawd). There’s got to be a simpler, less verbose way to do this, right?

15th Apr 2019, 7:50 AM
Truls Nilsen
Truls Nilsen - avatar
6 Respuestas
+ 7
Truls Nilsen Hey, your collision detection is not good :p Check for basic aabb collision detection :)
15th Apr 2019, 9:13 AM
r8w9
r8w9 - avatar
+ 7
Truls Nilsen sorry, ofc aabb is not suitable here :) Due to no connection error i have to post there one of my game that uses collision detection you might be interrested in :) Function is on line 1178 - 1219. Actual checking and calling the function is on line 262 - 291. https://code.sololearn.com/W3NVJIt4FTrJ/?ref=app
15th Apr 2019, 11:42 AM
r8w9
r8w9 - avatar
+ 1
I see your point, however I’m trying to stop the «player» from moving through objects, meaning that there has to be separate cases for each direction, right? If I do AABB basic, then i can execute some code if there’s an intersection. But there’s 4 different types of intersections here, based on which direction the player moves in.
15th Apr 2019, 10:11 AM
Truls Nilsen
Truls Nilsen - avatar
+ 1
So it is like an abstract function, (since it returns true) and then you just implement it elsewhere? Interesting.
15th Apr 2019, 1:15 PM
Truls Nilsen
Truls Nilsen - avatar
+ 1
I finally figured it out! Please check out my code web p5 js to see how i implemented it. Also, let me know if it could have been done easier
17th Apr 2019, 4:50 AM
Truls Nilsen
Truls Nilsen - avatar