Image moving to the target

303 Views Asked by At

A and C images are far away. At this time, B image is created from the coordinate values ​​of A For each iteration, x, y must be moved to arrive at the C image coordinate value.

For example, A (100,100), C (300,300)
Starting at B (100,100),
Each time it is repeated, x, y must be moved to reach B (300,300).

This is the method for entering the current moving source.

 Public void Attack () {
    int x1 = a.getX ();
    int y1 = a.getY ();

    int x2 = c.getX ();
    int y2 = c.getY ();
    if(b.getX ()==c.getX&&b.getY () == c.getY())'
    {
        system.out.println ("ok");'
    }else {
        b.setbounds (help1,help2,100,50)
    }
 }

Here, I want to know the code to enter help1 help2.

Pythagorean formula

Between A and C images I want to know how to make the B image move along a virtual straight line.

Like a tower defense game. A image is a tower B image is bullet The C image is the enemy.

I want the bullets fired from the tower to move to enemy locations.

I am Korean. I used a translator

1

There are 1 best solutions below

3
c0der On

The following code is an mre of using a straight line equation y = mx + c to draw a moving object along such line.
To test the code copy the entire code into MoveAlongStraightLine.java and run, or run it online:

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class MoveAlongStraightLine extends JFrame {

    public MoveAlongStraightLine(){
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        ShootinBoard shootingBoard = new ShootinBoard();
        JButton fire = new JButton("Fire");
        fire.addActionListener(e->shootingBoard.fire());
        add(shootingBoard);
        add(fire, BorderLayout.SOUTH);
        pack();
        setVisible(true);
    }

    public static void main(String[]args){
        SwingUtilities.invokeLater(()->new MoveAlongStraightLine());
    }
}

class ShootinBoard extends JPanel{

    //use constants for better readability
    private static final double W = 600, H = 400;
    private static final int DOT_SIZE = 10, SPEED = 2;
    private final int dX = 1; //x increment

    private final Timer timer;
    private final Point2D.Double shooter, target;
    private Point2D.Double bullet;

    public ShootinBoard() {
        setPreferredSize(new Dimension((int)W, (int)H));
        shooter = new Point2D.Double(50,350);
        bullet = shooter; //place bullet at start point
        target = new Point2D.Double(550,50);
        timer = new Timer(SPEED, e->moveBullet());
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setStroke(new BasicStroke(3));
        //draw source
        g2.setColor(Color.blue);
        g2.draw(new Ellipse2D.Double(shooter.getX(), shooter.getY(),DOT_SIZE , DOT_SIZE));
        //draw bullet
        g2.setColor(Color.black);
        g2.draw(new Ellipse2D.Double(bullet.getX(), bullet.getY(),DOT_SIZE , DOT_SIZE));
        //draw target
        g2.setColor(Color.red);
        g2.draw(new Ellipse2D.Double(target.getX(), target.getY(),DOT_SIZE , DOT_SIZE));
    }

    void fire(){
        timer.stop();
        bullet = shooter; //place bullet at start point
        timer.start();
    }

    void moveBullet() {

        if(target.x == bullet.x && target.y == bullet.y) {
            timer.stop();
        }
        //y = mx + c  for more details see https://www.usingmaths.com/senior_secondary/java/straightline.php
        double m = (target.y - bullet.y)/ (target.x - bullet.x);//slope
        double c = (target.x * bullet.y - bullet.x * target.y)/(target.x - bullet.x);

        double newBulletX = bullet.x+dX; //increment x
        double newBulletY = m * newBulletX + c; //calculate new y
        bullet =  new Point2D.Double(newBulletX,newBulletY);

        repaint();
    }
}

enter image description here