Search code examples
javaandroidconverterscollision-detectionpong

Collision Detection and Float Calculation


My pong ball has x and y values from 0.0 to 1.0, which are the bounds the screen.

It bounces off the screen just fine, but won't recognize my paddle. My paddles have values outside the range detectable, I don't know how to get the values to be between 0.0 and 1.0.

I need to get the values to be between 0.0 and 1.0

float paddleHeightTop = (float)(rPaddle - (height/10));
float paddleHeightBottom = (float)(rPaddle + (height/10));
float paddleLeadingEdge = (float) (rPaddle/1000);    

paddleLeadingEdge has value 897.0, needs to be 0.897... Refuses to convert, remains 897.

I haven't worked out the math on the paddleHeightTop or Bottom, but they also need to have a value between 0.0 and 1.0... paddleHeightTop has value 183.0. paddleHeightBottom has value 1.8970001.

rPaddle updates it's value from on 'onMotionEvent'.

My code for onDraw for paddle looks like this (and draws and updates position correctly):

canvas.drawRect( 93 * (width / 100) , rPaddle - (height/10), 95 * (width / 100), rPaddle + (height/10), light);

I'm putting that here because I'm sure there some relationship I'm missing. Thanks ahead of time.

PS: This is my collision detector (moved from comments):

if (ballY < paddleHeightTop  && ballX > paddleLeadingEdge && ballY > paddleHeightBottom ) { 
    soundPool.play(paddleSound, 1, 1, 0, 0, 1);
    ballSpeedX *= -1;
    }

Solution

  • My Android Studio Debugger was having an error, never mind.