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 8 of 8

Thread: Vector Math in Java

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Vector Math in Java

    I have in other threads posted problems and have received a great deal of help and good feed back from the old timers here. I want to thank you all for that and I have just one remaining issue.

    I was working on translating and old QBasic program to Java I finally got my code to compile AND run. I then went back to the original QB code for comparison trying to get the vector math to work correctly because it did not work as expected. I did notice some typos in my java algorithms, and now the math looks exactly the way it did in the QB code, but the output still does not look right.

    My question is, is java math the same as QB math? namley, does java.sqrt() work out the square root the same way that sqrt() did in QB?

    here is the math:

    I need to move an object(star) at x,y on a line from the center of the screen to the edge of the screen. To do this my data uses a 0,0 @ center grid.

    So to get the angle I do:

    angle = java.sqrt((x1*x1)+(y1*y1))

    This is the same as:

    angle = java.hypot(x1,y1)

    the new position is relative to the old one, so:

    x2 = x1 / angle
    y2 = y1 / angle

    since this new position is relative, I have to add it to the old position to get the real new position, so:

    x1 = x1 + x2
    y1 = y1 + y2

    Then I translate it all to a 0,0 @ top left grid by adding half the grid width to both x and y (newX = x + (width / 2)) only when I need to draw the star.

    This works real perfect in QBasic but the stars do not move correctly in java. If someone here knows vector math, I would really like some help. I did not post the code because the code works fine. I will continue to look for errors in my vector math, but if java does not calculate things in the same order as other languages or something else equally as silly please let me know.
    Last edited by Spidey1980; August 16th, 2011 at 11:30 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Vector Math in Java

    The only thing I can think of that might be an issue is precision. Remember that binary is not good at representing floating point values. So the square root of 8 might be 2.8284 or it might be 2.8283999998.

    Try doing a comparison between the 2 languages. Set up a simple loop that displays the sqrt of the numbers 1 - 20 (or some other reasonable value).
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: Vector Math in Java

    Lol I hate dos box (neither compiled QB executables nor the compiler are not compatible with any OS past Win Vista) but Ok. And i don't think that's the issue. In both languages I have to do (int)(angle) anyways. The angle does not need to be perfect, it just need to be a straight line.

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Vector Math in Java

    And i don't think that's the issue
    Junky's right. If you port some code and it doesn't do what it originally did, then you have to isolate the cause of the discrepancy. If eyeballing the code hasn't worked, and nobody knows the secret feature of Java that makes sqrt(x) = x/2, then your one and only hope is to write two very small versions of the problematic code, add print statements to show the line-by-line values of all your variables and compare the outputs from the two. It doesn't need to be a loop or anything, just start with your pre-math values and apply your math steps and terminate. After each step, print everything that might have changed.

    It's an interesting issue - when you find out where the problem occurs, post your sources and output.

  5. The Following User Says Thank You to Sean4u For This Useful Post:

    Spidey1980 (August 17th, 2011)

  6. #5
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: Vector Math in Java

    Thank you that makes so much sense....

    Point to note: if I drop the object class and methods and make it a straight forward main loop; it works fine...I believe it's something to do with the order things are executed in, my moveStar method includes an "if (isValid == true) then moveStar else restart star"... this should probably be moved to a separate method. This would clean things up a bit and make it easier to read; as well a straightening out the order of things. I think isValid is always false for some reason.

    Currently it's "if star inside valid locations the moveStar else restart star". If I make it separate then it would be "if star outside valid locations then restart star else doNothing" followed by a call to moveStar.
    Last edited by Spidey1980; August 17th, 2011 at 02:10 PM.

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Vector Math in Java

    angle = java.sqrt((x1*x1)+(y1*y1))
    Perhaps I've forgotten my math or just misunderstand the problem, but I'm confused...how does calculating the length of the hypotenuse (the right side of the equation above) equal the angle?

  8. #7
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: Vector Math in Java

    I forgot to say if (angle == 0) then angle = 1

    Let's say a star is currently at 3,3. the integer of the Hypotenuse would be 4. int(3/4) = 0, angle defaults to 1. then new location is one pixel away, 3+1,3+1 = 4,4.

    Let's say a star is at 11,15. Then integer of the hypotenuse is 18. int(11/18) && int(15/18) both = 0, so angle again defaults to 1 and the new location is 12,16.

    this works for -x,-y locations as well.

    oops I guess your right the starAngle variable is miss-named. it should be starVector
    Last edited by Spidey1980; August 17th, 2011 at 09:12 PM.

  9. #8
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: Vector Math in Java

    It does work, just had to weed and prune and fertilize a few things.

Similar Threads

  1. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM
  2. help w/ PI Math java program
    By robertsbd in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 16th, 2011, 09:43 PM
  3. Replies: 2
    Last Post: December 22nd, 2010, 09:21 AM
  4. Help need on math java program
    By zidangus in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 6th, 2010, 07:41 PM
  5. Math in Java?
    By [Kyle] in forum Java Theory & Questions
    Replies: 3
    Last Post: September 23rd, 2009, 12:21 PM