Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Vector implementation

Hi,

I'm currently studying Java and have an assignment to create a pool game. I have the bulk of the work done but I am having issues with implementing 2d vectors. I have not been shown this and am trying to work my way through it but cannot find anything which gives suitable explanations for the 2d vector which I am trying to create. I have written a vector class which I have displayed below but need to implement it in my main class. I need to divide the table into a number of squares of my choosing. I then must perform addition of the vectors and determine the new position of the ball objects. I hope someone can help me as I am at a loss at the moment. Also if there is anything else which I should add suggestions will be greatly appreciated. If anyone wants to look at my other classes then they are more than welcome to.

poolMan81

```import java.awt.*;
import java.util.*;

//Vector class which contains the code for performing the collisions between the balls
public class Vector2 {

private double x, y;

public Vector2(double x, double y){

this.x = x;
this.y = y;

}

public double getX(){

return this.x;

}

public double getY(){

return this.y;

}

public void setVector2(double x, double y) {

this.x = x;
this.y = y;

}

public double magnitude(){

return Math.sqrt(x * x + y * y);

}

public static Vector2 addVector (Vector2 a, Vector2 b) {

double sumX = a.getX() + b.getX();
double sumY = a.getY() + b.getY();
return new Vector2 (sumX, sumY);

}

public static Vector2 scaleVector(double scale, Vector2 d) {

return new Vector2 (scale * d.getX(), scale * d.getY());

}```

2. ## Re: Vector implementation

I'm honestly not sure what your actual question is. You might consider posting an SSCCE that demonstrates exactly what problem you're encountering.

3. ## Re: Vector implementation

Sorry about that. My question really is how do I go about implementing this code within my main and or other classes in order to divide my table into vectors? These vectors will then be used to determine the position of the balls before the next shot is taken. I have attached my code which contains 3 classes. Please ignore the comments as I have to enter them for my project.

4. ## Re: Vector implementation

People are pretty wary of downloading zip files like that. An SSCCE should be able to be copied and pasted into our own editors so we can see what you're talking about.

But I would expect you would have to create instances of your Vector class, passing in the appropriate information. Recommended reading: Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

5. ## Re: Vector implementation

2d vector which I am trying to create.
Can you explain what a two dimensional vector is? Would it be the same as the Java SE Vector class except it would take two index values in place of one? For example:
public E get(int index1, index2)
public boolean add(int index, E e)

6. ## Re: Vector implementation

Thanks that's a great document. I understand how to give the initial coordinates but how do you create the size of each square and the amount of squares on the plane? Is this done individually with the add() method? Sorry if this seems like a stupid question but I've never used vectors before so just trying to get my head around it.

7. ## Re: Vector implementation

Yes a 2d vector takes to values. The x and y coordinates of an object. As for the similarities of the 2 I can't say as I'm new to vectors myself.

8. ## Re: Vector implementation

You definition of vector seems closer to that of a point. The x,y location of something.

9. ## Re: Vector implementation

Yeah a point can be used too for my problem but vectors were suggested by my lecturer.

10. ## Re: Vector implementation

I guess you can call it whatever you want.

11. ## Re: Vector implementation

owrite lets gonna change

it work bro