# Java Collision Detection of a Slope using a Gradient [closed]

Im testing out collision detection for a game, The ball is traveling and i need to know when the ball hits a sloped line.

For this example i have used the whole screen, with a sloped line going from the top left corner to the bottom right corner.

I am working out the gradient using Y = MX + C

``````private int m;
private int c;
private float slopeY;
private void tryCollision(){
m = getHeight() / getWidth(); // change in Y / change in X
c = 0 // where the line crosses the x axis
slopeY = m * ball.x + c;
if(ball.y >= slopeY){
ball.ballStart = false; // ball will only move if it ballStart = true
}
}
``````

Ball stopped moving where ever i place it on the screen. The problem is that working with gradients, Your meant to work with the y axis starting at the bottom going to the top, but in java the y axis starts at the top left corner going down. I have been told to invert the getHeight() number but i am unsure.

 The orientation of the Y axis shouldn't matter as far as the calculations are concerned. This only becomes a factor when you plot to the screen. I have one concern about the line `slopeY = m *= ball.x + c;`: Is there a reason for the `*=` in there? It seems to me that it should just be `*`. – Code-Guru Aug 21 '12 at 20:19 I am not sure why i have done that, i ran it and the ball stopped moving where ever i place it on the screen. – Charlton Santana Aug 21 '12 at 20:25 The reason it may have to be converted is when i am working out M. – Charlton Santana Aug 21 '12 at 20:27 If the line slopes from the top left to the bottom right of the screen, then you have calculated m correctly with `m = getHeight() / getWidth();`. You should not later change m with the `*=` assignment operator, as far as I can tell. I suggest using just `y = m * ball.x + c;` in the line under discussion. – Code-Guru Aug 21 '12 at 20:58 I went to edit and see if it would work, but when asking the question.. i wasent ment to put the = in the question and in my code it was y = m * ball.x + c; sorry.. – Charlton Santana Aug 21 '12 at 21:09