Results 1 to 5 of 5
  1. #1
    KingZebulon's Avatar
    KingZebulon is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default [SOLVED] Using Vectors to Control Movement

    Hello all!

    I am trying to create a program that accepts an angle and a speed from the user and then shoots a ball at that angle and at that speed, slowing down as it goes. I have a program that does close to that, but my current method is not accurate at all. The movement of the ball is determined by my "slope calculator". This is a separate class that creates a triangle from the ball's X position, Y position, and launch angle. It then finds the slope of the hypotenuse and moves the ball up and over accordingly. After a certain number of loops, the values are decremented, causing the ball to slow down and eventually stop. This is not realistic at all, however, and adding more balls to the table would be incredibly difficult.

    My programming teacher told me to use vectors instead. By using vectors, I could create a vector representing friction to slow the ball down, and I could also add other balls to the screen to have them interact. This would be done through vector addition. He said vectors would make the program more accurate and more versatile.

    MY PROBLEM: I have no idea how to implement vectors in my code. Is there a Java class that is specifically made for vectors? Is there perhaps another way to approach this problem?

    Any help is greatly appreciated.

    Cheers

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    If you plan to make your program more complicated, then yes using vector maths will be more versatile, although in principle you can also do it by calculating in terms of separate components as you're trying to do -- you just need to do it right!

    The standard Java3D framework includes classes for manipulating vectors. Otherwise, I'm sure if you Google, you'll find a squillion other packages.

  3. #3
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by KingZebulon View Post
    My programming teacher told me to use vectors instead. By using vectors, I could create a vector representing friction to slow the ball down, and I could also add other balls to the screen to have them interact. This would be done through vector addition. He said vectors would make the program more accurate and more versatile.

    MY PROBLEM: I have no idea how to implement vectors in my code. Is there a Java class that is specifically made for vectors?
    There is a Vector class in Java, but it is not what you want. Not at all. The Java Vector class is a dymanic array.

    What your teacher wants you to use is polar processing, where you deal with angles and speed. The polar processing can be 2D or 3D, (even 4D if you want) depending on your domain.

    In 2D, a vector is just an angle and a magnitude. Its easy to implement.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Here's an idea.
    Java Code:
    public class YourViewComponent extends JPanel {
        Vector2 v;
        double friction = 0.025;
    
        public YourViewComponent() {
            v = new Vector2(10.0, 300.0, -Math.PI/4, 10.0);
        }
    
        protected void paintComponent(Graphics g) {
            v.draw(g);
        }
    
        inside your animation cycle {
            v.step(friction);
            repaint();
            pause
        }
    
        public static void main(String[] args) {
            show this component
            start animation cycle
        }
    }
    
    class Vector2 {
        double x;
        double y;
        double theta;
        double speed;
    
        public Vector2(double x, double y, double theta, double speed) {
            this.x = x;
            this.y = y;
            this.theta = theta;
            this.speed = speed;
        }
    
        public void step(double friction) {
            move ahead in theta direction
            adjust speed with friction
        }
    
        public void draw(Graphics g) {
            draw this into the graphics context
        }
    }
    Or you might try something like this:
    Java Code:
    public class YourViewComponent extends JPanel {
        Ball ball;
        Vector2 friction;
        double coefOfFriction = -0.025;
    
        public YourViewComponent() {
            ball = new Ball();
            v = new Vector2(10.0, 300.0, -Math.PI/4, 10.0);
            friction = new Vector2(0, 0, 0, coefOfFriction);
        }
    
        protected void paintComponent(Graphics g) {
            ball.draw(g);
        }
    
        inside your animation cycle {
            ball.step(friction);
            repaint();
            pause
        }
    }
    
    class Vector2 {
        double x;
        double y;
        double theta;
        double speed;
    
        public Vector2(double x, double y, double theta, double speed) {
            this.x = x;
            this.y = y;
            this.theta = theta;
            this.speed = speed;
        }
    
        public void add(Vector2 v) {
            this.x     += v.x;
            this.y     += v.y;
            this.theta += v.theta;
            this.speed += v.speed;
        }
    }
    
    class Ball
        Vector2 v;
    
        public Ball(Vector2 v) {
            this.v = v;
        }
    
        public void step(Vector2 fv) {
            adjust v speed with friction:
            v.add(fv);
            move v (x,y) ahead in theta direction
        }
    
        public void draw(Graphics g) {
            draw this into the graphics context
        }
    }
    which might be more in line with what your programming teacher had in mind.

  5. #5
    KingZebulon's Avatar
    KingZebulon is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default

    Thank you!

    Your code is incredibly helpful, hardwired. Thank you very much everyone for your help. I think I have an idea of what I need to do to make this program work now. I will give it a shot and see how it goes!

    Cheers

Similar Threads

  1. Snake game movement
    By BeerMonkey in forum New To Java
    Replies: 9
    Last Post: 11-27-2008, 01:48 PM
  2. Detecting user movement of a JFrame
    By dklett in forum AWT / Swing
    Replies: 4
    Last Post: 08-27-2008, 08:01 AM
  3. control app width based on certain control
    By thebillybobjr in forum SWT / JFace
    Replies: 0
    Last Post: 05-15-2008, 05:52 PM
  4. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 03:57 PM
  5. Movement of balls
    By BlitzA in forum New To Java
    Replies: 8
    Last Post: 01-09-2008, 04:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •