Hallo zusammen,
seit längerem beschäftige ich mich mit der Sprite-Bewegung eines Strategiespiels wie beispielsweise C&C.
Hierfür habe ich den Mittelpunkt des Sprites (hier ein Panzer) berechnet und möchte diesen möglichst linear auf die aktuell geklickte Mausposition zubewegen. Zuerst habe ich den Winkel zwischen Sprite und Maus berechnet mittels atan2 und schließlich die Geschwindigkeit xspeed bzw. yspeed mit Kosinus bzw. Sinus. Wenn ich nun den jeweiligen Speed zur Position hinzuaddiere pro Frame sollte sich das Sprite auch zum Mauszeiger hinbewegen, nur tut es das keineswegs. Stattdessen schwebt das Sprite nach Klick völlig unkoordiniert über das JLabel.
Hier meine Frage:
Wie kann ich erreichen, dass sich das Sprite linear zum Mauszeiger hinbewegt, dort stehenbleibt und keine seltsamen Drehungen mehr macht, sondern in der richtigen Richtung ausgerichtet bleibt?
Darüber hinaus würde mich interessieren wie man es realisiert, dass sich das Sprite zuerst um sein Zentrum dreht bis es die Bewegungsrichtung gefunden hat und dann erst losfährt?
Hier mein etwas rudimentärer Code:
Würde mich über Hilfe sehr freuen.
Eichelhäer
seit längerem beschäftige ich mich mit der Sprite-Bewegung eines Strategiespiels wie beispielsweise C&C.
Hierfür habe ich den Mittelpunkt des Sprites (hier ein Panzer) berechnet und möchte diesen möglichst linear auf die aktuell geklickte Mausposition zubewegen. Zuerst habe ich den Winkel zwischen Sprite und Maus berechnet mittels atan2 und schließlich die Geschwindigkeit xspeed bzw. yspeed mit Kosinus bzw. Sinus. Wenn ich nun den jeweiligen Speed zur Position hinzuaddiere pro Frame sollte sich das Sprite auch zum Mauszeiger hinbewegen, nur tut es das keineswegs. Stattdessen schwebt das Sprite nach Klick völlig unkoordiniert über das JLabel.
Hier meine Frage:
Wie kann ich erreichen, dass sich das Sprite linear zum Mauszeiger hinbewegt, dort stehenbleibt und keine seltsamen Drehungen mehr macht, sondern in der richtigen Richtung ausgerichtet bleibt?
Darüber hinaus würde mich interessieren wie man es realisiert, dass sich das Sprite zuerst um sein Zentrum dreht bis es die Bewegungsrichtung gefunden hat und dann erst losfährt?
Hier mein etwas rudimentärer Code:
Java:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public
[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] update([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#6a3e3e][SIZE=2][COLOR=#6a3e3e]xm[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#6a3e3e][SIZE=2][COLOR=#6a3e3e]ym[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]long[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#6a3e3e][SIZE=2][COLOR=#6a3e3e]delta[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]){[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]bounding[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]x[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2])[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]centerx[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]bounding[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]y[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2])[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]centery[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; [/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]winkel[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = Math.[I]toDegrees[/I]((Math.[I]atan2[/I]([/SIZE][SIZE=2][COLOR=#6a3e3e][SIZE=2][COLOR=#6a3e3e]ym[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]-[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]centerx[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],[/SIZE][SIZE=2][COLOR=#6a3e3e][SIZE=2][COLOR=#6a3e3e]xm[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]-[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]centerx[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])));[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]xspeed[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = Math.[I]cos[/I]([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]winkel[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]yspeed[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = Math.[I]sin[/I]([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]winkel[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]centerx[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] += [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]xspeed[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]centery[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] += [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]yspeed[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]
System.
[/SIZE][B][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/COLOR][/SIZE][/I][/B][SIZE=2].println([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]winkel[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2] }
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] BufferedImage rotate([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]double[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#6a3e3e][SIZE=2][COLOR=#6a3e3e]winkel[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]){[/SIZE]
[SIZE=2] AffineTransform
[/SIZE][SIZE=2][COLOR=#6a3e3e][SIZE=2][COLOR=#6a3e3e]at[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = AffineTransform.[I]getRotateInstance[/I](Math.[I]toRadians[/I]([/SIZE][SIZE=2][COLOR=#6a3e3e][SIZE=2][COLOR=#6a3e3e]winkel[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]),[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]pic[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getWidth()/2,[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]pic[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getHeight()/2);[/SIZE]
[SIZE=2] BufferedImage
[/SIZE][SIZE=2][COLOR=#6a3e3e][SIZE=2][COLOR=#6a3e3e]rotateImage[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] BufferedImage([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]pic[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getWidth(),[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]pic[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getHeight(),[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]pic[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getType());[/SIZE]
[SIZE=2] Graphics2D
[/SIZE][SIZE=2][COLOR=#6a3e3e][SIZE=2][COLOR=#6a3e3e]g2[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = (Graphics2D) [/SIZE][SIZE=2][COLOR=#6a3e3e][SIZE=2][COLOR=#6a3e3e]rotateImage[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getGraphics();[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#6a3e3e][SIZE=2][COLOR=#6a3e3e]g2[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setTransform([/SIZE][SIZE=2][COLOR=#6a3e3e][SIZE=2][COLOR=#6a3e3e]at[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#6a3e3e][SIZE=2][COLOR=#6a3e3e]g2[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].drawImage([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]pic[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],0,0,[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#6a3e3e][SIZE=2][COLOR=#6a3e3e]rotateImage[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2] }
[/SIZE]
Würde mich über Hilfe sehr freuen.
Eichelhäer