Advertisement Jump to content
Sign in to follow this  

inverse tan

This topic is 4481 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.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
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, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!