This discussion is archived
3 Replies Latest reply: Oct 31, 2012 10:32 AM by 971508 RSS

How to track a moving object

971508 Newbie
Currently Being Moderated
There is a randomly moving object(obj1) on the panel. We have another object(obj2) that is supposed to track obj1 initiating from the (0,0) point on the panel. The obj2 must get closer to the obj1 as time passes.
What logic should I use to do this?
In my code, I have that obj1 moving aimlessly on the screen with a line pointing that object only from the (0,0) point.

I put a link here that can help me make myself clear.

http://www.mediafire.com/view/?2fc0tvqamqf8fpl#

Edited by: 968505 on Oct 30, 2012 11:05 AM
  • 1. Re: How to track a moving object
    gimbal2 Guru
    Currently Being Moderated
    Sounds like basic 2D vector math to me, which is something you can Google for hundreds of articles on the matter. You have two points representing your two objects - now imagine an invisible line between those two points. One of the objects (your "terminator") should be periodically moving along that line until it collides with the other object.
  • 2. Re: How to track a moving object
    971508 Newbie
    Currently Being Moderated
    Hi. thank you for your reply gimbal2. I've been searching this for a while now but unfortunately I couldn't find a working solution for this. there are some approaches disscused on some websites but they didn't show good results. I would really appreciate it if you could tell me how it works or at least drop some link in which I could find it myself.
    I must also add that I'm not good at math.

    thanks a lot.
  • 3. Re: How to track a moving object
    971508 Newbie
    Currently Being Moderated
    This is also the code I have written so far which is not so good.
    One thing to mention is the speed of the object(missile) which must be constant all the time.


    package Test;

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JPanel;
    import javax.swing.Timer;

    public class Tracking extends JPanel implements ActionListener{

    private Timer t;

    private int newX= 0, newY= 0;
    private int currentX, currentY;
    private int i=0, j=500;
    private int movingY=800;


    public Tracking(){
    setBackground(Color.WHITE);
    setOpaque(false);
    }

    public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

    g2d.setPaint(Color.RED);
    g2d.fillOval(600, movingY, 50, 50);


    g2d.setPaint(Color.BLUE);
    g2d.fillOval(newX, newY, 10, 10);


    }
    public Dimension getPreferredSize(){
    return new Dimension(800, 1000);
    }

    public void doIt(){

    t= new Timer(100, this);
    t.start();
    }

    public void actionPerformed(ActionEvent e){

    if(i<=100) {
    i++;
    }

    currentX= newX;
    currentY= newY;

    newX= currentX + (i*((600 - currentX))/j);
    newY= currentY + (i*((movingY - currentY))/j);

    if(j>1){
    j--;
    }
    else
    t.stop();

    movingY-=10;
    //--------------------------


    repaint();
    }

    }

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points