A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Hit test between block and a ball.

  1. #1
    Junior Member
    Join Date
    Apr 2015
    Posts
    4

    Hit test between block and a ball.

    Hey, new user here, i am currently having issues rebounding a ball off a block correctly.

    for example the ball would need to hit the top, bottom and sides and rebound off them accordingly.

    help would be much appreciated.

    ps. if it makes any difference, the centre point of my objects are currently top left, (default position when using starling)

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Without any code of file perhaps take a look here and get what you need http://www.flashgametuts.com/tutoria...in-as3-part-2/

  3. #3
    Junior Member
    Join Date
    Apr 2015
    Posts
    4
    Quote Originally Posted by fruitbeard View Post
    Hi,

    Without any code of file perhaps take a look here and get what you need http://www.flashgametuts.com/tutoria...in-as3-part-2/


    Hello fruitbeard, thanks for the reply.

    This is the code i have come up with so far:

    if (ball.intersects(block)) {


    if((ball.y - (ball.height/2)) < (block.y - block.height/2) && yDirection > 0 && (ball.x < block.x + (block.width/2) + 10) && (ball.x > block.x - (block.width/2) - 10)){//bottom edge of ball, top edge of block
    ball.y = ball.y - 5;
    yDirection*= -1;

    }else if((ball.y - (ball.height/2)) > (block.y - block.height/2) && yDirection < 0 && (ball.x < block.x + (block.width/2) + 10) && (ball.x > block.x - (block.width/2) - 10)){//top edge of ball, bottom edge of block
    ball.y = ball.y + 5;
    yDirection*= -1;

    }else if(ball.x > block.x){
    ball.x = ball.x + 15;
    }else{
    ball.x = ball.x - 15;
    }



    }


    This somewhat works however not fully, if the ball is bouncing above or beneath the right side of the block but not hitting the sides, it gets pushed to the sides as if it hit the right side, again the registration point is top left, i cannot change this as i have hard coded the coordinates of the objects with that registration point, changing it to the centre would require too much re-coding, also this is the sizes for the ball and block if that would help:

    ball.width = 13.70;
    ball.height = 13.70;

    block.width = 65.70;
    block.height = 31.60;

  4. #4
    Junior Member
    Join Date
    Apr 2015
    Posts
    4
    Hello fruitbeard, thanks for the reply.

    This is the code i have come up with so far:

    if (ball.intersects(block)) {


    if((ball.y - (ball.height/2)) < (block.y - block.height/2) && yDirection > 0 && (ball.x < block.x + (block.width/2) + 10) && (ball.x > block.x - (block.width/2) - 10)){//bottom edge of ball, top edge of block
    ball.y = ball.y - 5;
    yDirection*= -1;

    }else if((ball.y - (ball.height/2)) > (block.y - block.height/2) && yDirection < 0 && (ball.x < block.x + (block.width/2) + 10) && (ball.x > block.x - (block.width/2) - 10)){//top edge of ball, bottom edge of block
    ball.y = ball.y + 5;
    yDirection*= -1;

    }else if(ball.x > block.x){
    ball.x = ball.x + 15;
    }else{
    ball.x = ball.x - 15;
    }



    }


    This somewhat works however not fully, if the ball is bouncing above or beneath the right side of the block but not hitting the sides, it gets pushed to the sides as if it hit the right side, again the registration point is top left, i cannot change this as i have hard coded the coordinates of the objects with that registration point, changing it to the centre would require too much re-coding, also this is the sizes for the ball and block if that would help:

    ball.width = 13.70;
    ball.height = 13.70;

    block.width = 65.70;
    block.height = 31.60;

  5. #5
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Try the page I linked to

  6. #6
    Junior Member
    Join Date
    Apr 2015
    Posts
    4
    Hello fruitbeard,

    i have tried the page you linked, that was actually one of the first tutorials i looked at, however it is not what i am looking for, what i am trying to replicate is four different hits from one object, i have got the stage rebound fine, its the block itself, i can have the ball rebounding from the bottom and top sides of the block fine, its the left and right sides of the block i am having issues with, i have posted some code of what i currently have, help will be much appreciated and donation will be made.

    Thanks

  7. #7
    Junior Member
    Join Date
    Apr 2015
    Location
    Mexico
    Posts
    1
    I want that treat also.! Hehe

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center