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!