libgdx guided missile

Hi everyone,
newbie here so please help out if I am doing something wrong here.
I am not using Box2d.

Problem:
I made a background and a ball in middle of it. Now i want a missile to come and hit it like in the link below as I cannot get it to work.

also I want to make a array so i can have more than 1 coming at it.

I am using libGDX, I created the ball and background using a GameWorld class and a render class to render the image.
Now I did enemy ai and it works the missile homes in on the ball but the image does not rotate.

I tried all the methods but my code crashes because the missile class is being called in GameWorld class as an update and it crashes if i use and of the math methods be is atan2, cos, sin etc.

sample of my code:

// This is the Bullet class for the Homing.
public class Bullet {

private Bullet bullet;

private Vector2 position;
private Vector2 acceleration;
private Vector2 velocity;

private int width;
private int height;
private int moveSpeed = 3;

public Bullet(float x, float y, int width, int height) {
this.width = width;
this.height = height;
position = new Vector2(x, y);
acceleration = new Vector2(0, 460);
velocity = new Vector2(0, 0);
}

public void update(float delta) {

   velocity.add(position.cpy().getAngleRad(), delta);
  position.add(velocity.cpy().getAngleRad(), delta);
  
  if (GameWorld.getBall().getX() < position.x) {
     position.x -= moveSpeed;
  }
  if (GameWorld.getBall().getX() > position.x) {
     position.x += moveSpeed;
  }
  if (GameWorld.getBall().getY() < position.y) {
     position.y -= moveSpeed;
  }
  if (GameWorld.getBall().getY() > position.y) {
     position.y += moveSpeed;
  }

}

public float getX() {
return position.x;
}

public float getY() {
return position.y;
}

public float getWidth() {
return width;
}

public float getHeight() {
return height;
}
}

// this is the GameWorld class
public class GameWorld {

public static Ball ball;
private Bullet bullet;

public GameWorld() {
   Ball = new Ball(480, 273, 32, 32);
   Bullet = new Bullet(480, 130, 94, 63);
}

public void update(float delta) {
    Ball.update(delta);
    Bullet.update(delta);
}

public static Ball getBall() {
    return soldier;
}

public ScrollHandler getScroller() {
    return scroller;
}

// The game render has the batcher spriteBatcher in the constructor the cam and also Game objects are there also the assets from assetLoader where all the game animation and images are which is loaded via this.

// The gameScreen implements and in here are gameworld object and gamerenderer

I hope this is enough for the help needed.

any help will be appreciated.

Thank you in advance.

Johnny

Can I get a copy of the error that comes up?

I would advise you not to double post. :cranky:

Newbie and Debugging Questions.

sorry again it won’t let me delete it for some reason.
I’ll keep trying again sorry.
newbie mistake.
johnny

Another tip, surround your code with [code.] [/code.] but without the periods. You get this


public class Test {
   public int a = 10;

   public static void main(String[] args) {
      System.out.println("Test");
   }
}

thank you ninja
i’ll try it

This is what happens if I do anything other than just update the position of the bullet that is it.
it’s just like a cartoon.

Exception in thread “LWJGL Application” java.lang.NullPointerException
at com.aozora.GameObjects.Bullet.update(Bullet.java:32)
at com.aozora.GameWorld.GameWorld.update(GameWorld.java:28)
at com.aozora.Screen.GameScreen.render(GameScreen.java:32)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

Removed the extra posts and moved to newbie & debugging. Don’t double post next time.

Ummm… Remember that in Java that your class names are “case sensitive”.


// This is the Bullet class for the Homing.
public class Bullet {
   
   private Bullet bullet;
   
   private Vector2 position;
   private Vector2 acceleration;
   private Vector2 velocity;
   
   private int width;
   private int height;
   private int moveSpeed = 3;

   public Bullet(float x, float y, int width, int height) {
      this.width = width;
      this.height = height;
      position = new Vector2(x, y);
      acceleration = new Vector2(0, 460);
      velocity = new Vector2(0, 0);
   }
   
   public void update(float delta) {
      
       velocity.add(position.cpy().getAngleRad(), delta);
      position.add(velocity.cpy().getAngleRad(), delta);
      
      if (GameWorld.getBall().getX() < position.x) {
         position.x -= moveSpeed;
      }
      if (GameWorld.getBall().getX() > position.x) {
         position.x += moveSpeed;
      }
      if (GameWorld.getBall().getY() < position.y) {
         position.y -= moveSpeed;
      }
      if (GameWorld.getBall().getY() > position.y) {
         position.y += moveSpeed;
      }
   }

   public float getX() {
      return position.x;
   }

   public float getY() {
      return position.y;
   }

   public float getWidth() {
      return width;
   }

   public float getHeight() {
      return height;
   }
}

// this is the GameWorld class
public class GameWorld {
   
   public static Ball ball;
   private Bullet bullet;

    public GameWorld() {
       Ball = new Ball(480, 273, 32, 32);
       Bullet = new Bullet(480, 130, 94, 63);
    }

    public void update(float delta) {
        //This may be a problem so I'd probably fix here...
-        Ball.update(delta);
+        ball.update(delta);

        //This may also be a problem
-        Bullet.update(delta);
+       bullet.update(delta);
    }

    public static Ball getBall() {
        return soldier;
    }
    
    public ScrollHandler getScroller() {
        return scroller;
    }
}


The rest of it looks okay, it in only in the update code that I saw you used uppercase instead of lower case. :slight_smile:

hi ctomni231
the code i moded a little before posting so there is no mistake there.

The bullet goes to the target like any other enemy ai it just goes to the target.

the bullet does not turn and it does not look like a guided missile and that is what i am looking for.

the bullet should come out from the position set and seek towards the target like a missile.

thank you for the help

johnny