Is it possible to make a collision detection without the use of canvas? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 14

Is it possible to make a collision detection without the use of canvas?

I'm planning to make a game (CSS) which contains a collision detector. I am seeing codes with it but it has canvas. But I am going to sketch the i.e cars with the use of CSS.

15th Sep 2018, 10:42 AM
Email Not Activated
9 Answers
+ 20
Not exactly conventional...but take a look at this code 😅 function in line 112 (checkHits()) checks collision between two divs https://code.sololearn.com/WDthrSVTYxzd/?ref=app
15th Sep 2018, 1:21 PM
Burey
Burey - avatar
+ 11
if you want to check collision between 2 rectangles, the algorithm is something like this... if(rect1.x < rect2.x + rect2.w && rect1.x + rect1.w > rect2.x && rect1.y < rect2.y + rect2.h && rect1.y + rect1.h > rect2.y) //collision detected here the h is for height and w for width
15th Sep 2018, 10:56 AM
Md. Nafis Ul Haque Shifat
Md. Nafis Ul Haque Shifat - avatar
16th Sep 2018, 1:31 AM
Amethyst Animion
Amethyst Animion - avatar
+ 7
Ehab Samir Thanks man!
15th Sep 2018, 10:41 PM
Email Not Activated
+ 7
It's done with the "element.getBoundingClientRect()" method: https://code.sololearn.com/Wq4Y40ULsJQ3/?ref=app
16th Sep 2018, 8:28 AM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
+ 6
Md. Nafis Ul Haque Shifat How about the x and y? Is that a built-in syntax?
15th Sep 2018, 11:01 AM
Email Not Activated
+ 6
There are multiple ways to get the co-ordinates, like using offsetLeft and offsetTop
15th Sep 2018, 11:16 AM
Md. Nafis Ul Haque Shifat
Md. Nafis Ul Haque Shifat - avatar
+ 6
Good question 💖💕. Thank you for your information 🙌🤗
15th Sep 2018, 11:38 PM
NimWing Yuan
NimWing Yuan - avatar
+ 5
https://code.sololearn.com/W1m4HcvBiN8b/?ref=app look at my code here for moving ball that detects four corners around the screen and apply collision rules .. I didn't need a canvas
15th Sep 2018, 9:34 PM
Ehab Samir
Ehab Samir - avatar