开发者

Creating a Region from a Path in Android

开发者 https://www.devze.com 2023-04-10 23:55 出处:网络
I\'m a new Android developer that\'s developing a simple twist on the Tron game with GPS movement, but I\'m having trouble implementing player\'s intersecting.

I'm a new Android developer that's developing a simple twist on the Tron game with GPS movement, but I'm having trouble implementing player's intersecting.

Right now, my player's trails are Paths that I move to co-ordinates and draw the co-ordinate difference as a line on the canvas.

The path class offers no such intersection method that I can find, so I resorted to using Regions which I've tested an intersection to work with 2 regular Rectangles, but I can't make a Region using Region.setPath for some reason. From my understanding, the path needs to be closed to form an area for it to create a Region, which isn't exactly what I need.

Is there a way to create a region off a path, and not the area the path creates? ie: If the path were a straight line of 10px thick, how do I make a region that is a line of 10px thick?

Here's a short code sample I'm doing:

Path p1path = new Path();
p1path.moveTo(startPos,startPos); 
p1path.lineTo(newPos,newPos);
p1path.moveTo(newPos, newPos);
Region p1region = new Region();
p1region.setPath(p1path, new Region(0,0,480,800); // this is where the开发者_StackOverflow region isn't setting as I thought it would...

// do same for p2
if(p1.quickReject(p2)) // checks for intersection

Thanks :)


So I've solved this quite some time ago, but to help those who eventually stumble onto this and want some similar functionality:

This post is quite some time ago - so let me remember what happened.

Creating a Region around a path actually did work but for a very limited set of Paths. What I mean by "Creating a region around a Path" is that for a Path that goes from x1,y1 to x2,y2 create a rectangular Region that covers (for example) x1-50,y1-50 to x2+50,y2+50 where 50 is the pixel weight stroke of the Path.

If you can visualise it, it basically creates a rectangular region that covers the Path and it's 50px stroke so you can "fake" Path intersection using Regions. Wherever a Path is, a Region is and so when 2 Paths "intersect", you can check for Region intersection (which you can do but I've forgotten the method names).

This however proved to work only for a few Paths. Though I'd like to think my Math proficiency is adequate, I could not get it so that for whichever direction the Path went the Region would work. Different angles, different directions etc. caused the Region not properly drawing under the Path. My above example of using the 50 stroke width would only work for going a particular direction.

The solution my parter and I stumbled onto was creating a 2D integer array that mapped onto the screen. Wherever a Path went into a certain direction, we would fill every array cell the Path mapped onto with a specific value (1). We would do the same for the other Path, but with a different value (2). Each move you make you would check the 2D array against the Path co-ordinate to see if it has been occupied or not. There was an important mathematical formula that would extrapolate which cells were visited when you go from x1,y2 to x2,y2 that proved very helpful - I believe it was called something along Brasenheim's formula, or something.

It's not the most elegant solution, but it ended up faking Path intersection well. If anyone is interested in a better explanation, you can message me.

Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号