https://chat.openai.com/chat/38aa8d21-4b36-4e76-998e-883037e001c6
Q1: write JavaScript code function for collision of 2 balls
Code provided by ChatGPT on 12 Jan 2023
function checkCollision(ball1, ball2) {
var dx = ball1.x - ball2.x;
var dy = ball1.y - ball2.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < ball1.radius + ball2.radius) {
// collision detected!
return true;
} else {
return false;
}
}
var ball1 = {x: 5, y: 5, radius: 10};
var ball2 = {x: 10, y: 15, radius: 5};
if (checkCollision(ball1, ball2)) {
console.log("Collision detected!");
} else {
console.log("No collision detected.");
}
Q2: write JavaScript code collision of 2 balls for the Evolution
Code provided by ChatGPT on 12 Jan 2023
function evolveCollision(ball1, ball2) {
if (checkCollision(ball1, ball2)) {
// collision detected!
var newBall = {
x: (ball1.x + ball2.x) / 2, // average x position
y: (ball1.y + ball2.y) / 2, // average y position
radius: (ball1.radius + ball2.radius) / 2, // average radius
color: averageColor(ball1.color, ball2.color), // average color
mass: ball1.mass + ball2.mass, // total mass
velocity: averageVelocity(ball1.velocity, ball2.velocity) //
average velocity
}
return newBall;
}
// else no collision
return false;
}
var ball1 = {x: 5, y: 5, radius: 10, color: "red", mass:2, velocity:{x:5,
y:3}};
var ball2 = {x: 10, y: 15, radius: 5, color: "green", mass:3, velocity:{x:7,
y:4}};
var newBall = evolveCollision(ball1, ball2);
if(newBall){
console.log(newBall);
}
var ball1 = {x: 5, y: 5, radius: 10, color: "red", mass:2, velocity:{x:5,
y:3}};
var ball2 = {x: 10, y: 15, radius: 5, color: "green", mass:3, velocity:{x:7,
y:4}};
var newBall = evolveCollision(ball1, ball2);
if(newBall){
console.log(newBall);
}
Q3: write JavaScript code collision of 2 balls with coefficient of restitution
obeying physics equations for the Evolution
function evolveCollision(ball1, ball2, e) {
if (checkCollision(ball1, ball2)) {
// collision detected!
var normal = {
x: ball2.x - ball1.x,
y: ball2.y - ball1.y
}
var distance = Math.sqrt(normal.x * normal.x + normal.y * normal.y);
normal.x = normal.x / distance;
normal.y = normal.y / distance;
var relativeVelocity = {
x: ball2.velocity.x - ball1.velocity.x,
y: ball2.velocity.y - ball1.velocity.y
}
var velocityAlongNormal = relativeVelocity.x * normal.x +
relativeVelocity.y * normal.y;
if (velocityAlongNormal > 0) {
return false; // no collision
}
var impulse = -(1 + e) * velocityAlongNormal;
impulse = impulse / (1 / ball1.mass + 1 / ball2.mass);
var impulseV = {
x: impulse * normal.x,
y: impulse * normal.y
}
ball1.velocity.x -= 1 / ball1.mass * impulseV.x;
ball1.velocity.y -= 1 / ball1.mass * impulseV.y;
ball2.velocity.x += 1 / ball2.mass * impulseV.x;
ball2.velocity.y += 1 / ball2.mass * impulseV.y;
return {ball1: ball1, ball2: ball2};
}
// else no collision
return false;
}
var ball1 = {x: 5, y: 5, radius: 10, color: "red", mass:2, velocity:{x:5,
y:3}};
var ball2 = {x: 10, y: 15, radius: 5, color: "green", mass:3, velocity:{x:7,
y:4}};
var newVelocities = evolveCollision(ball1, ball2, 0.8);
if(newVelocities){
console.log(newVelocities);
}
These function could form part of the code for this simulation, of course i
also used some computational thinking to build the rest of the stuff on
Drag and others by myself.