2
\$\begingroup\$

well i am a noob at java and libgdx. i got the homing bullet working with the help of someone. now i am smashing my head as to how i can make it rotate so it faces the ball (which is the main character) when it goes around it or when it is coming towards it. the bullet is facing <--- and the code below is what i have done so far. also i used sprites for the bullet and also animation method.

Also how do i make it an array/arraylist which is best so i can have multiple bullets at random or placed places. i tried many things nothing workd :(

thank you for the help.

// below is the bullet or enemy if you want to call it.

public class Bullet extends Sprite {

  public static final float BULLET_HOMING = 6000; 
  public static final float BULLET_SPEED = 300; 
  private Vector2 velocity;
  private float lifetime;


  public Bullet(float x, float y) {
     velocity = new Vector2(0, 0);
     setPosition(x, y);
  }

  public void update(float delta) {
     float targetX = GameWorld.getBall().getX();
     float targetY = GameWorld.getBall().getY();
     float dx = targetX - getX();
     float dy = targetY - getY();

     float distToTarget = (float) Math.sqrt(dx * dx + dy * dy); 
     dx /= distToTarget;
     dy /= distToTarget;
     dx *= BULLET_HOMING;
     dy *= BULLET_HOMING;
     velocity.x += dx * delta;
     velocity.y += dy * delta;

     float vMag = (float) Math.sqrt(velocity.x * velocity.x + velocity.y * velocity.y);
     velocity.x /= vMag;
     velocity.y /= vMag;
     velocity.x *= BULLET_SPEED;
     velocity.y *= BULLET_SPEED;

     Vector2 v = velocity.cpy().scl(delta);
     setPosition(getX() + v.x, getY() + v.y);
     setOriginCenter(); 
     setRotation(velocity.angle());
     lifetime += delta;
     setRegion(AssetLoader.bulletAnimation.getKeyFrame(lifetime));
  }
}

// this is where i load the images.

public class AssetLoader {
public static Animation bulletAnimation;
public static Sprite bullet1, bullet2;

public static void load() {

    texture = new Texture(Gdx.files.internal("SpriteN1.png"));
    texture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);

    bullet1 = new Sprite(texture, 380, 350, 45, 20);
    bullet1.flip(false, true);

    bullet2 = new Sprite(texture, 425, 350, 45, 20);
    bullet2.flip(false, true);

    Sprite[] bullets = { bullet1, bullet2 };
    bulletAnimation = new Animation(0.06f, aims);
    bulletAnimation.setPlayMode(Animation.PlayMode.LOOP);
}

public static void dispose() {
    // We must dispose of the texture when we are finished.
    texture.dispose();
}

// this is for the rendering of the images etc

public class GameRenderer {
private Bullet bullet;
private Ball ball;

public GameRenderer(GameWorld world) {
    myWorld = world;
    cam = new OrthographicCamera();
    cam.setToOrtho(true, 480, 320);

    batcher = new SpriteBatch();
    // Attach batcher to camera
    batcher.setProjectionMatrix(cam.combined);

    shapeRenderer = new ShapeRenderer();
    shapeRenderer.setProjectionMatrix(cam.combined);

    // Call helper methods to initialize instance variables
    initGameObjects();
    initAssets();
}

private void initGameObjects() {
   ball = GameWorld.getBall();
   bullet = myWorld.getBullet();
    scroller = myWorld.getScroller();
}

private void initAssets() {
    ballAnimation = AssetLoader.ballAnimation;
    bulletAnimation = AssetLoader.bulletAnimation;
}

public void render(float runTime) {

   Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

    batcher.begin();
    // Disable transparency 
    // This is good for performance when drawing images that do not require
    // transparency.
    batcher.disableBlending();

    // The ball needs transparency, so we enable that again.
    batcher.enableBlending();


    batcher.draw(AssetLoader.ballAnimation.getKeyFrame(runTime), ball.getX(), ball.getY(), ball.getWidth(), ball.getHeight());

    batcher.draw(AssetLoader.bulletAnimation.getKeyFrame(runTime), bullet.getX(), bullet.getY());

    // End SpriteBatch
    batcher.end();
    }
}

// this is to load the image etc on the screen i guess

public class GameWorld {

public static Ball ball;
private Bullet bullet;
private ScrollHandler scroller;

public GameWorld() {
   ball = new Ball(480, 273, 32, 32);
   bullet = new Bullet(10, 10);
    scroller = new ScrollHandler(0);
}

public void update(float delta) {
    ball.update(delta);
    bullet.update(delta);
    scroller.update(delta);
}

public static Ball getBall() {
    return ball;
}

public ScrollHandler getScroller() {
    return scroller;
}

public Bullet getBullet() { 
   return bullet;
 }
}

so there is the whole thing. the images are loaded via the AssetLoader then to the GameRenderer and GameWorld via the Bullet class. i am guessing that is how it is. sorry newbie so still learning.

thank you in advace for the help or any advice.

\$\endgroup\$
  • \$\begingroup\$ just want to comment that you shouldn't be creating variables in the update method, think about how many times you are creating something every 1/60th of a second \$\endgroup\$ – gallly Jun 8 '14 at 2:30
1
\$\begingroup\$

For your first question, what you'd need to do is to use a SpriteBatch draw() method that includes setting a rotation. You can use this draw method that takes in a rotation argument. Now, the question is how to calculate the rotation that is required. This requires a little bit of trigonometry, and also depends on how your bullet image looks like. Refer to the figure below. Calculating the value of the rotation given a bullet and target.

Assuming that you bullet image is like that shown above, where it is pointing up. Then, you can calculate the angle using the atan2 function. The parameters being passed in would just be deltaY and deltaX, which is the distance between your bullet and your target for both the y-axis and x-axis respectively.

For your second question, instead of having a single private Bullet bullet in your GameWorld, you would want to have an Array of bullets. You can declare 10 bullets with random positions between [-10,10] for both their x-position and y-position like this.

   private Array<Bullet> bullets;

   public GameWorld() {

      bullets = new Array<Bullet>();

      Bullet bullet = null;
      float bulletX = 0.0f;
      float bulletY = 0.0f;
      for (int i=0; i < 10; i++) {
         bulletX = MathUtils.random(-10, 10);
         bulletY = MathUtils.random(-10, 10);
         bullet = new Bullet(bulletX, bulletY);

         // add the bullet to your array so you can reference them later
         bullets.add(bullet) 
      }

      ...
   }
\$\endgroup\$
  • \$\begingroup\$ hey thanks for the help, but the array just throws exceptions. i tried to make changes in the GameRenderer class and also the GameWorld class but no luck. also i don't know where to put the draw() method as i tried something before and i get errors. my sprites is facing left and i tried so many things as it is a animation i think that might be it. i dont know how to change it :( Any help will do Thanks \$\endgroup\$ – johnny-b Jun 5 '14 at 7:35
  • \$\begingroup\$ What exception is being thrown, and at which point is it being thrown? In the code I listed, it's pretty much just initializing an Array and then adding bullets to it. If your sprite is facing left, then you can just set the angle to appropriately. I'll try to get an updated image for that case, but it uses pretty much the same logic to calculate. You should just replace the draw() method that is in your render() method. It is already there in the code you pasted. \$\endgroup\$ – midasmax Jun 5 '14 at 16:43
  • \$\begingroup\$ as you see above the GameWorld has an update method and GameRenderer has a render method. as a newbie i cannot do what is needed. the bullet does not rotate and the array i tried it your way nothing happens and when i try to edit the bullet then things go wrong. if you can try running my code and see if you can make sense as if i cant make this work i will have to scraps months of graphics and rendering and coding work. Thank you so much for helping. wish i could give you points but stack wont allow me yet. \$\endgroup\$ – johnny-b Jun 6 '14 at 0:13
  • \$\begingroup\$ If you can find some way of hosting the files i can take a look at them? \$\endgroup\$ – midasmax Jun 6 '14 at 4:05
  • \$\begingroup\$ hi midas max i found the problem but the solution is hard check my other question please that might help libgdx draw issue and animation \$\endgroup\$ – johnny-b Jun 7 '14 at 0:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.