I am trying to learn some more about vectors and rotation in Java by making a simple asteroids game. I have been trying out the Vector2d class, but I feel like I could have done it just using Points and doubles. Is my use of the Vector2d redundant? How would you change my program?
Source:
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.vecmath.Vector2d;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Asteroids extends JPanel implements Runnable{
BufferedImage img;
boolean up, left, right;
double angle = 0;
int imgw = 0;
int imgh = 0;
int magnitude = 0;
JFrame f;
ArrayList<Projectile> bullets;
Vector2d rocket = new Vector2d();
Vector2d Magnitude = new Vector2d();
AffineTransform at = new AffineTransform();
public Asteroids(JFrame f){
this.f = f;
setSize(900, 800);
try {
img = ImageIO.read(new File("res/rocket.png"));
} catch (IOException e) {}
imgw = img.getWidth();
imgh = img.getHeight();
bullets = new ArrayList<Projectile>();
f.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_UP:
up= true;
break;
case KeyEvent.VK_LEFT:
left = true;
right = false;
break;
case KeyEvent.VK_RIGHT:
right = true;
left = false;
break;
}
}
public void keyReleased(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_UP:
up= false;
break;
case KeyEvent.VK_LEFT:
left = false;
break;
case KeyEvent.VK_RIGHT:
right = false;
break;
case KeyEvent.VK_SPACE:
Vector2d dir = new Vector2d(Math.cos(angle-Math.toRadians(90)),Math.sin(angle-Math.toRadians(90)));
bullets.add(new Projectile(dir,rocket));
break;
}
}
});
Thread t = new Thread(this);
t.start();
}
public static void main(String[] Args){
JFrame frame = new JFrame();
BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");
frame.getContentPane().setCursor(blankCursor);
frame.add(new Asteroids(frame));
frame.setVisible(true);
frame.setSize(900,500);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.fillRect(0,0,this.getWidth(),this.getHeight());
g2.drawImage(img,at,this);
g2.setColor(Color.red);
for(Projectile p: bullets){
p.draw(g2);
}
}
public void run(){
rocket = new Vector2d(100, 400);
Magnitude = new Vector2d(Magnitude = new Vector2d(magnitude*Math.cos(angle-Math.toRadians(90)),magnitude*Math.sin(angle-Math.toRadians(90))));
while(true){
if(left){
angle-=Math.toRadians(5);
}
if(right){
angle+=Math.toRadians(5);
}
if(up){
Vector2d m2 = new Vector2d(Math.cos(angle-Math.toRadians(90)),Math.sin(angle-Math.toRadians(90)));
m2.normalize();
m2.x/=5;
m2.y/=5;
Magnitude.add(m2);
}
if(angle>=Math.toRadians(360)){
angle-=Math.toRadians(360);
}else if(angle<=Math.toRadians(-360)){
angle+=Math.toRadians(360);
}
Vector2d rocketN = new Vector2d(rocket);
rocketN.normalize();
rocket.add(Magnitude);
System.out.println(Math.toDegrees(angle));
at.setToTranslation(rocket.x-img.getWidth()/2, rocket.y-img.getHeight()/2);
at.rotate(angle, imgw/2, imgh/2);
Magnitude.x*=0.99;
Magnitude.y*=0.99;
for(Projectile p: bullets){
p.update();
}
repaint();
try {Thread.sleep(25);
} catch (InterruptedException e) {}
}
}
public class Projectile{
int magnitude;
private Vector2d Magnitude, position;
public Projectile(Vector2d dir, Vector2d Position){
this.position = new Vector2d(Position);
magnitude = 10;
Magnitude = new Vector2d(dir);
position.x+=(5*Magnitude.x);
position.y+=(5*Magnitude.y);
Magnitude.x*=magnitude;
Magnitude.y*=magnitude;
}
public void update(){
position.add(Magnitude);
}
public void draw(Graphics2D g2){
g2.drawOval((int)position.x, (int)position.y,4,4);
}
}
}
Currently there is no asteroids to shoot, you can just move around and shoot. Here is the Image I used: http://findicons.com/files/icons/1495/space/32/rocket_ship.png
I think that not (answer to your question)
but most important is there wrong usage of KeyListener, use KeyBindings instead, example for Keys UP, DOWN, LEFT, RIGHT
(I’m not good in Graphics2D) but another issues could be caused by method
public void draw(Graphics2D g2){
check this thread how to move with Graphics2D, especialy answer by @trashgod