Hello! After some time of lurking, I decided to create an account and hope you guys can help me out. So I have a set up where the user draws onto the screen through an onTouch event by creating a path as shown below:
where handle_move_touch is:
edgesRegion is a path full of rectangles, and what I'm trying to do above is say that if the path being drawn by the user (mPath) through the onTouch event collides with a rectangle in edgesRegion, to not allow the user to draw through it; instead they'd have to go around it. The collision detection between the user's path and the rectangles on the screen works as when I put a log statement in the "else" block and draw over a rectangle, it gets printed onto the logcat.
The problem I'm having is that you can still draw through it. I tried to push the user's path location back by doing a mX/mY -= 40 but to no avail. I also tried to create a boolean collision that gets set to true in the else block and wrap the MotionEvent.ACTION_MOVE case in an if(!collision) block, but then it just stops drawing all together after a collision. My guess is that it has something to do with the fact that even though it detects the rectangles, my finger still passes the rectangle and so the coordinates from onTouch are taken, allowing it to draw through the shape, if that made any sense lol.
Sorry if the question isn't particularly clear; I'd be glad to follow up on anything. But any ideas on how to get this to work? Thanks
Code:
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
...
case MotionEvent.ACTION_MOVE:
handle_move_touch(x, y);
invalidate();
break;
...
where handle_move_touch is:
Code:
private void handle_move_touch(float x, float y) {
edgesRegion.setPath(edgePath, new Region(0, 0, width, height));
currentRegion.set((int) x - 10, (int) y - 10, (int) x + 10, (int) y + 10);
if (!edgesRegion.op(currentRegion, Region.Op.INTERSECT)) {
mPath.pathTo(mX, mY);
mX = x;
mY = y;
} else{
//This is the part I'm having trouble with
}
...
}
The problem I'm having is that you can still draw through it. I tried to push the user's path location back by doing a mX/mY -= 40 but to no avail. I also tried to create a boolean collision that gets set to true in the else block and wrap the MotionEvent.ACTION_MOVE case in an if(!collision) block, but then it just stops drawing all together after a collision. My guess is that it has something to do with the fact that even though it detects the rectangles, my finger still passes the rectangle and so the coordinates from onTouch are taken, allowing it to draw through the shape, if that made any sense lol.
Sorry if the question isn't particularly clear; I'd be glad to follow up on anything. But any ideas on how to get this to work? Thanks