Vector Rotation [SOLVED]

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Vector Rotation [SOLVED]

Post 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
Last edited by Benjamin100 on Sun Nov 03, 2013 11:25 am, edited 1 time in total.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Vector Rotation

Post 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.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Vector Rotation

Post 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.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Vector Rotation

Post 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.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Vector Rotation

Post by K-Bal »

Benjamin100 wrote:Just made it round to the nearest thousandth.
How is that going to make it more accurate? ;)
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Vector Rotation

Post by Benjamin100 »

It won't, it will just stop the annoying long scientific notation.
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Vector Rotation

Post by qpHalcy0n »

The "annoying long scientific notation" is just a representation of the value. Rounding will only do you harm.

See error propagation.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Vector Rotation

Post by Benjamin100 »

OK. Thanks for the advice
Post Reply