Check collision of lines and circle in android canvas

I am trying to check for collisions between rows and a circle, if the circle hits the line, it will work with changing the boolean values ​​to true, and as soon as it is not affected, it will change it to false. I use canvas and surface.

Here is my collision check code that did not work and ended up in error:

@Override public void run() { while(runnable) { if(!holder.getSurface().isValid()){ continue; } Canvas mCanvas = holder.lockCanvas(); update(mCanvas); values = new Values(mCanvas); createPaints(); drawBackground(mCanvas); drawObjects(mCanvas); holder.unlockCanvasAndPost(mCanvas); } } 

Now the collision is processed in the update:

 private void update(Canvas c) { ball.update(c, checkLinesCollision(values.level1, ball.getX(), ball.getY())); //takes a canvas, and a boolean } boolean checkLinesCollision(float[] f,float x,float y){ int c = 0; for(int i = 0; i < f.length; i+=4){ float x1 = f[i]; float y1 = f[i+1]; float x2 = f[i+2]; float y2 = f[i+3]; if (x> x1 && x<x2 && y>y1 && y>y2){ c++; } } if(c>0){return true;}else{return false;} } 

level values

  float yLow = c.getHeight()-c.getHeight()/4; level1 = new float[]{0,yLow,c.getWidth(),yLow, 40,c.getHeight()/2,300,c.getHeight()/2}; 

ball update function:

 public void update(Canvas c, boolean b) { if(b){ dy=-dy; b = false; } y -= dy; dy--; } 

Now, according to the logarithm, the problem is the main update function.
I think I am using the wrong function, what can I do to fix it?

Thanks!

+4
source share
1 answer

I have found a solution. Apparently the problem was in the float array; in my application, I declared a float array in the constructor. After much research on the Internet and re-checking my logarithm, I found that you cannot declare an array in the constructor. all i did was move the float out of the constructor, and everything was fine, although I found a solution, I'm not sure why you cannot declare it in the constructor.

0
source

All Articles