Ball to Ball collision handling
-
hi, I have looked all over and can't find something that suits my needs... so here goes... I am creating a snooker game in HTML 5 canvas and JavaScript and have now came to the part of resolving the ball to ball collisions. So far I have this, which is great, it detects the cue ball hitting another ball:
function isColliding(ball){
var distance = Math.sqrt(((this.posX - ball.posX)*(this.posX - ball.posX)) + ((this.posY - ball.posY) * (this.posY - ball.posY)));if (distance < this.radius + ball.radius){
//balls have collided now work out where they collidedvar collisionPointX = ((this.posX * ball.radius) + (ball.posX * this.radius)) / (this.radius + ball.radius);
var collisionPointY = ((this.posY * ball.radius) + (ball.posY * this.radius)) / (this.radius + ball.radius);
ball.speed = this.speed;
this.directionX = Math.cos(?);
this.directionY = Math.sin(?);
ball.directionX = Math.cos(?);
ball.directionY = Math.sin(?);
}
}
However I need to know how do I work out the Math.cos and Math.sin for the balls to move in a direction that is expected (using elastic collision), when they collide? Thanks for help in advance.. Steve
-
hi, I have looked all over and can't find something that suits my needs... so here goes... I am creating a snooker game in HTML 5 canvas and JavaScript and have now came to the part of resolving the ball to ball collisions. So far I have this, which is great, it detects the cue ball hitting another ball:
function isColliding(ball){
var distance = Math.sqrt(((this.posX - ball.posX)*(this.posX - ball.posX)) + ((this.posY - ball.posY) * (this.posY - ball.posY)));if (distance < this.radius + ball.radius){
//balls have collided now work out where they collidedvar collisionPointX = ((this.posX * ball.radius) + (ball.posX * this.radius)) / (this.radius + ball.radius);
var collisionPointY = ((this.posY * ball.radius) + (ball.posY * this.radius)) / (this.radius + ball.radius);
ball.speed = this.speed;
this.directionX = Math.cos(?);
this.directionY = Math.sin(?);
ball.directionX = Math.cos(?);
ball.directionY = Math.sin(?);
}
}
However I need to know how do I work out the Math.cos and Math.sin for the balls to move in a direction that is expected (using elastic collision), when they collide? Thanks for help in advance.. Steve
I think you should have a look at the three top articles in the search below: http://www.codeproject.com/search.aspx?q=snooker&x=0&y=0&sbo=kw[^] In any case you should use vector calculus to calculate the angles as well as the elsatic collition. The Angles would be dependent on the incoming vector and the deviation should be from this line of reference. I know that the code is written in C# but I think you should get the understanding of the physics.
-
I think you should have a look at the three top articles in the search below: http://www.codeproject.com/search.aspx?q=snooker&x=0&y=0&sbo=kw[^] In any case you should use vector calculus to calculate the angles as well as the elsatic collition. The Angles would be dependent on the incoming vector and the deviation should be from this line of reference. I know that the code is written in C# but I think you should get the understanding of the physics.
There are physics engines already implemented in JS Physics Engine comparison :)
"It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.
-
There are physics engines already implemented in JS Physics Engine comparison :)
"It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.
Did not know that. :)
-
hi, I have looked all over and can't find something that suits my needs... so here goes... I am creating a snooker game in HTML 5 canvas and JavaScript and have now came to the part of resolving the ball to ball collisions. So far I have this, which is great, it detects the cue ball hitting another ball:
function isColliding(ball){
var distance = Math.sqrt(((this.posX - ball.posX)*(this.posX - ball.posX)) + ((this.posY - ball.posY) * (this.posY - ball.posY)));if (distance < this.radius + ball.radius){
//balls have collided now work out where they collidedvar collisionPointX = ((this.posX * ball.radius) + (ball.posX * this.radius)) / (this.radius + ball.radius);
var collisionPointY = ((this.posY * ball.radius) + (ball.posY * this.radius)) / (this.radius + ball.radius);
ball.speed = this.speed;
this.directionX = Math.cos(?);
this.directionY = Math.sin(?);
ball.directionX = Math.cos(?);
ball.directionY = Math.sin(?);
}
}
However I need to know how do I work out the Math.cos and Math.sin for the balls to move in a direction that is expected (using elastic collision), when they collide? Thanks for help in advance.. Steve