Bombing Run Help


#1
# Incoming oscars! (That's military speak for ogres).
# You will need to calculate their angle of attack.
# Use that angle to command your Griffin Bombers!

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Find the vector of attack
        
        # Use trigonometry to find the the angle in Radians!
        
        # The answer must be in Degrees!
        # To convert Radians to Degrees multiply by (180 / Math.PI)
        
        # Say the angle!

this is the code given


#2
# Incoming oscars! (That's military speak for ogres).
# You will need to calculate their angle of attack.
# Use that angle to command your Griffin Bombers!

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Find the vector of attack
        vector.attack()
        # Use trigonometry to find the the angle in Radians!
        angle = 
        # The answer must be in Degrees!
        # To convert Radians to Degrees multiply by (180 / Math.PI)
        angle2 = 100 * angle
        # Say the angle!
        hero.say(angle2)

is this right and if not( I know its not) can u help?


#3

especially with triginometery


#4

does this work better?

# Incoming oscars! (That's military speak for ogres).
# You will need to calculate their angle of attack.
# Use that angle to command your Griffin Bombers!

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Find the vector of attack
        x = vector.x
        y - vector.y
        # Use trigonometry to find the the angle in Radians!
        angle = math.atan2(y, x)
        # The answer must be in Degrees!
        # To convert Radians to Degrees multiply by (180 / Math.PI)
        angle2 = 100 * angle
        # Say the angle!
        hero.say(angle2)

#5

it still doesn’t work :sob:


#6
# Incoming oscars! (That's military speak for ogres).
# You will need to calculate their angle of attack.
# Use that angle to command your Griffin Bombers!

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Find the vector of attack
        x = Vector.x
        y = Vector.y
        # Use trigonometry to find the the angle in Radians!
        angle = Math.atan2(y, x)
        # The answer must be in Degrees!
        # To convert Radians to Degrees multiply by (180 / Math.PI)
        angle2 = 100 * angle
        # Say the angle!
        hero.say(angle2)

THE ONLY PROB HERE IS THEY RUN HORIZANTLY (MY GUYS)


#7

I don’t now how to doo this level too.
Here is my code :

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Find the vector of attack
        vector=Vector.add(enemy.pos,hero.pos)
        # Use trigonometry to find the the angle in Radians!
        angle=Math.atan2(vector,enemy.pos)
        # The answer must be in Degrees!
        # To convert Radians to Degrees multiply by (180 / Math.PI)
        angle=angle*(180/Math.PI)
        # Say the angle!
        hero.say(angle)

please help


#8

So the first question I have to ask is if both of you understand the Math and Vector libraries we we are using in the game?


#9

not so at all. With the hints I think that we must use Math.atan2


#10

yes agreed x95 and I am not entirely sure either about the vector or maths


#11

what part is wrong in my code?
(the vector I presume)


#12

@X95 @isnode

Code Library definition

a library is a collection of precompiled routines that a program can use. Libraries are particularly useful for storing frequently used routines because you do not need to explicitly link them to every program that uses them.

So in the case of Math we have a structure like an object that has properties (attributes) and functions (methods).

We could have created this ourselves if it didn’t exist.

For instance

class MathLibrary:
    PI = 3.14195
    
    def __init__():
        return True
    
    def factorial(self, number):
        factorialNumber = 1
        while number > 0:
            factorialNumber = factorialNumber * number
            number -= 1
        return factorialNumber
        
    def power(self, number, power):
        powerNumber = 1
        while power > 0:
            powerNumber = powerNumber * number
            power -= 1
        return powerNumber

Math2 = MathLibrary()

hero.say(Math2.PI)
hero.say(Math2.factorial(4) )
hero.say(Math2.power(-1,3) )
        

Sarven Treasure Question About My Code
#13

So basically think of the Math and Vector libraries as toolkits of already made code that you don’t have to create yourself. (imagine if you had to do Taylor approximations to create the sin() function each time)

So the idea is to then understand what each function in the library does. If you don’t really understand them then we should write sample code to determine what is happening. This is where good documentation on each library function will be very helpful.

KHAN Academy gives a good refresher on Trigonometry and Arctan. If we don’t understand the math how can we know what to expect from running a Math function? So I encourage you to tough it out and review anything you are not 100% on.


#14

@isnode @X95
Now lets look at the Vector library

Taking a look at the code that ya’ll wrote lets look at what the different code does:

  1. What does the following code tell us?
x = Vector.x
hero.say(x + " ")

.2. What is the output from this ?

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        vector=Vector.add(enemy.pos,hero.pos)
        hero.say( "Enemy x=" + enemy.pos.x + " Enemy y=" + enemy.pos.y )
        hero.say( "Hero x=" + hero.pos.x + " Hero y=" + hero.pos.y )
        hero.say( "vector x=" + vector.x + " Hero y=" + vector.y )

On test simulation I saw a similar output:

Enemy x=10, y=86
Hero x=52, y=15
Vector x=62, y=101

What can we conclude from this output that Vector.add( <vector1>, <vector2> ) is doing?

And if we draw a line from our hero to the resulting point that was created (vector) from the above code where x=62, y=101, is that there we want our Griffin riders to attack?

That point location is somewhere off the screen to the right of the Hero isn’t it?


#15

Getting to a better answer.

Lets start again. If we look at the hints we need to define a point from the Hero to the Enemy or a vector that starts at the hero and ends at the enemy. In the above diagram that would be up and to the left.

If we know that the x coordinates increase as you move from the left to the right, this would mean that we need a -x right? Because the enemy is to the left of our hero.

And we can observe that the enemy is above us and we know that the y values increase as you go up from the bottom. So this would mean a +y right?

So that means the vector that we want in the end is (-x,+y) from our hero.

[Challenge]
Can you use the numbers returned from the above example in my last post and tell me what that new vector coordinates should be?


#16

So just like Sal said from KHAN Academy. Lets limit our talks to 2 quadrants. 1 and 2, and calculate the angle Θ.

From this diagram we know that we want an angle > 90°. Working backward we can use a estimated guess for instance if we take 125° as 90° + 45° = 125° for a starting angle and then find the tangent of 125°.(convert to radians first)

Also notice that we are changing the zero point on the coordinates to the location of our Hero because that is what the Griffin Riders expect. They know our location and if we can give them an angle from our position that the Ogres are coming from, they will be able to setup their bombing run.

Can either of you tell me what the maximum and minimum angle in degrees ° that the griffin riders should accept? In other words the range of valid answers.


#17

yes the maximum should be 125 and minimum should be 45


#18

so what should i put in my code?
this:

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        vector=Vector.add(enemy.pos,hero.pos)
        hero.say( "Enemy x=" + enemy.pos.x + " Enemy y=" + enemy.pos.y )
        hero.say( "Hero x=" + hero.pos.x + " Hero y=" + hero.pos.y )
        hero.say( "vector x=" + vector.x + " Hero y=" + vector.y )

#19

This is just the debug code to allow you to see what is happening.

Using this code, can you look at the vector library and determine which function might get you the vector from the hero pointing to the ogre? In other words the black arrow in the picture above.


#20

I haven’t fund the code. Can you help me?