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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Vector implementation

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    Thanks in advance

    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. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Attached Files Attached Files

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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)
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    Thanks for your help.

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Vector implementation

    You definition of vector seems closer to that of a point. The x,y location of something.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Vector implementation

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

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Vector implementation

    I guess you can call it whatever you want.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Banned
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Vector implementation

    owrite lets gonna change

  12. #12
    Banned
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Vector implementation

    it work bro

Similar Threads

  1. Replies: 2
    Last Post: December 22nd, 2010, 09:21 AM
  2. Unit Vector
    By mingming8888 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 14th, 2010, 02:53 PM
  3. [SOLVED] 2D Vector
    By nasi in forum Collections and Generics
    Replies: 2
    Last Post: May 6th, 2010, 01:42 AM
  4. Help wih implementation...
    By Frank_nor in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 24th, 2009, 05:43 PM
  5. vector
    By sriraj.kundan in forum Java Theory & Questions
    Replies: 8
    Last Post: August 12th, 2009, 10:17 AM