Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


newbie help with a c++ problem

This topic is 5986 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all Just wondering if someone can help me with my math please.... float rise = ((SCREEN_HEIGHT/2) - yCoord); float run = (400 - xCoord); double CurrentAngle = atan(rise/run); float length = (float)Fast_Distance_2D(run + 0.5,rise + 0.5); float NewAngle = CurrentAngle + 1; rise = (cos(NewAngle)*length); run = (sqrt((pow(length,2))+(pow(rise,2)))); xCoord = ((SCREEN_HEIGHT/2 - 11) + run); yCoord = ((PLAYABLE_AREA/2 + 160 - 11) + rise); i''m trying to fing the xy coords of 1 degree next to the current one. Am i doing this right? Jimmy

Share this post

Link to post
Share on other sites
Well, I really can''t work through your code right now, but just for one hint, if you''re looking for 1 degree past the current, you''re going to have to do some conversions. Remember all the trig functions in C work with radians...

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
don''t use atan(rise/run), use atan2(rise,run), because atan is only defined for a small domain and if you use it you will have to throw in a conditional and so forth. Using atan2 fixes everything. Melraidin is right about radians. Don''t use degrees for anything, otherwise you''ll end up switching back and forth constantly, which is just stupid. So learn to do everything in radians. There are 2pi radians to a circle, so 45 degrees would be pi/4 radians for example. You can multiply degrees by pi/180 to get constants, like your "1" in this example. Just try to stick to radians as much as possible.

Your code has some strange parts. Are you calculating run using the pythagorean theorum? Neat trick, keep in mind that n*n is probably significantly faster than using power, function calls are costly, short loops are more costly than they seem(because of mispredicted branches), and so forth. The rest of your code seems to be application specific so I can''t comment.

Share this post

Link to post
Share on other sites
Well, here''s some sample code on how to calculate the XY Position plus one degree away, to get you started

#include <cmath>
#include <iostream>

using namespace std;

int main( void )
double dXorg, dYorg;
double dRadius;
double dAngle;

// Set Initial X,Y Coordinates
dXorg = 2.00;
dYorg = 2.00;

// Determine Radius
dRadius = sqrt(dXorg*dXorg + dYorg*dYorg);

// Calculate Angle from X-Axis
dAngle = atan2(dXorg,dYorg);

// Add one degree (notice I convert it toradians
// before addition
dAngle= dAngle + 1.00/360.00 * 2 * 3.14;

// Compute new x and y
cout << "New X is" << dRadius * cos(dAngle) << endl;
cout << "New Y is" << dRadius * sin(dAngle) << endl;

return 0;

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!