Collision detection without using canvas | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 18

Collision detection without using canvas

Hi guys, I want to make a collision detection without using canvas just with JavaScript (not JQuery or...), Is there anyone who can write a ***simple*** code (functions or algorithms) for that. I haven't written the code yet but for example imagine one shape is moving automatically and you control the second shape movement, then when it collides with the first shape, the color of first or second shape changes. (I just want the collision code) p.s. I found this related questionšŸ‘‡ but the answers are kinda complicated šŸ˜ƒ https://www.sololearn.com/Discuss/1503786/?ref=app

30th Aug 2019, 12:37 PM
Sheida Hedayati
Sheida Hedayati - avatar
15 Respostas
+ 4
https://code.sololearn.com/WDhSn98SlX3b/?ref=app
30th Aug 2019, 2:07 PM
Gordon
Gordon - avatar
+ 15
CalviÕ² šŸ˜Šthank you so much šŸŒ¹
30th Aug 2019, 2:19 PM
Sheida Hedayati
Sheida Hedayati - avatar
+ 14
Gordon thanks alottt I think it's a bit close to my codešŸ˜ŠšŸŒ¹, but why <20 ? and how can we write change color to blue when they hit then when they separate their colors black to their first colors , instead of console.loge collided
30th Aug 2019, 2:24 PM
Sheida Hedayati
Sheida Hedayati - avatar
+ 13
Gordon thanks a bunch šŸ˜Š I wrote style.back... but forgot to write elsešŸ˜šŸŒ·šŸŒ¹
30th Aug 2019, 2:30 PM
Sheida Hedayati
Sheida Hedayati - avatar
+ 12
Cbrāœ”[ ZeroCharisma ] thanks šŸ˜Š I can't do thatšŸ˜ but I'll look at itšŸŒ¹
30th Aug 2019, 1:26 PM
Sheida Hedayati
Sheida Hedayati - avatar
+ 11
Coder yesssšŸ˜ they are genius like youšŸ‘ŒšŸ‘šŸŒ¹
30th Aug 2019, 3:17 PM
Sheida Hedayati
Sheida Hedayati - avatar
+ 10
Anton Bƶhler wow thanks alotšŸŒ¹šŸŒ¹, I should analyze it for myself šŸ˜ƒ
30th Aug 2019, 12:59 PM
Sheida Hedayati
Sheida Hedayati - avatar
+ 10
Mike Perkowski thanks for the new method šŸŒ¹šŸ˜Š
2nd Sep 2019, 6:16 PM
Sheida Hedayati
Sheida Hedayati - avatar
+ 7
Anton Bƶhler If there weren't any circles, does this function work with any shapes? I don't think so, because it has r šŸ˜
30th Aug 2019, 12:48 PM
Sheida Hedayati
Sheida Hedayati - avatar
+ 5
It looks like this has already been adequately answered but I haven't seen any mention of the element.getBoundingClientRect() method. This method returns the left, top, right, bottom, x, y, width and height values in pixels for the element it is called on. It's really useful for locating the bounds and position of an element for collision detection.
2nd Sep 2019, 5:39 PM
Mike Perkowski
Mike Perkowski - avatar
+ 4
collisions don't require the canvas at all, they require math šŸ™Œ collision between two circles: function circleCollision(c1, c2){ return Math.hypot(c1.x - c2.x, c1.y - c2.y) < c1.r + c1.r; } (c1 and c2 are circles with attributes x, y, r)
30th Aug 2019, 12:45 PM
Anton Bƶhler
Anton Bƶhler - avatar
30th Aug 2019, 2:06 PM
CalviÕ²
CalviÕ² - avatar
+ 3
here collisions between circles rectangles and points: https://code.sololearn.com/WMg5JEA2551G/?ref=app
30th Aug 2019, 12:53 PM
Anton Bƶhler
Anton Bƶhler - avatar
+ 2
the formula is the same as the one Anton provided. the unit of my circles are in a viewport unit vmin. in css, i have set them both of width /height 20vmin, so i know that they are of radius 10 vmin each, so the sum is 20. for changing color, just replace the report "collided" in console with setting red.style.background and green.style.background and add an else to handle changing their colors back.
30th Aug 2019, 2:27 PM
Gordon
Gordon - avatar
- 1
Hi
30th Aug 2019, 7:41 PM
Anowar Hossain
Anowar Hossain - avatar