Page 1 of 1

Vector Rotation [SOLVED]

Posted: Wed Oct 30, 2013 10:12 am
by Benjamin100
Hello.
I'm trying to rotate two vectors around the origin.
I continue to get very strange numbers that don't make any sense.
Is there anything notably incorrect about this rotation function?

Code: Select all

void rotateVectors(double rad) //Rotation must be in radians.
{
	va[0]= cos(rad)*va[0]  -sin(rad)*va[1] ; //vector a, x coordinate.
	va[1]= sin(rad)*va[0] +cos(rad)*va[1];
	vb[0]= cos(rad)*vb[0] -sin(rad)*vb[1];  //vector b, x coordinate.
	vb[1]= sin(rad)*vb[0] +cos(rad)*vb[1];
}
Thanks,
Benjamin

Re: Vector Rotation

Posted: Wed Oct 30, 2013 11:20 am
by Falco Girgis
Yep, you got a problem, homie.

After you calculate the x component of the vector, you have modified it before using it to calculate the y component of the vector.

Use a temporary vector, and assign it to your original vector after the calculation is done.

Re: Vector Rotation

Posted: Wed Oct 30, 2013 1:07 pm
by Benjamin100
Ah! Thanks, Falco.

Also, I seem to be having some trouble with the cos() function.
It isn't calculating it like my calculator does.
I'll have to figure it out.

Re: Vector Rotation

Posted: Wed Oct 30, 2013 1:21 pm
by Benjamin100
OK, appears the result of the cos() function is only slightly inaccurate. It gets it off by a very small point. Not quite sure how to fix that.

EDIT: Just made it round to the nearest thousandth.

Re: Vector Rotation

Posted: Wed Oct 30, 2013 5:19 pm
by K-Bal
Benjamin100 wrote:Just made it round to the nearest thousandth.
How is that going to make it more accurate? ;)

Re: Vector Rotation

Posted: Wed Oct 30, 2013 5:28 pm
by Benjamin100
It won't, it will just stop the annoying long scientific notation.

Re: Vector Rotation

Posted: Wed Oct 30, 2013 11:01 pm
by qpHalcy0n
The "annoying long scientific notation" is just a representation of the value. Rounding will only do you harm.

See error propagation.

Re: Vector Rotation

Posted: Sun Nov 03, 2013 11:24 am
by Benjamin100
OK. Thanks for the advice