So I am trying to build a top down space shooter in java.
I am fairly new but I have a good grasp on it having perfected making pong. My main problem is that I can't seem to get rotation down.
I want to be able to press right or left on the arrow keys and have, say a basic rectangle, rotate around its center in the direction the user is inputting.
Once I have that down I want to be able to press forward or backward on the arrow keys to make it go in the direction it is currently facing.
Everything I have tried so far affected everything in the frame instead of just a singular object. Having come from as2, rotation and objects are very different in java.
Can someone help explain to me how I would do this?
package spaceroyalgame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.geom.AffineTransform;
public class SpaceRoyalGame extends JPanel implements KeyListener {
static double shipRotation=0, shipRotationSpeed=0, shipSpeed=0, TOPSPEED= 20;
static int shipy=50, shipx=50, shipWidth = 10, shipHeight = 20;
static int WorldWidth = 700, WorldHeight = 700;
static boolean slowdown = true;
public static void main(String[] args) throws InterruptedException {
SpaceRoyalGame game = new SpaceRoyalGame();
JFrame frame = new JFrame();
frame.add(game);
frame.setSize(WorldWidth,WorldHeight);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addKeyListener(game);
while(true){
game.moveShip();
game.repaint();
Thread.sleep(10);
}
}
public void moveShip(){
if (shipSpeed != 0) {
shipx += shipSpeed;
}
if (shipRotationSpeed != 0) {
shipRotation += shipRotationSpeed;
}
}
public void paint (Graphics g){
super.paint(g);
setBackground(Color.black);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.white);
Rectangle ship = new Rectangle(shipx,shipy,100,50);
g2d.rotate(Math.toRadians(shipRotation));
g2d.draw(ship);
g2d.fill(ship);
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
slowdown = false;
if (keyCode == KeyEvent.VK_W) {
//System.out.println("W is pressed");
shipSpeed = -2;
}
if (keyCode == KeyEvent.VK_S) {
//System.out.println("S is pressed");
shipSpeed = 2;
}
if (keyCode == KeyEvent.VK_A) {
System.out.println("A is pressed");
shipRotationSpeed = 1;
shipRotation += shipRotationSpeed * (shipSpeed/TOPSPEED);
}
if (keyCode == KeyEvent.VK_D) {
System.out.println("D is pressed");
shipRotationSpeed = -1;
shipRotation += shipRotationSpeed * (shipSpeed/TOPSPEED);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
shipRotationSpeed = 0;
shipSpeed = 0;
}
}
Try using Vectors . Since 'A' and 'D' keys are modifying your current angle so you should keep a variable to keep track of angle.
From there:
x += speed * cos(θ)
y += speed * sin(θ)
Also if you want to be able to accelerate/decelerate:
Δx = speed * cos(θ) * Δt + 1/2 * acceleration * Δt^2
Δy = speed * * sin(θ) * Δt + 1/2 * acceleration * Δt^2
Δt would be the time between the last frame.
It might be worth taking a look into Physics/Kinematics
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.