c# - Bounding rectangle collisions - ball jumps through wrong side of paddle -


i'm making pong game, , i've come across problem. when ball (a rectangle) collides racket (or bat) below or above racket, strange bug ball moves rectangle , goes left-right-left-right reaching high speeds(because added acceleration) , jumps out @ opposite side. know why bug happening:

if (ballrec.intersects(player1rec)         && ball.x <= 20         && ball.y + 20 >= player.y         && ball.y <= player.y + 100) //checks front rebound-here's bug {     ball.vx *= -1; //changes x-direction     if (ball.vx < 0)         ball.vx -= 1; //increases x-velocity     effect.play();     if (r.next(4) == 0)     {         if (ball.vy < 0) ball.vy--;         else ball.vy++; //increases y-velocity on special occasion     } } else {     if (ballrec.intersects(player1rec))     {         ball.vy *= -1;         effect.play();     } } 

ball.vy=velocity y-axis: multiply -1 change direction

effect=sound

the bug: make ball rebound @ given location on front of racket, says ball's lower side (that +20) mustn't higher racket's upper side , ball's upper side mustn't lower racket's lower side. because of x coordinates (ball.x<=20, 20=the width of racket), front rebound effect consumes top , bottom side of racket, , rebound there can't work.

when try solve it, best non-complicated solution (because next year i'm starting middle school (14-18 in country) , don't know lot of fancy maths), don't solution (check below).

my solution (which i'm not happy with): lower area required front rebound ball.y>=player.y , ball.y+20<=player.y+100(the length) , , down rebound work, if ball hits corner of racket, same bug appears in case ball moves up-down-up-down.

my question: how fix bug? thank time! hope wasn't long!

current solution (not perfect):

if (ballrec.intersects(player1rec)         && ball.x <= 20         && ball.y >= player.y         && ball.y + 20 <= player.y + 100) {     ball.vx *= -1;     if (ball.vx < 0)         ball.vx -= 1;     effect.play();     if (r.next(4) == 0)     {         if (ball.vy < 0) ball.vy--;         else ball.vy++;     } } else {     if (ballrec.intersects(player1rec))     {         ball.vy *= -1;         effect.play();     } } 

solution 1: check speed vector

one solution take direction of speep vector (ball.vx) account. allow player1 flip balls x speed if speed negative (e.g. moving towards left screen , vice versa second player) if simple pong game, fine:

// player 1 if (ballrec.intersects(player1rec)     && ball.x <= 20     && ball.y >= player.y     && ball.y + 20 <= player.y + 100     && ball.vx <= 0 ) //<--- here { // ..... }  // player 2 if (ballrec.intersects(player2rec)     // ....     && ball.vx >= 0 ) //<--- here { // ..... } 

solution 2: save collision state of ball

another solution save current collision state (colliding or not colliding , flip speed when status switches not colliding colliding):

public class ball {     public bool colliding = false; }  //in update of ball/game bool player1collision = ballrec.intersects(player1rec)     && ball.x <= 20     && ball.y >= player.y     && ball.y + 20 <= player.y + 100; if( player1collision && !ball.colliding ) {     // set state here, reduce issues when there chance different players can overlap     ball.colliding = true;     // ..... }  // same player 2,3,4,5 .....  //update state next frame ball.colliding = player1collision || player2collision /* .... */; 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -