showing results for - "javascript collision detection"
Mahe
17 Sep 2016
1if (rect1.x < rect2.x + rect2.width &&
2   rect1.x + rect1.width > rect2.x &&
3   rect1.y < rect2.y + rect2.height &&
4   rect1.y + rect1.height > rect2.y) {
5    // collision detected!
6}
Lucia
13 Oct 2018
1function checkCollisions(x1, y1, w1, h1, x2, y2, w2, h2){
2	if (x1 + w1 >= x2 && x1 + w1 <= x2 + w2 && y1 + h1 >= y2 && y1 + h1 <= y2 + h2) {
3		return true;
4	} else if (x1 >= x2 && x1 <= x2 + w2 && y1 >= y2 && y1 <= y2 + h2) {
5		return true;
6	} else {
7		return false;
8	}
9}
Brenton
23 Jun 2018
1/*
2  You should call the function resolveCollsion, when a collision between two
3  circles is detected.
4*/
5
6function rotate(velocity, angle) {
7    const rotatedVelocities = {
8        x: velocity.x * Math.cos(angle) - velocity.y * Math.sin(angle),
9        y: velocity.x * Math.sin(angle) + velocity.y * Math.cos(angle)
10    }
11
12    return rotatedVelocities;
13}
14
15function resolveCollision(particle, otherParticle) {
16  	/*
17    	the particles passed as parameters should be objects with
18       	x and y position,
19        a velocity object with x and y values,
20        a mass value
21        as attributes
22        example of a class:
23        
24        function Circle() {
25    		this.x = x;
26    		this.y = y;
27    		this.velocity = {
28            	x: 5,
29                y: 5
30            };
31    		this.mass = 2;
32
33		}
34    */
35    const xVelocityDiff = particle.velocity.x - otherParticle.velocity.x;
36    const yVelocityDiff = particle.velocity.y - otherParticle.velocity.y;
37
38    const xDist = otherParticle.x - particle.x;
39    const yDist = otherParticle.y - particle.y;
40
41    if (xVelocityDiff * xDist + yVelocityDiff * yDist >= 0) {
42        const angle = -Math.atan2(otherParticle.y - particle.y, otherParticle.x - particle.x);
43
44        const m1 = particle.mass;
45        const m2 = otherParticle.mass;
46
47        const u1 = rotate(particle.velocity, angle);
48        const u2 = rotate(otherParticle.velocity, angle);
49
50        const v1 = { x: u1.x * (m1 - m2) / (m1 + m2) + u2.x * 2 * m2 / (m1 + m2), y: u1.y };
51        const v2 = { x: u2.x * (m1 - m2) / (m1 + m2) + u1.x * 2 * m2 / (m1 + m2), y: u2.y };
52
53        const vFinal1 = rotate(v1, -angle);
54        const vFinal2 = rotate(v2, -angle);
55
56        particle.velocity.x = vFinal1.x;
57        particle.velocity.y = vFinal1.y;
58
59        otherParticle.velocity.x = vFinal2.x;
60        otherParticle.velocity.y = vFinal2.y;
61    }
62}
63
64/*
65	source:
66    https://youtu.be/789weryntzM
67*/
queries leading to this page
what is collisions in javascriptfunction detect collisioncanvas collisiondetecting collision in javascripthtml collision detection detect elements collision jsjs square collisionjs collision detectioncollision detection in jshow to know if colliding js canvashow to detect collision in javascriptjavasript element collisionjavascrip collision detectionhow make a collision detection in javascripthtml javascript detect collisioncollision detection in canvasrectangle collision detection2d collision in javascriptcheck collision matter jsjavascript collision detection codehow to code collision detection in jscollision function javascriptrun function if collision detectedrectangle formula collidingcollision detection game in javascriptjavascript check element collision detectionmdn 2d collisionsdetect collision javascriptjavascript collision detection bounding box efficientfastest rect vs rect collisiion responsehow does object collision work in jsjavascript game collision2d mdn collisioncollision detection javascriptadd collision in a circlejavascript collision detection libraryjavascript collision detection between circle and trianglehow to check for cirular collision in jscollision script javascriptjavascript detect collision divhow to hit detection in javascriptcollision detectionjavascript box collisiondetect collision in javascriptcollision circle javascriptjs game collision detectionhow to make collision detection in javascripthtml 2fjavascript detect collisionimplemetn collision in javascriptchecking collision in javscript circlehow to detect collision vanilla javascriptcollision detection jshow to create collision detectioncollison javascriptfast javascript collision detectionhow to write collide function in javascriptcorrect position on collision jscollisions in javascriptcollision detection javascript codeefficient collision detection 2d jsjavascript collision detection tutorialjavascript calculate circle collision2d collision detection with imagesobject collision javascriptdetect collisionjavascript rect circle collisioncollision code javascriptget colliding element in html using jsjs circle collision2d collision mdncoding 2d rectangle collisionscollision detection js 5djs 2d collision detectionjs div collision detectionalgorithm for collider in javascripthow collisions in javascript workwhat is object collision in javascriptjavascript detect collisionhow to create collisions in javascriptjavascript simple collisioncircle collision detection javascriptjava script or js circle collisioncollision detection jshit detection in html with javascripthow to check collision javascriptrectangle collision detection pseudocodejavascript circle collision detectionhtml elements collision detectionhow to make an object detect if it is touching another object in html canvassquare collision javascriptjava scipt hitdetectienhow to connect the collision detection to the elements in javascriphit detection javascripton collision in javascriptjavascript collision detection circlecollision javascriptjs canvas collision detectionhow to handle collision in game in javascriptcheck collision javascripta collision detection function in javascriptjavascipt collision detectionchek if object collide in jsjavascript circle collision detection formulahow to make collision resolution in javascriptcollision detection function jscollision rectangle jsjavascript game code for collision detectionjavascript hitboxcollide javascriptcollision element jsjavascript collsioncollision detection algorithm in javascriptcollision in jsjs code for collision of two moving circlecircle rect collision javascriptjavascript game collision detectionjavascropt collision detectionbasic collision detection jshow to make a collision functioncolision jsjavascript check collisionjavascript collision detectionrect circle collision jsjavascript calculate new angle on circle collisioncollision detection code javascriptformula to detec collisionscircle line collision javascriptimplement object collision in javascriptcircle and line collision jshow to make an object have collision jsyou 27ll modify a function to implement the 2d collision detection algorithm for circles found at https 3a 2f 2fdeveloper mozilla org 2fen us 2fdocs 2fgames 2ftechniques 2f2d collision detection 23circle collision 28note 3a that code is javascript 2c so you 27ll nhow to separate two colliding objects jsjavascript collision detectcollisions js examplejavascript collisionformula to detect collisionscollicion function jsjs collision detection line2d collision javascripthow to make a collision function in javascriptcollision detection library jsjavascript collision detextionhow do you check for collision in javascriptcircle circle collision javascriptcollision detection in javascripthow to get collisionshow does collision work in jsjavascript 2d collision detectioncolission boxes jsjavascript div collision detectioncollision detection rectanglefunction collision javascriptjs canvas collision recthow to detect where two rectangles are colliding javascriptjs collisionscircle collision javascripthow to make a simple collision with 2 rectanglescollision in javascripthow to check for collision with a specific side of a square jscollision in a 2d plane c 2b 2b problemhow to make collision in javascriptdetect collisions javascriptjavascript gaming collision detection xycircle collision detection jshitboxes javascripthow to do square collisionscollisiion control jsobject collision line circle jscollision detection in javascript tutorialhow to make a collision detector jshow to hitdetectie in java scriptbounding box collision detection javascriptcollision jscollision jsjs collisionjavascript how to create a collision checkobject collision detection codecircle collision jsjs collision detection librarycollision detect javascriptcheck for collision 2djavascript collision detection