-1

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

c0der
  • 18,467
  • 6
  • 33
  • 65
jsc0000
  • 11
  • 1
  • What is your question ? How to calculate points along [straight line between A and C](https://www.usingmaths.com/senior_secondary/java/straightline.php) ? – c0der Apr 22 '20 at 15:46
  • Yes! If it is hard... How to generate B image from A coordinate value and move to C image coordinate value The method doesn't matter. Only the results need be correct. What I want is for the bullet from the tower to reach the enemy. I am making a defense game. Among them, the attack function is being implemented. – jsc0000 Apr 22 '20 at 15:53
  • See the link I posted. Here is another one: https://stackoverflow.com/a/18772031/3992939 – c0der Apr 22 '20 at 15:54
  • Your question needs to be corrected: finding an imaginary straight line between image A and image C so that B moves along an imaginary straight line. – jsc0000 Apr 22 '20 at 16:01
  • Thanks for the link!! (yB-yA) / (xB-xA) = (yC-yA) / (xC-xA) but I don't know what yC xC is What is that formula for? And setbounds (help1, help2,100,50) I don't know how it is used in help1 and help2. – jsc0000 Apr 22 '20 at 16:15

1 Answers1

0

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

c0der
  • 18,467
  • 6
  • 33
  • 65
  • When I applied it to my code, I got an error. I tried to upload the code, but it says that it is too long. – jsc0000 Apr 23 '20 at 07:27
  • "To test the code copy the entire code into MoveAlongStraightLine.java and run" don't forget to add the package name. The code runs with no errors as you can also see by using the link posted. – c0der Apr 23 '20 at 10:12
  • This code runs without error If I add it like my code I just deleted, I get an error Lastly, when you know the coordinates of the tower image, the bullet image, and the enemy image, can you tell me how to move the bullet using the angle and distance between the tower and the enemy? I add an image using lable. So I want to know how to use Setbounds. setBounds are int only, so you have to cast the double. – jsc0000 Apr 23 '20 at 10:26