Jump to content
  • Advertisement
Sign in to follow this  
ridefast42

inverse tan

This topic is 4242 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

I am making some collision stuff and I am calculating the angle of the "collision line". the lines are made with two coordinate pairs:(xa,ya) is the start of the line and (xb,yb) is the end of the line. This is not working and I am guessing the it has something to do with this function because if plug in the angle directly into my physics it works fine.So here is my code, the only thing that I know of that might be wrong with my math is maybe atan() is not the inverse tangent.
void collision::createcollisionline(int xa, int ya, int xb, int yb, float surfriction)
{
	float angle;
	float number;
	collision_props newprop;

	number = (yb-ya)/(xb-xa);
	angle = atan(number);

	allcols.push_back(newprop);
	allcols.back().x1 = xa;
	allcols.back().y1 = ya;
	allcols.back().x2 = xb;
	allcols.back().y2 = yb;
	allcols.back().collision_angle = angle;
	allcols.back().surface_friction = surfriction;
}

Share this post


Link to post
Share on other sites
Advertisement
Short answer: use atan2().
angle = atan(yb-ya,xb-xa);
Also, most collision detection and physics problems are better expressed using vector math than with angles. If you can revise your code accordingly, it may be that you won't need to store the angle at all.

Share this post


Link to post
Share on other sites
atan, like the actual inverse tangent, gives you a reference angle. I believe C++ has an atan2(y,x) which will give you the answer you are expecting. (I didn't heavily analyze your code, I'm just guessing since this is a common error). I find using angles and trig functions to be much slower than using vector operations to resolve collissions. However, I can't tell enough from your post to suggest how to do it that way at this time.

edit: beat me by three minutes...curse my verboseness
edit: fixed the order of arguments for atan2

[Edited by - Alrecenk on October 16, 2006 8:47:50 AM]

Share this post


Link to post
Share on other sites
If you were to use vectors, it would simply be Vector(xb-xa,yb-ya) which would represent your collision line [assuming x and y go along standard axes and are not pixel values... in the latter case you will have to use ya-yb because pixels are numbered downward positive on the Y-axis]. Also, if you were to use atan2, remember it is atan2( y, x ) [correcting the above poster's order of arguments].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!