# [SOLVED] Using Vectors to Control Movement

• 12-27-2008, 11:01 PM
KingZebulon
[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
• 12-28-2008, 01:01 AM
neilcoffey
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.
• 12-28-2008, 01:24 AM
fishtoprecords
Quote:

Originally Posted by KingZebulon
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.
• 12-28-2008, 08:25 AM
hardwired
Here's an idea.
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:
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.
• 12-29-2008, 03:14 AM
KingZebulon
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