Jump to content

Math.Atan() for the full 360 degree?

- - - - -

  • Please log in to reply
4 replies to this topic

#1
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
I have a program that simulates a ball being thrown. In addition to the simulation it also calculates the terminal velocity and its angle in advance.

This is what the code looks like right now:

double angle = Math.Atan(terminalVelocityY / ball.CurrentSpeedX) * 180 / Math.PI;

It doesn't work for the full 360 degree because the Arctangens repeats after 180 degrees.

Does anyone know how to solve this? Is there a two-parameter version for this method hidden somewhere, because that would work. The error stems from the method not knowing the individual signs of the two numbers, as it passes those along as one number, with one sign. i.e., a movement with the coordinates (2, 3) will be treated the same as one with (-2, -3).

#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
Not sure what the problem here is. The ball can only go straight up (90 degrees) to straight down (-90 degrees). Whether it is going to the left or right is determined by ball.CurrentSpeedX (from what you've shown and from what I can guess).

#3
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
if it goes to the left, the angle should be between 90° and 270°.

#4
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts

anotheruser said:

if it goes to the left, the angle should be between 90° and 270°.

I think you are having issues with understanding what a tangent is. Since it's based on the slope of the tangent line, it has no left/right direction, just up/down. If you want to mirror it across the y axis, you'll have to make your own adjustment (add the result to 180) when you are moving in a negative direction.

#5
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
I had tried that already. It doesn't work for some reason.

actually, I just tested it again and suddenly it works. I didn't make any changes to the code but it works now.

I must have been confused when finding this "error" that doesn't actually exist.:confused:

This thread can be closed, sorry for the waste of time.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users