User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Game Development section within the Software Development category of DaniWeb, a massive community of 361,552 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,101 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Game Development advertiser:
Views: 1042 | Replies: 13
Reply
Join Date: Jul 2006
Location: Deptford, London
Posts: 916
Reputation: MattEvans will become famous soon enough MattEvans will become famous soon enough 
Rep Power: 4
Solved Threads: 45
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

Re: Breakout Ball bouncing physics

  #11  
May 12th, 2008
Where and what are the issues? Do they occur at the 'corners and sides' of the paddle, or at the flat, upwards pointing, part? What exactly doesn't seem right when you test?
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Jan 2008
Posts: 167
Reputation: CoolGamer48 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 5
CoolGamer48 CoolGamer48 is offline Offline
Junior Poster

Re: Breakout Ball bouncing physics

  #12  
May 12th, 2008
Wthe ball hits the side of the paddle, it ussualy just goes through the paddle and starts spazing, and then comes out when I move the paddle away. Bouncing on the top side works well, but I have some issues with bottom bouncing as well (when the ball moves below the paddle it starts moving straight until it moves out from under the paddle).
- Gratias
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 916
Reputation: MattEvans will become famous soon enough MattEvans will become famous soon enough 
Rep Power: 4
Solved Threads: 45
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

Re: Breakout Ball bouncing physics

  #13  
May 16th, 2008
Hi, sorry I didn't reply sooner, been abit busy.

With movement and collision, you have a couple of pitfalls. One is that the speed/velocity of a moving object can exceed the width of objects being tested against, a symptom of this is moving objects 'tunneling through' other objects. This occurs when you use 'static collision testing', which is testing the current position of objects in each iteration as opposed to testing the positions and trajectories of objects. With static tests, two collision tests ( in two frames/iterations ) can report a non-collision even though the trajectory of an object may have passed right through another object.

You can often get away with static tests by increasing the number of iterations and decreasing the maximum velocities involved, this means you can keep the same 'visual speed', but objects are actually moving much less between frames and being collision-tested much more frequently.

Another specific problem with the bounce method you're using is that the ball can be collision-tested as being below paddle.top( ), your code will flip the velocity of the ball; in the next frame, the ball might not escape the collision between paddle.top( ), meaning the ball trajectory gets flipped again, and again, and so on. The ball would indeed 'spaz about' in that case, the ball never actually escapes the top of the paddle.

Two viable fixes:
if(m_position.y+m_height >= paddle->GetY() && m_position.y <= paddle->GetY() + paddle->GetHeight() && m_position.x+m_width/2 > paddle->GetX() && m_position.x+m_width/2 < paddle->GetX() + paddle->GetWidth())
	{
		m_velocity.x += paddle->GetMoveX()*(0.25);
		m_velocity.y *= (-1);
		m_position.y -= [a small number];
	}
This assumes that y=0 is the top of the screen, effect is slightly displacing ( separating ) the ball position from the collision, so that the ball can start moving away without getting stuck in this test. Another solution:
if(m_position.y+m_height >= paddle->GetY() && m_position.y <= paddle->GetY() + paddle->GetHeight() && m_position.x+m_width/2 > paddle->GetX() && m_position.x+m_width/2 < paddle->GetX() + paddle->GetWidth())
	{
		m_velocity.x += paddle->GetMoveX()*(0.25);
		m_velocity.y = fabs( m_velocity.y ) * -1;
	}
( again assuming that negative velocity in Y goes upwards, if not the case, remove the *-1 ). Need to #include <cmath> to get fabs, and fabs just takes the absolute of a floating point number ( i.e. without the sign ). This way, you can always push the ball 'upwards'.

I said two viable solutions.. there's actually three, the last is to use vector math to calculate the reflection against the surface based on the normal of the surface.. equation for which is something like:
velocity2 = ( 2 * surface_normal * surface_normal.dot( velocity1 ) ) - velocity1;
( not tested )
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 916
Reputation: MattEvans will become famous soon enough MattEvans will become famous soon enough 
Rep Power: 4
Solved Threads: 45
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

Re: Breakout Ball bouncing physics

  #14  
May 17th, 2008
Based on a quick test in something I've got running ATM, that last bit of code I posted:
velocity2 = ( 2 * surface_normal * surface_normal.dot( velocity1 ) ) - velocity1;
Should probably be:
velocity2 = velocity1 - ( 2 * surface_normal * surface_normal.dot( velocity1 ) );
!
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Game Development Marketplace
Thread Tools Display Modes

Other Threads in the Game Development Forum

All times are GMT -4. The time now is 2:06 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC