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
Benjamin100
ES Beta Backer
Posts: 250 Joined: Tue Jul 19, 2011 9:37 pm
Post
by Benjamin100 » Wed Oct 30, 2013 10:12 am
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.
Falco Girgis
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:
Post
by Falco Girgis » Wed Oct 30, 2013 11:20 am
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
Posts: 250 Joined: Tue Jul 19, 2011 9:37 pm
Post
by Benjamin100 » Wed Oct 30, 2013 1:07 pm
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
Posts: 250 Joined: Tue Jul 19, 2011 9:37 pm
Post
by Benjamin100 » Wed Oct 30, 2013 1:21 pm
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
Posts: 701 Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:
Post
by K-Bal » Wed Oct 30, 2013 5:19 pm
Benjamin100 wrote: Just made it round to the nearest thousandth.
How is that going to make it more accurate?
Benjamin100
ES Beta Backer
Posts: 250 Joined: Tue Jul 19, 2011 9:37 pm
Post
by Benjamin100 » Wed Oct 30, 2013 5:28 pm
It won't, it will just stop the annoying long scientific notation.
qpHalcy0n
Respected Programmer
Posts: 387 Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:
Post
by qpHalcy0n » Wed Oct 30, 2013 11:01 pm
The "annoying long scientific notation" is just a representation of the value. Rounding will only do you harm.
See error propagation.