Trigonometry problem

Game is here: http://www.javadaemon.com/games/breakout

However, that control scheme is hard. I want the balls new direction to reflect where it hit on the bat.

This is how I currently do it, and it does not work one bit.

float degreesPerPixel = (float)180/player.getWidth();

The bat is 80 pixels long, so this should equal 2.25, which is does.

int whereBallHit = (int) ((player.getX()+player.getWidth())-(ball.getX()+(ball.getWidth()/2)));

This bit should reflect the pixel coordinate of where the center of the ball hit the bat. It is counted from left to right because of the way degrees work. They start with 0 on the left, working over at 180 in the right.
This bit also works.

float degreesApplied = degreesPerPixel * whereBallHit;

This bit also works as intended. It is the amount of degrees on the angle the ball should now be flying in.

ball.setXV((float)Math.cos(Math.toRadians(degreesApplied)) * 1.8f);
ball.setYV((float)Math.sin(Math.toRadians(degreesApplied)) * 1.8f);

This bit however, has drawn my attention because of not working. I’m suspecting wrong use of the Math functions. The ball does not make a lot of air after this bit. How can I fix this? Also, how would I set the generel speed of the ball?

Thanks guys :slight_smile:

Cos = X, Sin = Y, you are correct. But shouldn’t you be adding cos(deg)*1.8 to the X and sin(deg)*1.8 to the Y? :wink:

It does work quite a lot better now. However, it will become VERY slow if one of the velocities gets really low. How can I work around this?
Keeping one generel speed. You know, either having XV = 5 and VY = 0 or XV = 2.5 and YV = 2.5, instead of one of them just disappearing.

Doing this

ball.setXV(ball.getXV() + (float)Math.cos(Math.toRadians(degreesApplied)) * 1.2f);
			ball.setYV(ball.getYV() + (float)Math.sin(Math.toRadians(degreesApplied)) * 1.2f);

Still slows down to a crawl, and acts wierd. Angle is off, and it gets more and more off the longer I play.

What is the V in the XV and YV? Velocity? Why are you adding distance to your velocity?

Pseudocode of what you should be doing:


deltaX = cos(degree) * velocity.x;
deltaY = sin(degree) * velocity.y;

x += deltaX;
y += deltaY;

When you described how degrees worked, I thought your problem might have been that you went the wrong way (0° is on the right, 180° is on the left) but your code looks fine. I am unsure why you are multiplying the sines and cosines by 1.8 and 1.2, but I do know that for any right triangle where the angle is supplied, X = hypotenuse * cos(Θ) and Y = hypotenuse * sin(Θ), where X and Y denote velocity, Θ is the angle, and hypotenuse is the ball’s total velocity. Your code seems to keep X and Y velocity separate, whereas they should both be based off of a single absolute velocity.

Try this code, see if it works:


ball.setXV((float)Math.cos(Math.toRadians(degreesApplied)) * ballV);
ball.setYV((float)Math.sin(Math.toRadians(degreesApplied)) * ballV);

You can also easily change the ball’s speed by changing ballV.