Results 1 to 6 of 6

Thread: Homing Missiles

  1. #1
    bakriawad2000 is offline Member
    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

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

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,775
    Rep Power
    5

    Default 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,
    Jim
    Last edited by jim829; 08-12-2013 at 03:12 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,415
    Rep Power
    20

    Default Re: Homing Missiles

    Moved from a staff-only section.

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

  4. #4
    bakriawad2000 is offline Member
    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

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

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,775
    Rep Power
    5

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    105
    Rep Power
    0

    Default 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •