Results 1 to 6 of 6

# Thread: Homing Missiles

- 08-12-2013, 02:20 AM #1Member
- Join Date
- Aug 2013
- Posts
- 6

- Rep Power
- 0

## 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.

- 08-12-2013, 03:06 AM #2Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 5,655

- Rep Power
- 9

## 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 y-intercept.

Regards,

JimLast edited by jim829; 08-12-2013 at 03:12 AM.

The Java^{TM}Tutorials | SSCCE | Java Naming Conventions

Poor planning on your part does not constitute an emergency on my part

- 08-12-2013, 03:23 AM #3
## Re: Homing Missiles

Moved from a staff-only section.

dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.

- 08-20-2013, 04:30 PM #4Member
- Join Date
- Aug 2013
- Posts
- 6

- Rep Power
- 0

## 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,myLast edited by bakriawad2000; 08-20-2013 at 05:46 PM.

- 08-20-2013, 07:50 PM #5Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 5,655

- Rep Power
- 9

## 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 y-intercept. 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,

JimThe Java^{TM}Tutorials | SSCCE | Java Naming Conventions

Poor planning on your part does not constitute an emergency on my part

- 09-11-2013, 07:42 PM #6
## 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

## Bookmarks