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

2013/2/5

how can I miss with this code?

erdelf erdelf

2013/2/5

#
I have an ActorA walking with guns and a Target called ActorB. I am missing with the missile this is in ActorA in act
        

        List<ActorB> turs= getObjectsInRange(range,ActorB.class);
        if(turs.size()==0)
            return;
        Actor B tur = (ActorB)turs.get(0);
        turnTowards(tur.getX(),tur.getY());
        Actor missiles = new Missile();
        missiles = new MissileA(getRotation());}
        getWorld().addObject(missiles,getX(),getY());
this in the missile
           
           public MissileA(int rot)
          {
             setRotation(rot);
         }
        public void act()
        {
           ActorB tur= (ActorB) getOneIntersectingObject(ActorB.class);
            if(tur!=null)
            {
                tur.damage(power);
                getWorld().removeObject(this);
                return;
            }
        move(speed);
      }
MatheMagician MatheMagician

2013/2/5

#
How fast is your speed? If you just go in one direction at the start, your actor's speed will limit the directions that it can go, increasing the likelihood you are going to miss. Danpost had a suggestion on this, which I have slightly improved;) Instead of setting the rotation of the bullet in the constructor, tell it the exact location of where you want to go. Then, use that information to extrapolate another point where that trajectory would take the bullet passed the crosshairs. In your bullet act code then, you can just make it reset its rotation so that it always is going in the right direction. Here is what your new code in Actor A would look like
List<ActorB> turs= getObjectsInRange(range,ActorB.class);  
if(turs.size()==0)  
    return;  
Actor B tur = (ActorB)turs.get(0);  
turnTowards(tur.getX(),tur.getY());  
Actor missiles = new Missile();  
missiles = new MissileA(tur.getX()-getX(),tur.getY()-getY(),getX(),getY());}  
getWorld().addObject(missiles,getX(),getY());  
And your new missile code would look like this
int x;
int y;
public MissileA(int x1,int y1,int x2,int y2)  
    {  
       this.x = x1*2+x2;
       this.y = y1*2+y2;
   }  
  public void act()  
  {  
     ActorB tur= (ActorB) getOneIntersectingObject(ActorB.class);  
      if(tur!=null)  
      {  
          tur.damage(power);  
          getWorld().removeObject(this);  
          return;  
      }  
  turnTowards(x,y);
  move(speed);  
}  
erdelf erdelf

2013/2/12

#
sry, for the late answer. It doesn't work. The missile is moving with the right rotation but only in one direction with only changing one coordinate, which one is depending on the position of the target. As soon as the missile is near, it is instantly changing its move direction, targeting the target,
MatheMagician MatheMagician

2013/2/12

#
Sorry, I did not test my code. Anyway, what was happening was that merely doubling the distance wasn't enough. This new code automatically updates the point it is turning towards whenever it gets stuck like that.
int x;  
        int y;  
        int x1 = 0;
        int y1 = 0;
        int speed = 3;
        public MissileA(int x1,int y1,int x2,int y2)    
        {    
           this.x = x1;  
           this.y = y1;  
            this.x1 = x2;
            this.y1 = y2;
            x += x2;
            y += y2;
       }
       public void act()
       {
            ActorB tur= (ActorB) getOneIntersectingObject(ActorB.class);    
      if(tur!=null)    
      {    
          tur.damage(power);    
          getWorld().removeObject(this);    
          return;    
      }
            turnTowards(x,y);  
            move(speed);
            if(Math.abs(getX()-x1)>=Math.abs(x-x1)||Math.abs(getY()-y1)>=Math.abs(y-y1))
            {
                y = y1+(y-y1)*2;
                x = x1+(x-x1)*2;
            }
       }
erdelf erdelf

2013/2/13

#
this code is working correctly in more situations, but not in every situation. In the not working situations the missile is moving like it was before
MatheMagician MatheMagician

2013/2/14

#
I am not having the same issue. How fast are you moving the missiles? My only other idea would be to change
if(Math.abs(getX()-x1)>=Math.abs(x-x1)||Math.abs(getY()-y1)>=Math.abs(y-y1)) 
to
if(Math.abs(getX()-x1)>=Math.abs(x-x1)-speed ||Math.abs(getY()-y1)>=Math.abs(y-y1)-speed) 
This will mean the missile will point to somewhere else a little bit further back.
erdelf erdelf

2013/2/14

#
a bit better, but the problem is still there.
MatheMagician MatheMagician

2013/2/16

#
I can't tell clearly from your picture what the problem is. It appears it cut off at the ends.
erdelf erdelf

2013/2/17

#
at the beginning, the Missiles Rotation is right, but the movement isnt. the movement is straightly changing in only one axis.
You need to login to post a reply.