This site requires JavaScript, please enable it in your browser!
Greenfoot back
Artyoum
Artyoum wrote ...

2014/3/7

homing missile

Artyoum Artyoum

2014/3/7

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public void getGoon()
   {   
      World w = getWorld();
       
      List<ship> n = w.getObjects(ship.class);
      ListIterator<ship> s = n.listIterator();
      if(!s.hasNext())
      {
          return;
      }
       
      List<Goon> goons = w.getObjects(Goon.class);
      ListIterator<Goon> itr = goons.listIterator();
      if(!itr.hasNext())
      {
          return;
      }
       
      ship shipu = s.next();
       
      int gArrX[] = new int[goons.size()];
      int gArrY[] = new int[goons.size()];
      int rX[] = new int[goons.size()];
      int rY[] = new int[goons.size()];
      int hyp[] = new int[goons.size()];
      int lowest = hyp[0];
      //int X;
      //int Y;
       
      for(int i = 0; i < gArrX.length; i++)
      {
          Goon enemy = itr.next();
           
          int ex = enemy.getX();
          gArrX[i] = ex;
          rX[i] = ex;
          int ey = enemy.getY();
          gArrY[i] = ey;
          rY[i] = ey;
           
          int sx = shipu.getX();
          int sy = shipu.getY();
           
          for(int xy = 0; xy < hyp.length; xy++)
          {
              hyp[xy] = (int)(Math.sqrt((gArrX[i]-sx)^2 + (gArrY[i]-sy)^2));
          }
          
         /* for(int t = 0; t < hyp.length; t ++)
          {
              int X=0;
              int Y=0;
              if(lowest > hyp[t])
              {
                  lowest = hyp[t];
                  X = gArrX[t];
                  Y = gArrY[t];
                  this.turnTowards(X,Y);
              }
              //this.turnTowards(X,Y);
          }*/
       }
        
       for(int t = 0; t < hyp.length; t ++)
          {
              //int X;
              //int Y;
              if(lowest > hyp[t])
              {
                  lowest = hyp[t];
                  X = rX[t];
                  Y = rY[t];
                  turnTowards(X,Y);
              }
              //this.turnTowards(X,Y);
          }
      //turnTowards(X,Y);
      turn(30);
   }
I'm trying to make a homing missile with this code, but it doesn't seem to be working. Can anyone help?
davmac davmac

2014/3/7

#
Well "^ 2" doesn't mean "to the power of 2" in Java. I think that's your main problem. You should consider using Math.hypot(...) to calculate the distance instead.
davmac davmac

2014/3/7

#
Also, you initialise 'lowest' before you have actually calculated any of the distances (line 26). It will always start at 0, so you will never turn towards any Goon, because the distance to each Goon will always be greater than 0.
Artyoum Artyoum

2014/3/7

#
Thanks! I'll change it and see if it works!
Artyoum Artyoum

2014/3/7

#
I just did it, it Is working now! Thank you so much!!!! (*^*)/
You need to login to post a reply.