Bluetiger6001
Bluetiger6001

Reputation: 149

Python (pygame) Sin, Cos and Tan

I noticed there was a sin, cos and tan function in Python.

So, I thought I would use these to make a way of aiming in my game, unfortunately, the word description of sin,cos,tan,asin,acos and atan are very confusing.

I know how to do all the sin, cos and tan rules from school, I just need to apply them to the code. So, here's what I need to do, I just need to know which one I must use:

I have

  1. The Angle
  2. The Hypotenuse
    (I'm just keeping that the value of how far I want the object to travel before I blit it again)

From the angle, I want to work out either/both the opposite and adjacent.
The hypotenuse is going to be sin/asin and cos/acos. Which one? I don't know.

How to I input my numbers? Do I just do aim = cos(angle,hyp) or do I have to apply some other calculations?

Upvotes: 1

Views: 10113

Answers (2)

rodrigo
rodrigo

Reputation: 98526

Your wording is a bit confusing... but what I understand is that you have a point in the 2D space and you want to advance it a particular distance (hypotenuse) aiming a specified angle above the horizon. If so:

newX = oldX + dist * cos(angle)
newY = oldY + dist * sin(angle)

That assumes that angle is in radians, that the Y axis is positive upwards and that the angle is 0 aiming to the right and PI/2 to the top. If these are not the case you may need to wiggle the signs a little.

Upvotes: 2

NPE
NPE

Reputation: 500953

The formulae are:

adjacent = hypothenuse * math.cos(angle)
opposite = hypothenuse * math.sin(angle)

where angle is in radians.

Upvotes: 4

Related Questions