Changing direction towards a different object

I'm in the process of putting together some pseudo-code, trying to read through my professors code (he doesn't comment much) and just general planning for an upcoming school project but there's a snag I've hit that I'm not quite sure how to approach.

The project is to use the list library to build a a navel fleet using several derived classes from a base Ship class, and have input going into the center Airline carrier in order to direct all the other ships, and then run this all for a few cycles using GNUplot and the ships assigned X and Y (Z if a submarine)coordinates to make a gif.

The issue I'm running into is one of the required derived classes is a small Fuel ship that follows behind the main Carrier. The Fuel ship is supposed to be designed so that when one or more of the fleet ships get low on fuel, the whole fleet is to slow down and the Fuel Ship starts going ship to ship topping off their fuel before returning back to its default position behind the carrier so that the screen movement can continue. But I'm having a hard time figuring out how exactly to make the Fuel boat get the direction of each of the low fuel boats relative to it's own position (IE, making it turn towards the next low fuel boat).

I don't have any example code at the moment since I'm just in the planning stages but I'm a bit stumped on how to get the angle the Fuel Ship needs to turn in order to move towards the other ship. Any help is greatly appreciated.
Last edited on
closed account (48T7M4Gy)
Use the x,y coordinates of the two ships - the fuel ship and the particular fleet ship - combined with the direction the fuel ship is travelling in, you can work out the change in direction that the fuel ship needs to make. Update the changes at suitable time intervals.

Of course if your tracking system is really smart you can allow for the direction of travel of the fleet ship and how fast each of the ships are going at any time. These considerations generally come under the heading of tracking or homing algorithms if you want to surf araound.
That's kind of where I'm running into the issue, I'm not quite sure how to get the angle that would come from the change in direction. For example say it needs to turn 30 degrees (.5 radians) I'm not entirely sure how to calculate that. I've had trouble with angles and angle calculations ever since high school, and it's been well over a year and a half since I've had to do any calculus or geometry.

I've been thinking its over in my head and the closest thing I could come up with was subtracting the two ships x and y values and popping the results into a triangle calculation to get the angle opposite of the hypotenuse... But would that work? I'm not sure if that would direct the fuel ship towards the other boat and thus making it the fuel ships new baring? Or would the given new angle be too large and have to be reduced in some way....? Would it need to be cut in half?

Sorry if this seems an overly simplistic question, I just have trouble with mentally visualizing angle calculations.
Last edited on
I did some digging and realized I was asking the wrong questions. I eventually found the calculation I was looking for.

float angle = atan2(p1.y - p2.y, p1.x - p2.x)

I completely forgot about Arctangents and the like. Alright now to get this game plan written up.
closed account (48T7M4Gy)
Well, you need a pencil and paper. Seriously! Don't imagine it just draw it! Except for submarines which is just a third dimension embellishment I suggest you go back and revise/learn some 2d coordinate geometry and revise/learn trigonometry.

They form a basic start which will get you a long way and you won't need calculus.

The only term you need to know beyond that is 'bearing' and that is just a direction measured from north (+ve y axis). There are two bearings - the fuel ship direction of travel and the bearing from the fleet ship to the fuel ship which is derived from the two sets of coordinates and trigonometry.

The difference between the x's and y's correctly give you the two sides of the right angle triangle that needs to be resolved etc so you are on the right track using that and say the arctangent to get an angle (in radians is good enough).

The difference between the two bearings is the change in direction required. (Don't forget the bearing difference can be 180 degrees 'out' so you need to make sure the coordinate sets are used in a consistent and the correct order. A sketch will easily show what is happening there.) You might subtract 180 degrees but I suspect strongly halving it would be a disaster.

You can work in radians and save the trouble of converting backwards and forward from degrees. BTW 30 degrees is pi/6 radians not 0.5 radians

If you can't handle the trigonometry involved why no design a neural network/AI approach and let the ship train itself to do the job?
Thank you for all your help Kemort. Between what you've said here and deciphering my professors code, I think I have a strong idea of how to proceed from here. It's starting to get late but starting tomorrow I think I can start building the classes and get them to work as I need. Thankfully the prof was kind enough to provide us with a few functions on calculating bearings and slight drift in the fleet direction. Using those as a starting point I should be able to get the classes up and running.

Also yeah I should go relearn 2D coordinate geometry, it's been so long I could use a good refresher.

Thanks abain.
Topic archived. No new replies allowed.