# Vector Math in Java

• August 16th, 2011, 11:28 PM
Spidey1980
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.
• August 17th, 2011, 01:09 AM
Junky
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).
• August 17th, 2011, 11:17 AM
Spidey1980
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.
• August 17th, 2011, 01:18 PM
Sean4u
Re: Vector Math in Java
Quote:

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.
• August 17th, 2011, 02:07 PM
Spidey1980
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.
• August 17th, 2011, 02:53 PM
copeg
Re: Vector Math in Java
Quote:

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?
• August 17th, 2011, 03:06 PM
Spidey1980
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
• August 18th, 2011, 12:05 PM
Spidey1980
Re: Vector Math in Java
It does work, just had to weed and prune and fertilize a few things.