- Home /
Need help with logic.
Hello! Currenty im making a game, where you should make spaceships to capture planets. Im stuck in making proccess of choosing the closest path of chosen way. Here is a picture of what i mean. S - starting point, E - end of path, red arrows show the closest path. I was thinking about system like this: Each planet has a list of connectet planets. (Planet 1 has list of 2,4,5) etc. Then you just take ending planet and look for planets, that are connected to it, check if connected planets arent your starting point, if not choose the next one planet. Then store all that paths and just choose the closest one. Im stuck in implemeting way how to store all that paths. Thanks in advance.
I understand that its a bit confusing, so you can ask me anything you dont understand.
If you have very small number of planets you can just try all combinations. Above that it becomes a non-trivial problem, known as "traveling salesman problem." Google for that and you'll find possible solutions.
Is there a way to store all possible combination within one loop?
Answer by spraw · Nov 13, 2016 at 03:57 AM
The A* algorithm should do exactly what you are looking for, given that you already have a node based system and everything...
Answer by UrielKane · Nov 13, 2016 at 11:12 PM
Helo dear Le_Valius i'm sertainly not a profesional at this, and sure there someone can have a better solution. Anyway i'm going to tell you how i will do it if i was developing that logic. I'm currently developing a prototype of a FPShooter so i have to deal with pathfinding and waypoints systems. I found some years ago on the web a really simply and very useful waypoint system. That is ideal for car racing like games, that store in every waypoint, with are the next one and have a return type function that let you get the next waypoint. Of course may be you would amplify its versatility and functions.
After saying that, what i would do in your place is. Think the solar sistem like a web so every planet have stored wich ones are conected. You can do an array variable with every planet conected. Then you have to create probably a very tricky loop where you are going to check and eliminate those ones wich are not conected until the end of the path. So you propose a very simply example wich can become very complex in more extended scenarios. But lests stay in the simply.
Lets say that you want to go from Start to End like in the example. So in the midle of the path you have a node planet how is not the end but a bridge to rich it. So in the loop you probably have to see is. 1) where are you, and what planets are linked to that one. 2) Take those linked planets and search wich ones have the end path (I have to say that this in first attempt can work on that simply example but in a larger scenario may be can become so tricky). Construct an array of those planest that have linked the end destination. And from here everything should be less problematic. Now you have to compare the distance of full path each other, and pick that one that is the smaller.
You may be notice that this is pretty functional in that specific scenario that you bring to us. But lets say that the system is a web of docens or even hundreds of planets. That's when the big trouble begun, becouse i have not taking into account that is important to kept track of every node of the path. So to achive a good result may be you will have to do a tremendous scan of the entire web. May be eliminating some obious ponts where is no need for a check.
This is just a general idea i really dont know if it's going to work, or work properly. Is just what i probably will do if i was trying to achive what you prupose. I dont remember where i found the waypoint system, but if you need it just ask and i will share it.
Good Luck!
Thank you mate, i will try your idea for sure. Would be thankful if could link your waypoint system, but no problems if you forgot! :)
#pragma strict
// The start waypoint, this is initialized in Awake.
// This variable is static thus all instances of the waypoint script share it.
//public DirectionalWaypoint start;
// The next waypoint, this variable needs to be assigned in the inspector.
// You can select all waypoints to see the full waypoint path.
public var next : DirectionalWaypoint;
// This is used to deter$$anonymous$$e where the path start.
public var isStart : boolean = false;
// This is used to deter$$anonymous$$e where the path end.
public var isEnd : boolean = false;
// Returns where the AI should drive towards.
// position is the current position of the car.
function CalculateTargetPosition (position:Vector3, nDistance:float) : DirectionalWaypoint{
// If we are getting close to the waypoint, we return the next waypoint.
// This gives us better car behaviour when cars don't exactly hit the waypoint
if (Vector3.Distance (transform.position, position) < nDistance) {
return next;
}
// We are still far away from the next waypoint, just return the waypoints position
else {
return this;
}
}
function CalculateDistance (position:Vector3) : float {
// If we are getting close to the waypoint, we return the next waypoint.
// This gives us better car behaviour when cars don't exactly hit the waypoint
return Vector3.Distance (transform.position, position);
}
// This initializes the start and goal static variables.
// We have to do this inside Awake because the waypoints need
// to be initialized before the AI scripts use it.
// All Awake function are always called before all Start functions.
function Awake () {
if (!next)
Debug.Log ("This waypoint is not connected, you need to set the next waypoint!", this);
}
// Draw the waypoint pickable gizmo
function OnDrawGizmos () {
Gizmos.DrawIcon (transform.position, "waypoint.png");
if (next) {
Gizmos.color = Color.green;
Gizmos.DrawLine (transform.position, next.transform.position);
}
}
// Draw the waypoint lines only when you select one of the waypoints
function OnDrawGizmosSelected () {
Gizmos.DrawIcon (transform.position, "waypoint.png");
if (next) {
Gizmos.color = Color.red;
Gizmos.DrawLine (transform.position, next.transform.position);
}
}
That's the code and you will need a gizmos icon to work properly. It has to be a transparent icon and name it Waypoint. Anyway i'm going to se if i can send to you the icon.
Here a folder with both the script and the gizmos icon. Luck in your project. https://mega.nz/#F!z8hX2$$anonymous$$ZL!B8FIv8PUS2kl_a3wtA0SRQ
Your answer

Follow this Question
Related Questions
TransformPoint and InverseTransformPoint 2 Answers
How destroy a object and it's similars 4 Answers
Check to see if any object in a list meets set of requirements 0 Answers
Provide user virtual currency after every 24 hrs. 1 Answer
Scenes and objects organization in games like Stellaris or Endless Space... 0 Answers