Jump to content
• Advertisement

Public Group

# Breakout Paddle Question

This topic is 3041 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hello right now I am starting a breakout and was wondering how the ball should bounce off the paddle.

I know how to make the ball bounce off the walls

Vec2d tempVec = _ballPos + _ballVel;		if(tempVec.y + BALL_RADIUS > FIELD_TOP){			_ballVel.y *= -1;		}		if(tempVec.y < FIELD_BOTTOM){			_ballVel.y *= -1;		}		if(tempVec.x + BALL_RADIUS > FIELD_RIGHT){			_ballVel.x *= -1;		}		if(tempVec.x < FIELD_LEFT){			_ballVel.x *= -1;		}

but when it comes to the paddle I am not so sure. I got the following formula in google. The problem is I don't know how to define the paddle's velocity since the paddle's x position is simply wherever the mouse is.

//on collision				float speed = _ballVel.length();				_ballVel.x += _paddleVel.x * SOME_CONSTANT;				_ballVel.y *= -1;				_ballVel.normalize();				_ballVel *= speed;

Thanks

#### Share this post

##### Share on other sites
Advertisement
You can get the velocity by recording the changes in the mouse. The previous velocity could be as simple as newX - oldX, but you might want to save a sample of N positions and use the average to smooth out massive changes.

#### Share this post

##### Share on other sites
So it would be something as simple as

int x = gameEngine->get_input()->GetMouseX();		int x_vel = (x - PADDLE_LENGTH/2) - _paddlePos.x;		_paddleVel.x = x_vel;				_paddlePos.x = x - PADDLE_LENGTH/2;

It doesn't seem to change the direction of the ball, maybe it's too instantaneous.

#### Share this post

##### Share on other sites
I think that is why SOME_CONSTANT" is included, its for tweaking. The basic formula just makes the reaction proportional to the speed of the paddle.

#### Share this post

##### Share on other sites
Thanks, yeah setting SOME_CONSTANT to .5 made the results a little better. It's just kind of weird still, sometimes the ball will just go horizontal and stay like that from side to side.

I've been having trouble with other aspects of the physics as well. As I mentioned, sometimes the ball will just end up horizontal and go like that forever.
Sometimes the ball will just melt through the walls, destroy them, without stopping/bouncing off.

Setting paddle position.
//x = mouse x position//_lastX is used to determine velocity later on.	//SET VELOCITY 	if(_controlTimer.get_elapsedTime() > _controlCooldown){		_controlTimer.reset();		_lastX = x - PADDLE_LENGTH/2;	}	//SET PADDLE POSITION	_paddlePos.x = x - PADDLE_LENGTH/2;

Collision with walls, paddle or bars.
//tempVec is the ball's current position plus its velocity(For future position)		//IF BALL REACHEST RIGHT OR LEFT WALL FLIP X		if(tempVec.x + BALL_RADIUS > FIELD_RIGHT ||			tempVec.x < FIELD_LEFT){				_ballVel.x *= -1;		}		//IF BALL REACHEST TOP OR BOTTOM WALL FLIP Y		if(tempVec.y + BALL_RADIUS > FIELD_TOP || 			tempVec.y < FIELD_BOTTOM){			_ballVel.y *= -1;		}		//IF BALL COLLIDES WITH PADDLE		if(tempVec.x < _paddlePos.x + PADDLE_LENGTH &&			tempVec.x + BALL_RADIUS > _paddlePos.x &&			tempVec.y < _paddlePos.y + PADDLE_WIDTH &&			tempVec.y + BALL_RADIUS > _paddlePos.y){				float speed = _ballVel.length();				if(_ballPos.x < _paddlePos.x || 					_ballPos.x > _paddlePos.x + PADDLE_LENGTH){					_ballVel.x *= -1;				}				else{					_ballVel.x = (_paddlePos.x - _lastX) * SOME_CONSTANT;				}				if(_ballPos.y < _paddlePos.y || 					_ballPos.y > _paddlePos.y + PADDLE_WIDTH){					_ballVel.y *= -1;				}				_ballVel.normalize();				_ballVel *= speed;		}		//IF BALL COLLIDES WITH A BAR		std::vector<Bar>::iterator bar_it = _bars.begin();		while(bar_it != _bars.end()){			Vec2d p = (*bar_it)._pos;			if(tempVec.x < p.x + BAR_LENGTH &&				tempVec.x + BALL_RADIUS > p.x &&				tempVec.y < p.y + BAR_WIDTH &&				tempVec.y + BALL_RADIUS > p.y){				if(_ballPos.x < p.x || 					_ballPos.x > p.x + BAR_LENGTH){					_ballVel.x *= -1;				}				if(_ballPos.y < p.y || 					_ballPos.y > p.y + BAR_WIDTH){					_ballVel.y *= -1;				}				bar_it = _bars.erase(bar_it);			}			else{				bar_it++;			}		}

I haven't accounted for separation if two objects penetrate either. What I am wondering mostly is how some of these things are handled in standard breakout.

Thanks.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5
• Advertisement

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633295
• Total Posts
3011245
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!