Reputation: 1
I'm trying to make a pong game in javascript for a final project, but the hit detection doesn't work for my left paddles. the right paddle will work but the left doesn't. If i tweak the greater than less than values the ball has a seizure.
I really don't know how to fix this. please help!
//hit detection right paddle
if((ballinfo.x + ballinfo.size) <=(boxtwo.x)){
if (ballinfo.y > boxtwo.y){
if((ballinfo.y + ballinfo.size) <= (boxtwo.y + boxtwo.height)){
ballinfo.velocity.x *= -1;
console.log("collision");
}
}
}
//hit detection left paddle
if((ballinfo.x + ballinfo.size) <=(box.x)){
if (ballinfo.y < box.y){
if((ballinfo.y + ballinfo.size) <= (box.y + box.height)){
ballinfo.velocity.x *= -1;
console.log("collisionleft");
}
}
}
ballinfo.x += ballinfo.velocity.x;
ballinfo.y += ballinfo.velocity.y;
Upvotes: 0
Views: 63
Reputation: 11
It looks like the wrong comparison operator was inverted between your right and left. Try changing the first 2 compare operators in the left section to see if that helps.
if((ballinfo.x + ballinfo.size) >=(box.x)){
if (ballinfo.y > box.y){
I'm assuming the checking on y is for the paddle, so the code there should be identical between the left and right paddles (replacing the object name, of course).
Upvotes: 1