
Homing Missiles
i gotta admit... it is taking the p***!!!!
how can i get an object to move from a certain coordinates to another in a line like a missile or a bullet...? what is the algorithm?
i was not able to get it... i been trying for a couple of days now to think it out; outcome = failure!
i want the speed to be always steady so it does not go faster or slower.. always at maybe 5px/tick (doesn't aply to lasers / mouse defined coordinates)
what i need is both for laser/bullet type (straight line, could be instant) and a missile which moves up and down according to the target.

Re: Homing Missiles
Well, it's not as hard as you might think. I will assume you know how to override paintComponent and draw an image at some location x,y in a JPanel. Make certain that those coordinates are accessible (either directly from instance fields or from get methods) from the paintComponent method where you draw the image. Then simply update them every unit of time. If you want to move 5 pixels per second then you can use a timer. If you want the image to move vertically then alter the y coordinate. If you want it to move horizontally then alter the x. Add either 5 pixels every second or 1 pixel every 200 milliseconds. After each update, just call repaint. The formula for a straight line is y = mx + b. The m is the slope and b is the yintercept.
Regards,
Jim

Re: Homing Missiles
Moved from a staffonly section.
db

Re: Homing Missiles
thanks for the help, but i did not understand it fully.. i dont get what the slope is or how to get it.
i am aware of the painting and all, i just want to be be Homing..
so a bullet will be shot at a target u select with a mouse which could be anywhere!
also, a homing missile which should follow the target.
so, y=mx+b
if the coordinates are:
bullet: 10,20
target: 100,50
how can u apply this formula?
maybe with:
bullet cords = bx,by
target cords = tx,ty
movement = mx,my

Re: Homing Missiles
This does require some algebra. Since you have the coordinates of the bullet as it starts and the target, you can calculate the slope m, (change in y's over change in x's). You can then calculate "b", the yintercept. Since y = mx + b. b = y  mx. Just use one of your coordinate pairs along with m. Then you should have the equation, y = mx + b when m and b are real numbers. So just interate thru values of x starting with 10 and going to 100. That will provide a straight line to the target. Make certain you do your calculations using doubles to avoid integer truncation errors.
Note: you may need to play around with this since my description was for a normal xy graph where the origin (0,0) would be at the lower left of a positive quadrant. In Java, the origin is at upper left of the positive quadrant. That also means that y increases in value as it goes from top to bottom in a graphics context.
Regards,
Jim

Re: Homing Missiles
you could try my "slope meothod" shop slope (point a  point b)
it gets a missle from point a to point b anywhere on the screen including diagonal