I am developing a simple tower defense game and have been trying to think of a solution to this for a while: I have a cannon tower, of which I can get the X and Y in pixels and the games coordinates, and I also have it's target who's position moves every frame for it is running down the track. How would I make the cannon that the tower shoots "follow" the target. Picture it as a heat seeking missile, trailing behind it. Help would be much appreciated! Also, if anyone knows a simple way to maybe use slope to return which angle the tower should be pointing at? I have all the pictures on a sprite sheet, loaded up and ready. Thanks!cMp

One way would be to use atan2 to find the angle between the target and the cannonball. However, as the target changes, the angles starts to curve from the original start point. To make it more like a cannonball, you just would want to use atan2 when you initiate the cannonball and it will just go in that direction regardless. You probably would want to predict it before.

Usage:double angle = Math.atan2(target.y - this.y, target.x - this.x)

Or if you don't want to use all this trig, you can just get a vector direction and normalize it and multiply it by the speed of the cannon ball, and every update, add the direction vector to the cannonball's position.

Easy. Learn vector math. Get the difference between two points, normalize it, multiply by the missile speed. More advanced movement is slightly more complicated, but that is not saying much assuming you know how to use vectors.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org