Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7611567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:41:37+00:00 2026-05-31T01:41:37+00:00

I am trying to learn some more about vectors and rotation in Java by

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:41:38+00:00Added an answer on May 31, 2026 at 1:41 am
    • 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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im am trying to learn some more about vectors and rotation so I can
I have been trying to learn a bit more about delegates and lambdas while
I have been trying to learn Erlang and have been running into some problems
I am currently trying to learn some more about web design and I used
I'm trying to learn more about 'ajax' and so far have had the most
I'm trying to dissect some well designed sites, in order to learn more about
In my bid to try and learn some more about WPF I've been looking
Just trying to ping up some experienced Thread gurus out there...trying to learn more
I've been lately trying to learn more and generally test Java's serialization for both
In an effort to learn more about php and mysql, I have been working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.