This site requires JavaScript, please enable it in your browser!
Greenfoot back
Dytanoth
Dytanoth wrote ...

2012/12/23

Stay on the road

Dytanoth Dytanoth

2012/12/23

#
Hello, So I've got some cars, they're supposed to drive a random route. The problem is, is how do I get the cars to stay on the roads. Is it only possible using something like: getOneObjectAtOffset(x,y,road.class), or are there better ways of doing this? Thanks!
Duta Duta

2012/12/24

#
Can you give some more detail about your scenario? One way I can think of to do this, is to have various route segments (which would be lists of x-y co-ords), and every time a car gets to the end of one, it randomly chooses one of the connecting segments. By a list of x-y co-ords, I mean something like this:
class RouteSegment {
	Point[] points;
	RouteSegment[] nextPossibleRoutes;
	
	// Methods, constructors etc.
}

class Point {
	int x, y;

	// Again, methods etc
}
While you're going through a route, each car basically just goes toward the next point in the array until it gets there, then goes to the next one and so on until it gets to the end, at which point it chooses one of the route segments from the next possible routes and follows that route. And so on. But like I said, if you give more details about your scenario maybe a different method would suit your scenario better.
Duta Duta

2012/12/24

#
I thought my reply wasn't adequate, so I've written some code available here. You may want to do it yourself and not look at my code, but in case I wasn't clear, it's there. I haven't actually tested it (typed it straight into a text editor), so it could have a bug in (e.g. a missing import or something).
Dytanoth Dytanoth

2012/12/24

#
I hadn't thought of it that way, but it sounds logical. My roads are something like: ---------------------------- ---------------| | | | ---------------| |------------ -------------- | | I will take a look at how you told it, but then I have another question. You can control a car, this car is at the X, but it has to go to the O. How can I get the car to drive to the O, without getting of the road ----------------------O----- ---------------| | | | -----X--------| |------------ -------------- | |
Duta Duta

2012/12/24

#
I don't really get what you mean, sorry :/
Dytanoth Dytanoth

2012/12/24

#
Well those "---" are horizontal roads, and the "|" are vertical roads. Imagine there is a car at the X, but it has to go to the O. How can the car drive to the O, but stay on the road. How can I tell him where he should drive to.
Duta Duta

2012/12/24

#
You could look at all the road sections coming off the one X is on, and the ones coming off those, and so on until you get to the section that O is on. I'll try to explain what I mean a bit better. Use this image for reference: First we're at X, on the red road segment. The segments coming off X are 1 and 2 (the blue segments). Off each of the blue segments, see what comes off those (the green segments). We can discard sections 3 and 6 as they are basically dead ends. We then look at the segments coming off 4 and 5 - 7, 8, and 9 (the yellow/orange segments). We can, of course, discard 9. As we can see O's segment is 7, which we've made it to, we're now nearly done! So we found two routes to get from X to O:
  • 1, 4
  • 2, 5, 8
But which one do we choose? While we were going along the routes, trying different possibilities, we add the lengths of new segments that we find. Because of this, we can know the length of the two routes, which is:
  • Length1 = Length(1) + Length(4)
  • Length2 = Length(2) + Length(5) + Length(8)
And then we just pick the route with the shortest length (assuming, of course, that we want to take the quickest route). This would have been much easier/better to explain using graph theory, but as I have no idea what kind of level your maths is, I avoided it. If you know graph theory, try visualizing the image I gave as a graph - convert all the route intersections to points, and the routes to edges. Then it's just a case of getting from one edge to another.
Dytanoth Dytanoth

2012/12/25

#
You, sir, are a genius. I wouldn't have thought of this myself. I'd like to thank you for you help, this really is helpfull!
You need to login to post a reply.