Projektil zur Maus Position

Lauezl

Mitglied
Hey Leute,
libgdx.png
Ich hätte gerne so wie auf dem Bild, dass das Projektil von einer Waffe zur Maus-Position fliegt. Ich habe schon ein bisschen gegoogelt und bisher das gefunden:
[CODE lang="java" title="Code"]pathX = xMouse - x;
pathY = yMouse - y;
double distance = Math.sqrt(pathX * pathX + pathY * pathY);
directionX = pathX / distance;
directionY = pathY / distance;

//Projektil Koordinaten
position.x +=directionX*delta*100;
position.y +=directionY*delta*100;[/CODE]

Es funktioniert irgendwie nicht richtig(siehe Bild).

LibGdx2.jpg

Danke im Vorhinein.
 
Trigonometrie...Satz des Pythagoras usw. sollten dir weiterhelfen.

Edit: So kompliziert brauchst du es gar nicht. Probiere mal das folgende aus (ich nehme mal deinen Code und bearbeite ihn etwas):

Java:
pathX = xMouse - x;
pathY = yMouse - y;
//double distance = Math.sqrt(pathX * pathX + pathY * pathY);
//directionX = pathX / distance;
//directionY = pathY / distance;

//Projektil Koordinaten
position.x +=pathX*delta*100;
position.y +=pathY*delta*100;
 
Zuletzt bearbeitet:
Trigonometrie...Satz des Pythagoras usw. sollten dir weiterhelfen.

Edit: So kompliziert brauchst du es gar nicht. Probiere mal das folgende aus (ich nehme mal deinen Code und bearbeite ihn etwas):

Java:
pathX = xMouse - x;
pathY = yMouse - y;
//double distance = Math.sqrt(pathX * pathX + pathY * pathY);
//directionX = pathX / distance;
//directionY = pathY / distance;

//Projektil Koordinaten
position.x +=pathX*delta*100;
position.y +=pathY*delta*100;
Stimmt. Nur das Problem von bleibt immer noch. Egal wo ich draufdrücke, die Schüsse gehen irgendwie nicht dahin wo sie sollen. z. B. Ich drücke rechts unten in die Ecke dann geht der Schuss Rechts oben in die ecke.
 
Das wäre so ein Moment um mal den Debugger zur Anwendung zu bringen. Ist bei einigen verpönt, aber ich finde es praktisch um mal zu überprüfen ob in deinen Variablen auch steht was du erwartest.
 
LibGdx3.jpg
So ich bin schon ein bisschen weitergekommen.
Also die Maus-Y Koordinate fängt oben bei 0 an,
und die Koordinate wo ich zeichne fängt unten die y Koordinate bei 0 an.
Ich benütze libGDX und bekomme die Maus Koordinaten von "Gdx.input.getY()".
Also könnte man das irgendwie umdrehen oder sowas in der Art.
Liebe Grüße
 
dass das Projektil von einer Waffe zur Maus-Position fliegt.
1637012218342.png
Java:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

@SuppressWarnings("serial")
public class ShooterDisplay extends Display {
    private Cannon cannon;
    private BulletStack bullets = new BulletStack(5);

    public ShooterDisplay(int width, int height, int fps) {
        super(width, height, fps);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                Point p = e.getPoint();
                cannon.setDirection(cannon.getFirePoint(), p);
                bullets.add(new Bullet(cannon.getCenter(), 15, -cannon.getAngle()));
            }
        });
    }

    @Override
    protected void init(int width, int height) {
        int cannonW = width / 20;
        int cannonH = (int) (1.5 * cannonW);
        int x[] = new int[] { 0, cannonH, 0 };
        int y[] = new int[] { 0, cannonW / 2, cannonW };
        cannon = new Cannon(new Polygon(x, y, x.length), 1);
        Dimension dim = getPreferredSize();
        cannon.setPosition(dim.width / 2 - cannonW / 2, dim.height / 2 - cannonH / 2);
        cannon.setAngle(90);
    }

    @Override
    protected void update() {
        cannon.move();
        bullets.move();
    }

    @Override
    protected void updateGraphic(Graphics g) {
        bullets.draw(g);
        cannon.draw(g);
    }

}
[CODE lang="java" title="Shooter"]import javax.swing.JFrame;

public class Shooter {
public static void main(String[] args) {
ShooterDisplay game = new ShooterDisplay(500, 500, 80);
JFrame frame = new JFrame("Shooter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(game);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
game.start();
}
}[/CODE]
 

Anhänge


Zurück
Oben