CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2004
    Posts
    48

    [MFC] How to make a ball bounce off from an object?

    How would i make the ball bounce off from the object (bitmap) in MFC? (look at the picture below)
    How would i set the boundaries and stuff?
    Does anyone have the code?
    I really really need help....i need to complete my project soon.

    just in case you want to know, the bitmap size is 40 x 40.
    don't know if it matters though.
    and remember, i want the ball to bounce off from all four sides of the bitmap, and it would just repeat this process over and over.
    Attached Images Attached Images  
    Last edited by hkboy1019; April 27th, 2004 at 09:12 PM.

  2. #2
    Join Date
    Jan 2004
    Posts
    48
    bump

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640
    Assuming you have objects for ( a ) the ball, and ( b ) the obstacles; all you need to do is check that the two are not intersecting after you've moved them.

    Some pseudo code:
    Code:
    - Check ball bounds for obstacle intersection
    -  If something intersects, check which side of ball
      -  Change ball direction based on side hit
    - Move ball
    While this is a simple solution; I don't have any actual code offhand...

    Viggy

  4. #4
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    The attachment in this thread might provide some insight.

    TDM

  5. #5
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Hey !! That thread was started by the same person

  6. #6
    Join Date
    Feb 2002
    Posts
    4,640
    HAHAHAHAHAHAHA! That's funny! I didn't realize that!!!



    Viggy

  7. #7
    Join Date
    Jan 2004
    Location
    Hong Kong
    Posts
    17

    bouncing ball project file

    The project file contain the program of bouncing ball. This require DirectX SDK to compile. But you can run the executable inside the release folder. And i written this program just for fun

    The program segment you may interest is
    Code:
    void CBallDlg::OnTimer(UINT nIDEvent) 
    {
    	
    	if(nIDEvent==1){
    		if(curX+circleWidth>=windowRect.right){
    			offsetx=-offsetx;
    			m_pEdgeSound->Play();
    		}
    		if(curX<=0){
    			offsetx=-offsetx;
    			m_pEdgeSound->Play();
    		}
    		if(curY+circleWidth>=windowRect.bottom){
    			offsety=-offsety;
    			m_pEdgeSound->Play();
    		}
    		if(curY<=0){
    			offsety=-offsety;
    			m_pEdgeSound->Play();
    		}
    		
    		if(curY+circleWidth==curBarRect.top||curY==curBarRect.bottom){
    			if(curX>=curBarRect.left&&curX<=curBarRect.right){
    				offsety=-offsety;
    				m_pBarSound->Play();
    			}
    		}
    		curX+=offsetx;
    		curY+=offsety;
    		//curX=rand()%windowRect.right;
    		//curY=rand()%windowRect.bottom;
    		m_pMemDC->FillSolidRect(&windowRect,RGB(0,0,0));
    		m_pMemDC->Ellipse(curX,curY,curX+circleWidth,curY+circleWidth);
    		m_pMemDC->FillSolidRect(&curBarRect,RGB(0,255,0));
    		sprintf(mess,"X: %d\nY: %d",curX,curY);
    		//m_status.SetWindowText("X:\nY:");
    		m_status.SetWindowText(mess);
    		Invalidate(NULL);
    	}
    	CDialog::OnTimer(nIDEvent);
    }
    Attached Files Attached Files

  8. #8
    Join Date
    Jan 2004
    Posts
    48
    Originally posted by MrViggy
    HAHAHAHAHAHAHA! That's funny! I didn't realize that!!!



    Viggy
    yeah, i started that thread.
    i solved that problem (in the other thread)...so now i could click anywhere in the four areas and the ball will move to different directions!

  9. #9
    Join Date
    Jan 2004
    Posts
    48

    Re: bouncing ball project file

    Originally posted by patricksky
    The project file contain the program of bouncing ball. This require DirectX SDK to compile. But you can run the executable inside the release folder. And i written this program just for fun

    The program segment you may interest is
    Code:
    void CBallDlg::OnTimer(UINT nIDEvent) 
    {
    	
    	if(nIDEvent==1){
    		if(curX+circleWidth>=windowRect.right){
    			offsetx=-offsetx;
    			m_pEdgeSound->Play();
    		}
    		if(curX<=0){
    			offsetx=-offsetx;
    			m_pEdgeSound->Play();
    		}
    		if(curY+circleWidth>=windowRect.bottom){
    			offsety=-offsety;
    			m_pEdgeSound->Play();
    		}
    		if(curY<=0){
    			offsety=-offsety;
    			m_pEdgeSound->Play();
    		}
    		
    		if(curY+circleWidth==curBarRect.top||curY==curBarRect.bottom){
    			if(curX>=curBarRect.left&&curX<=curBarRect.right){
    				offsety=-offsety;
    				m_pBarSound->Play();
    			}
    		}
    		curX+=offsetx;
    		curY+=offsety;
    		//curX=rand()%windowRect.right;
    		//curY=rand()%windowRect.bottom;
    		m_pMemDC->FillSolidRect(&windowRect,RGB(0,0,0));
    		m_pMemDC->Ellipse(curX,curY,curX+circleWidth,curY+circleWidth);
    		m_pMemDC->FillSolidRect(&curBarRect,RGB(0,255,0));
    		sprintf(mess,"X: %d\nY: %d",curX,curY);
    		//m_status.SetWindowText("X:\nY:");
    		m_status.SetWindowText(mess);
    		Invalidate(NULL);
    	}
    	CDialog::OnTimer(nIDEvent);
    }
    oh thank you for the code....i'll try it.
    i am from hong kong too wor.

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

Featured