# Trigonometry for programming enemies and trajectories

Starting from this site: http://www.petercollingridge.co.uk/book/export/html/6

I get to the part where he defines:

def move(self):
self.x += math.sin(self.angle) * self.speed
self.y -= math.cos(self.angle) * self.speed

I am working on my own little project at the same time and my issue is finding a way to make a circle of enemies close in on the player at their fixed angles. (Like in the first level of Axley for snes!)

On my way home from the library my idea was that the x1-x2/y1-y2 would give a ratio, and then I can subtract a fixed amount from I guess x, and then multiply y by the ratio so that x and y keep the same ratio which should mean the same angle.

However, I want to get this trig stuff cause I feel it will keep being important and in my face. So I wiki’d it and found, „And for a given angle, cos and sin give the respective x, y coordinates on a unit circle.“

Then I wiki’d „unit circle“ and found out that it is like a circle generalized to a radius of one and the angle is at the 0,0 in Cartesian format so the middle of an x,y axis. So sine(self.angle) gives the x value out of a radius of 1, and cos(self.angle) the same, for a given angle. This means that the self.x and self.y that result will be…I can’t think of the word but it’s like generalized lol. It’s that it’s based on a standardized value and has nothing to do with the size of your grid or screen or anything. It’s x and y values based on a unit circle with radius 1. This might help. So the max y value is 1, the min y value is also 1. Any other values are somewhere in between including the values calculated in our example.

ok, so self.angle is in radians. So with a fixed angle being initialized, the x and y values, will also be fixed as generalized x and y values. Then incrementing by the sin(self.angle) and cos(self.angle) will result in movement along the fixed angle (self.angle) because incrementing the x and y based on their ratio to a fixed angle maintains that angle.

self.angle = math.pi/2

Make the angle pi/2 = 90 degrees , and you will have a (y) value of zero because 90 degrees is completely horizontal. Oh, and hear I based on how the angle is drawn here startingat the y line and going down. From there 90 degrees is hitting the x line and 180 is hitting the y line at the bottom. The picture above this one has 90 degrees as the y line which is different I guess.

# Path Finding Algorithms + Animation (Mega Man X Study)

This is based on the spider boss of Mega Man X.  The most interesting feature of that boss was that it would produce a web which is basically a graph, and then in would chose almost at random, a path down the web. I say almost because it would sometimes choose a less efficient path to hitting the player, but other times it would seems to choose the shortest path to the player and that made that boss very interesting to fight and even a bit scary.

So I am trying to reproduce that idea with pygame. What I have so far is a black sprite representing the spider, and green gridblocks representing the traversable web, and red representing not part of the web. If you press 4, the shortest path from the hard coded start position, to the hard coded end position, calculated using the basic shortest path dfs algorithm I recently learned. Then the sprite is updated as with position changes based on stepping through the path list.

This path is from (2,0) to (0,5). (x, y)

Then I have an even more basic find first path algorithm. So if you press 3 the spider sprite will just take that path which is longer.

# Success? On finding a path through a grid.

btw, good song: Words You Said…I’ll Never Forget You Now by Hammock

# Attempts at Path-finding: Some pictures of what I have so far

I have this one graph I generated but it seems to big and finding the shortest path on it just takes way way way too long, I never waited to see how long would actually take. I know there is a way to calculate that but I haven’t learned that yet.

Next is after following this tutorial:        http://programarcadegames.com/index.php?chapter=array_backed_grids

Oh and btw I didn’t follow the tutorial exactly because I try to use classes whenever I can now to get practice. After that I decided to implement path finding with this grid as the basis of a graph. What I need to do tomorrow is use xy tuple coordinates instead of the way I did it which was to add the row and column to try and get an index out of 100. Oh damn I just realized, maybe I need to multiply them instead of add to get an index out of 100 row of 1 to 10 and columns of 1 to 10 yeilds 100 spaces but trying to create indexes by adding the row and column space doesn’t yeild 10o unique positions…iunno I’m tired. lol, so here are the pictures in order of my introductions to them. The over-sized graph The result of finding just one path on the oversized graph from a start node to an end node. The 10X10 grid I turned into a graph and added values 0-2 and colors based on the values. The found a path from 1 -I think 15?

# Attempts at Pathfinding : Argg!!! The little technicalities

I have been spending the last few days wrapping my head around basic path finding with recursion for backtracking, and also stumbled across path finding with just for loops and a queue.

I have been trying to add the path-finding into both my games, and another really interesting idea I have which should actually be easier.

My stumbling points today have actually just been little python technicalities involving dictionaries, tuples, un-hashables, and for row in list meaning „row“ is actually part of a list instead of an int making it unhashable in dictionaries if I use the same variable again later etc etc. Stuff like that took up the whole day lol.

Also using a dictionary to hold coordinates as keys and as values for keys has been slow because of the above I guess.

Also just learning the best way to implement a graph and how the map should work with the grid.

# Space Game I’m working on: Farscape Inspired: Path Finding

I am working on a game based on Farscape where you get to fly a prowler as Commander Craise’s brother and try to not get killed by John Chriton’s „death-pod“. I plan to have a single Module appear along with a black hole sprite and the ship will be attracted to them both thus following the story line some what, only you have a chance to kill Chriton instead of crashing into him which would be an instant. game over.

Today though I decided to work on what may become an AI for a spaceship weapon. I learned about graphs to help me create a graph overlay on the screen that I could do path finding algorithms on.

What I have so far is graph points that do represent the range of the screen.
Then I have a function that with a given starting point actually right now it just maps the neighboring cells when you press ‚4‘ and creates objects in each of those cells.

What I was planning on doing was making each those objects detect collisions with other objects and use that as the bases for path finding. However, I’m rethinking this because path finding is supposed to be about nodes not cells.

I will have to decrease the number of enemy objects also and decrease the sixe of the ship and the objects, or increase the size of the screen and the graph, so that I can actually something where path finding would be useful. Maybe for evasion, or for homing. Also the path finding will have to account for the fact that the objects will move from where they were when the path was created.

etc etc.

# Graphs: Basic Path Finding Algorithm( Just finding one path)

I have taken screen shots of me tracing through the algorithm in the python IDE.

# Set Builder Notation(Needed to understand Sets and then Markov Models)

https://en.wikipedia.org/wiki/Element_(mathematics)#Notation_and_terminology

http://www.ma.utexas.edu/users/gordanz/notes/introduction_to_stochastic_processes.pdf

S={}: means S is will be a „Set“

R stands for real numbers.

R^2 stands for „real plane“ or „Cartesian Plane“

S={xR^2}: Specifies a Universe of Discourse, meaning the type of elements we are interested in, in the set. For example, integer numbers, words, etc.

^2 means „pairs of which“ so R^2 means „pairs of real numbers“

S={xR2x[0,1]^2}  Specifies the unierse of discourse and then further specifies the members from that universe that we want in our set which are [0,1] meaning all numbers between 0 and 1.

[0,1]^2 means pairs of 0 and 1

and also I think it’s about coordinates on a Cartesian Plane.

| means add another condition or“such that“.

∈ is a symbol that means „in“

I read some of the tutorial in the third link. I feel sick lol.

# Language Learning Tool – Hanzify

This simple program allows the user to enter the name of a text file on their desktop(although the path in the code is to my desktop). The code then determines the most common 10 words, translates those words into Chinese, and creates a new txt file with every instance of the most common 10 words replaced with it’s Chinese translation, and also prints it to the python shell, or tries to print it to powershell but fails because utf-8.