#1
  1. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12

    Simple bouncing box around window (collision)


    I have a window, and a box, I'm using C, and GDI. These are the rect of both:
    Code:
    	RECT wndrect, boxrect; 
    
    	wndrect.bottom = WINHEIGHT; //300
    	wndrect.top = 0; 
    	wndrect.right = WINWIDTH; //500
    	wndrect.left = 0; 
    	
    	boxrect.bottom = boxrect.top+30; 
    	boxrect.right = boxrect.left+30; 
    	boxrect.left = 0; 
    	boxrect.top = 0;
    How would I go about just detecting if the box hit the sides of the window? and after that I'll go into making it bounce back and whatnot.
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    if( boxrect.left < 1 )
    then direction is move right...
    if( boxrect.right > WINWIDTH - 1)
    then directions is move left
    if( boxrect.top < 1)
    then ..
    if( boxrect.bottom > WINHEIGHT - 1)
    then ..
    something like that souund right? that's what i did for a screensaver i was messing around with if i remember correctly.
  4. #3
  5. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    I don't know, I have a 30x30 box in a window, the window itself is 300x500 (LxW), I want the box to detect if it touched any of the sides of the window (the box is inside the rect, that's the confusing part). Should I make 4 rects around the box instead?
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    what do u mean the "box is inside the rect?" basically you just want to test if the (x,y) coordinates of your box have reached the (x,y) coordinates of the edge of your window. unless im misunderstanding you... i unearthed some old code,
    Code:
    case WM_TIMER:
    		//check for top bounc
    		if(my_pic.y1 < 10)
    			y_dir = 1;
    
    		//check for bottom bounce
    		if((my_pic.y1 + bm.bmHeight ) > (ypos - 10))
    			y_dir = -1;
    
    		//check for left bounce
    		if( my_pic.x1 < 10)
    			x_dir = 1;
    
    		//check for right bounce
    		if( (my_pic.x1 + bm.bmWidth ) > (xpos - 10))
    			x_dir = -1;
    
    		my_pic.x1 += x_dir;
    		my_pic.y1 += y_dir;
    
    		InvalidateRect(hWnd, 0, 0);
    
    		break;
  8. #5
  9. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    Code:
    		if(x+30 >= wndrect.right) {
    			x2--; 
    			x--; 
    		} else if(x+30 >= wndrect.left) {
    			x2++; 
    			x++; 
    		}
    That "kind of" works, it hits the edge, but it shakes, because it goes back and forth, I know why it's doing it, but how do I fix?
  10. #6
  11. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12

    Re: Simple bouncing box around window (collision)


    Originally posted by movEAX_444
    Code:
    	RECT wndrect, boxrect; 
    
    	wndrect.bottom = WINHEIGHT; //300
    	wndrect.top = 0; 
    	wndrect.right = WINWIDTH; //500
    	wndrect.left = 0; 
    	
    	boxrect.bottom = boxrect.top+30; 
    	boxrect.right = boxrect.left+30; 
    	boxrect.left = 0; 
    	boxrect.top = 0;
    You are setting boxrect.bottom and boxrect.right with variables that are not set, yet.

    The shaking is due to the fact that the box has moved through the bounding area, and even though you have instructed it to reverse direction, in the next step, it is still out of bounds, and the algorithm believes that it has hit the other side, when in fact it is still 'stuck' in the same side, so it reverses direction again. This can repeat forever, causing it to shake back and forth. Your collision algorithm is obviously not proper.

    The basic algorithm is to check to see if the top-left corner of the box (treat it just like a one pixel box) is within the bounding box that has the same top-left corner as the window, but whose bottom-right corner is smaller than the window (30 pixels both ways - the exact size of the box). Check for a collision before making the box move through the bounding area - assume it has moved, and if there is a collision do not make it move. Reverse direction, instead.

    The proper way to handle this, if you want to get fancy, is that you should calculate how far into the bounding area the box would travel if you let it, and make this distance the amount that it reflects off of that edge.

    I hope this clears things up.
  12. #7
  13. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    Ok, I have 2 boxes, 1 is moveable using ASDW or arrow keys. The other sits in the middle of the screen. I am checking if X, Y of top left corner is in the bounding box, also if the bottom right corner, but do I need to do this for all 4 corners? This is my code at the moment, I also put a SS.

    boxLeftx, boxLefty is the x, y of the top left corner.
    boxRightx, boxRighty is the x, y of the bottom left corner.

    Code:
    int init(int x, int y, int boxLeftx, int boxLefty, int boxRightx, int boxRighty) {
    	//return 1 if x,y is in the box. 
    	if((x>=boxLeftx && x<=boxRightx) && (y>=boxLefty && y<=boxRighty))
    		return 1; 
    	return 0; 
    }
    Attached Images
  14. #8
  15. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Originally posted by movEAX_444
    Ok, I have 2 boxes, 1 is moveable using ASDW or arrow keys. The other sits in the middle of the screen. I am checking if X, Y of top left corner is in the bounding box, also if the bottom right corner, but do I need to do this for all 4 corners?
    No. Please read my post above. You need only check one corner. Check to see if the top-left corner of the bouncing box is within a bounding box (not the bounding window) that is the size of the bounding window minus the size of the box you wish to bounce within the window.

    However, if you wish to implement proper collision detection, you need to do more computations to determine which side of the screen the box collided with, and how bad the collision was to determine how far it bounces off of it. But, I don't think you are looking for something this complicated.

    Btw, your code could be optimized a bit like so:
    Code:
    int init(int x, int y, int boxLeftx, int boxLefty, int boxRightx, int boxRighty) {
    	return((x>=boxLeftx && x<=boxRightx) && (y>=boxLefty && y<=boxRighty));
    }

IMN logo majestic logo threadwatch logo seochat tools logo