Python’s atan and atan2 functions

This is needed to get an angle based on the rise and run of a vector. So I need this in situations where I need to aim say a turret, and a player sprite and fire. I need to calculate the vector’s rise y2-y1 and run  x2-x1 and put those two parameters into the atan2 function.

There are some things I can’t quite visualize yet but I’ll get to that.

First, there are positive and negative angles. Positive angles are when you go counter clockwise around the unit circle, and negative angles are when you go clockwise. Also, the angle in radians if you go counter clockwise starting from zero, will be the same as that angle if it were positive and going clockwise. enter image description here

so – 45 degrees is positive 315 degrees.

This is important because in aiming something like a turret in the game, having negative angles means you can turn it both ways, instead of having to turn 315 counter clockwise, it can just go clockwise -45 degrees.

So atan2 allows for this by having the parameters for rise and run separate. atan, another math. function, only accepts the already calculated ratio, and so if rise and run were both negative, the result would be positive.

Also for atan, this means the angle then could never be in the 4rd quadrant, the part of the cartesian plane where both y and x are negative.

So the issue in pygame is we a y axis that starts from 0 and goes down from there. So if y1 was 5 and y2 was 10, y2 would be lower than y1 on the screen. I’m trying to get an idea of why the options for fixing this work the way they do.

The first way people fix this is  dy = y1-y2 and then set dy to negative before passing it to atan2. atan2(-dy, dx).

The other way that works for some reason is just flipping the two numbers, atleast when using atan anyways, people are doing atan(dx/dy) and say that works…

I’m going to take a break from it for now but that’s something I’d like to figure out.

廣告

Collections

Lists: Ordered, Heterogeneous, Mutable

Strings: Ordered(“Sequential"?), Characters, Immutable

Tuples: Ordered, Heterogeneous, Immutable

Sets: Un-ordered, Heterogeneous, Immutable

Learning Python Through interactivepython.org

My Plan

I am studying the interactivepython.org website. Going to work through this at approx 5 – 10 minutes per day. I know it doesn’t sound like much, but I have enough background and I learn fast enough that it should be fine, and any more seems to lead me into a spiral of anxiety/depression about not learning enough fast enough. If I feel like doing more on some days I guess I might. Also I want to get really into the social aspect of it so it will be in my environment. I’m choosing Python over all the web dev stuff I was previously thinking about doing for a few reasons.

Reasons for choosing Python

1. I think to get the most bang for my buck in terms of accelerated growth, I need to pick one language and learn to do anything I want to do in that language.

2. The python community has a philosophy of collaboration and support which is more inline with my values(as well as I think the values of science in general) and should help me stay motivated and learn faster, and also they have a lot of information on a lot of interesting areas like game development, so if I ever need a change I can still stick with the same language.

So here is my journal for the first day:

So I really just skimmed the intro until the section about atomic data types and collection data types. I like that it introduces a lot of the functions that are going to be used but I’m also anxious to get to the actually problem solving part. Slowly but surely I guess.

I feel that for me, my retention will depend on keeping up with this over a long period of time, so I’m not gonna worry about how much I learn on any given day. I skimmed a bit more just to see what was coming up and it looks like the book is heading in the right direction for me.

I hope you enjoyed reading this!

Feel free to talk to me, let me know what you’re doing, etc!