Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

java script or js circle collision

if (distance < circle1.radius + circle2.radius) {
    // Collision detected!
}
Comment

circle collision javascript

/*
  You should call the function resolveCollsion, when a collision between two
  circles is detected.
*/

function rotate(velocity, angle) {
    const rotatedVelocities = {
        x: velocity.x * Math.cos(angle) - velocity.y * Math.sin(angle),
        y: velocity.x * Math.sin(angle) + velocity.y * Math.cos(angle)
    }

    return rotatedVelocities;
}

function resolveCollision(particle, otherParticle) {
  	/*
    	the particles passed as parameters should be objects with
       	x and y position,
        a velocity object with x and y values,
        a mass value
        as attributes
        example of a class:
        
        function Circle() {
    		this.x = x;
    		this.y = y;
    		this.velocity = {
            	x: 5,
                y: 5
            };
    		this.mass = 2;

		}
    */
    const xVelocityDiff = particle.velocity.x - otherParticle.velocity.x;
    const yVelocityDiff = particle.velocity.y - otherParticle.velocity.y;

    const xDist = otherParticle.x - particle.x;
    const yDist = otherParticle.y - particle.y;

    if (xVelocityDiff * xDist + yVelocityDiff * yDist >= 0) {
        const angle = -Math.atan2(otherParticle.y - particle.y, otherParticle.x - particle.x);

        const m1 = particle.mass;
        const m2 = otherParticle.mass;

        const u1 = rotate(particle.velocity, angle);
        const u2 = rotate(otherParticle.velocity, angle);

        const v1 = { x: u1.x * (m1 - m2) / (m1 + m2) + u2.x * 2 * m2 / (m1 + m2), y: u1.y };
        const v2 = { x: u2.x * (m1 - m2) / (m1 + m2) + u1.x * 2 * m2 / (m1 + m2), y: u2.y };

        const vFinal1 = rotate(v1, -angle);
        const vFinal2 = rotate(v2, -angle);

        particle.velocity.x = vFinal1.x;
        particle.velocity.y = vFinal1.y;

        otherParticle.velocity.x = vFinal2.x;
        otherParticle.velocity.y = vFinal2.y;
    }
}

/*
	source:
    https://youtu.be/789weryntzM
*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: public static void main(dsjjsdds, jdnjd, jsndjsd, isjdjsd, sjdijs, skjdks_+) __ osakd___ +++ 
Javascript :: json api data fetch error 
Javascript :: mock anonymous function jest 
Javascript :: double click sur image change javascript 
Javascript :: read more/less button with smoth expand 
Javascript :: "npm supertest 
Javascript :: ejs include 
Python :: jupyter notebook warning off 
Python :: minecraft 
Python :: matplotlib change thickness of line 
Python :: python today - 1 day 
Python :: how to make a resizable pygame window 
Python :: how to install OpenCV? 
Python :: legend size matplotlib 
Python :: how remove name of index pandas 
Python :: how to print error in try except python 
Python :: selenium python maximize window 
Python :: warning ignore python 
Python :: install serial python 
Python :: how to add text in python turtle 
Python :: selenium press tab python 
Python :: how to get file name without extension in python 
Python :: invert y axis python 
Python :: flask minimul app 
Python :: renaming headers pandasd 
Python :: python everything after last slash 
Python :: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details. 
Python :: show image in tkinter pillow 
Python :: selenium driver wait python 
Python :: opencv draw a point 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =