3 Replies Latest reply on Oct 31, 2012 5:32 PM by 971508

    How to track a moving object

      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.


      Edited by: 968505 on Oct 30, 2012 11:05 AM
        • 1. Re: How to track a moving object
          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
            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
              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(){

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

              g2d.fillOval(600, movingY, 50, 50);

              g2d.fillOval(newX, newY, 10, 10);

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

              public void doIt(){

              t= new Timer(100, this);

              public void actionPerformed(ActionEvent e){

              if(i<=100) {

              currentX= newX;
              currentY= newY;

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