0)){
ball1x = ball1.x = -ball1.radius + canvas.width/2
ball1.velocity.x = -e * ball1.velocity.x;
collisionCounter = collisionCounter + 1 // only need these 2 lines for ball1 collisiion with the left wall
}
if ((ball2.x - ball2.radius + canvas.width/2<0)){
ball2x = ball2.x = ball2.radius - canvas.width/2
ball2.velocity.x = -e * ball2.velocity.x;
collisionCounter = collisionCounter + 1
}
if ((ball2.x + ball2.radius - canvas.width/2>0)){
ball2x = ball2.x = -ball2.radius + canvas.width/2
console.log("hit")
ball2.velocity.x = -e * ball2.velocity.x;
collisionCounter = collisionCounter + 1
}
if ((ball1.y - ball1.radius + canvas.height/2<0)){
ball1y = ball1.y = ball1.radius - canvas.height/2
ball1.velocity.y = -e * ball1.velocity.y;
collisionCounter = collisionCounter + 1
}
if ((ball1.y + ball1.radius - canvas.height/2>0)){
ball1y = ball1.y = -ball1.radius + canvas.height/2
ball1.velocity.y = -e * ball1.velocity.y;
collisionCounter = collisionCounter + 1
}
if ((ball2.y - ball2.radius + canvas.height/2<0)){
ball2y = ball2.y = ball2.radius - canvas.height/2
ball2.velocity.y = -e * ball2.velocity.y;
collisionCounter = collisionCounter + 1
}
if ((ball2.y + ball2.radius - canvas.height/2>0)){
ball2y = ball2.y = -ball2.radius + canvas.height/2
ball2.velocity.y = -e * ball2.velocity.y;
collisionCounter = collisionCounter + 1
}
// to prevent accidental overlap of balls
//sync ejss variables to chatgpt's
ball1.x = ball1x
ball1.y = ball1y
ball2.x = ball2x
ball2.y = ball2y
// prevent overlap ball 1 overlap ball2
var dx = ball1.x - ball2.x;
var dy = ball1.y - ball2.y;
var distance = Math.sqrt(dx * dx + dy * dy);
var tol = 1.001
var tol2 = tol *1.0001
if (distance < ball1.radius*tol + ball2.radius*tol ) {
var angle = Math.atan2(dy, dx);
console.log (angle)
var targetX = ball2x + Math.cos(angle) * (ball1.radius + ball2.radius)*tol2;
var targetY = ball2y + Math.sin(angle) * (ball1.radius + ball2.radius)*tol2;
ball1x = ball1.x = targetX;
ball1y = ball1.y = targetY;
}
/*
// prevent overlap ball 2 overlap ball 1 when Evolution is running
var dx = ball1.x - ball2.x;
var dy = ball1.y - ball2.y;
var distance = Math.sqrt(dx * dx + dy * dy);
var tol = 1.001
var tol2 = tol *1.0001
if (distance < ball1.radius*tol + ball2.radius*tol ) {
var angle = Math.atan2(dy, dx);
console.log (angle)
var targetX = ball1x - Math.cos(angle) * (ball1.radius + ball2.radius)*tol2;
var targetY = ball1y - Math.sin(angle) * (ball1.radius + ball2.radius)*tol2;
ball2x = ball2.x = targetX;
ball2y = ball2.y = targetY;
}
*/
]]>
{
// console.log(voice.name, voice.lang)
//})
//debug
// Queue this utterance.
window.speechSynthesis.speak(msg);
}
]]>
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;
}
/*
more output from ChatGPT
In this example, ball1 and ball2 are objects with properties including x, y, radius, color, mass and velocity properties that represent the position, radius, color, mass, and velocity of the balls. The function uses the checkCollision() function to check for a collision and, when collision is detected, it updates the velocity of the balls according to the physics equations and the coefficient of restitution (e). The coefficient of restitution represents the elasticity of collision, value of 'e' ranges from 0 to 1, means 0 means the collision is completely inelastic and 1 means it is completely elastic.
You can call this function in your main code and test with 2 balls positions, radius and coefficient of restitution, here is an example
more code of which i combine into the EJSS variables
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);
}
*/
]]>
"digit dynamics," tends to be around 314. This intriguing result is related to the mathematical constant π (pi).
The setup involves two blocks of significantly different masses (here, 1 kg and 10000 kg) sliding on a frictionless surface. One block starts moving towards the other, and they collide elastically. The number of collisions before they come to rest or one block moves away is proportional to the digits of π.
For two blocks with masses in the ratio 1:100n, the number of collisions will be approximately equal to the first n+1 digits of π. In this case, with n = 3 (since 10000 = 1002), the number of collisions is close to the first 3 digits of π, which is 314.
This remarkable connection arises from the way the collisions and velocities behave, involving iterative calculations that mimic the series expansion of π.
Since this is a computationally intensive during the collisions between left wall and 2 balls, perhaps slowing down the velocities of ball2 in x direction to -0.0001 might allow to compute the correct number of collisions before it becomes unstable? Good luck! and let me know if it works! Enjoy!
]]>