Jump to content
  • Advertisement
Sign in to follow this  
tnutty

quick question about collision response

This topic is 3497 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

In pong, when the ball hits the pad. I am ignoring friction so there is no loss in energy in the x direction, so ballSpeedx equals -ballSpeedX. But for the y component, I am having a little trouble. this is what I am doing for the response : NOTE: Pongball.ballX is the x position of the ball. positionPadX is the y position of the pad. All I need help is on ballSpeedY. I want the ball in to respond in a intuitive manner. What I am doing below for ballSpeedY is taking the x to be 1, and adding 1 to the height and finding its magnitude. Any help improving this part?
		if(PongBall.ballX + PongBall.radius  >= positionPadX - padWidth/2)
		{
			ballSpeedX = -ballSpeedX;
			ballSpeedY = -sqrt(pow(padHeight+positionPadY+1,2)/1) /100;
		}

Share this post


Link to post
Share on other sites
Advertisement
If you ignore friction, the intuitive reaction to me would be not to change SpeedY during the collision. But this does not leaves the player with much control, so it might not be what you want ... You could take into account the Y speed of the pad. Or also, like on some breakouts, the part of the pad that was hit by the ball (comparing ballY and paddY).

Share this post


Link to post
Share on other sites
Wouldn't it be more natural to keep constant the total speed? Of course, this can make the game horribly slow with great SpeedY values.

I have been looking at http://www.xnet.se/javaTest/jPong/jPong.html and it seems to me that it's doing something like

SpeedY = SpeedY + PaddlePositionHit;

So, it adds to the Speed the paddle position with was hit, having the center of the paddle a value of cero, the upper part adding speed towards the top of the screen, and the lower part adding speed towards the bottom.

And it feels good.

Share this post


Link to post
Share on other sites
For ballSpeedX, it feels natural just to do ballSpeedX = -ballSpeedX.

For ballSpeedY, I need the ballSpeedY to be at least -ballSpeedY, but this
also controls the ball direction. If the ballSpeedY is fast, then the ball goes
higher. If its too low, then the ball will go in the negative y direction, and
if the ballSpeedY is close to 0, then the ball goes in a constant line.

I want to change to ballSpeeY so that the physics seems normal.
The name ballSpeedY seems contradictory because it sounds like a scaler and
not a vector, but ultimately, it acts like a vector.


if(PongBall.ballX + PongBall.radius >= positionPadX - padWidth/2)
{
ballSpeedX = -ballSpeedX;
ballSpeedY = -sqrt(pow(padHeight+positionPadY+1,2)/1) /100;
}



Share this post


Link to post
Share on other sites
Did you try ballSpeedY = ballSpeedY (doing nothing do ballspeedY) ?
That's the most "normal" in terms of physics.

Share this post


Link to post
Share on other sites
Quote:

Did you try ballSpeedY = ballSpeedY (doing nothing do ballspeedY) ?
That's the most "normal" in terms of physics.


Well, all that will do is make the ball travel in reverse direction with the same magnitude. This means that the ball will always travel in the same
trajectory, which is very very predictable. All I want it do is
travel in a certain direction depending of the position of collision. I though
that is want I was doing with the source code I provided, but it feels like
its not what I want it to be.

Share this post


Link to post
Share on other sites
OK, so you want some control on the ball rather than complete naturalness.
The easiest would be the formula given by ravengangrel:

SpeedY = SpeedY + PaddlePositionHit;

PaddlePositionHit must be 0 if the ball hits the center of the pad, and range from some negative to some positive value. For example simply:

PaddlePositionHit = PongBall.ballY - positionPadY;

If the deflection is too much or too low (you'd be lucky if adding a position to a speed was working raw), you can use a multiplicator on PaddlePositionHit in the first formula.

If you want a larger portion of the pad that does not deflect the ball, or to keep the total speed of the ball, more complex formulas are needed.

Share this post


Link to post
Share on other sites
A more natural way to do this can be adding the Paddle speed to the ball speed (I think I have seen this in some commercial arkanoid clone), but of course, for this you need to keep track of the paddle speed.

Share this post


Link to post
Share on other sites
I will try that. I have the position of the pad in my pad class, so it shouldn't
be a trouble.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

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!