# libgdx rotation (animation, arrays) issues and help needed

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;

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());
}
}


// this is where i load the images.

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

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() {
}

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();

// 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.

• 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 – gallly Jun 8 '14 at 2:30

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.

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