Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

Libgdx: move img from one point to another?

$
0
0

I’m trying to make an image move towards another point.
I only have the current X/Y and the destination X/Y + Speed.

That’s it.

public void Draw(SpriteBatch batch, int goalX, int goalY){
    destX = goalX - posX;
    destY = goalY - posY;

    dist = Math.sqrt(destX * destX + destY * destY);
    destX = destX / dist;
    destY = destY / dist;

    posX += destX * this.speed;
    posY += destY * this.speed;

    batch.draw(img, posX, posY);    
}

This is my code, it kinda moves towards the object, but it moves past the object to a certain distance and then starts spazzing out for some reason.

The other point is also moving, just so you know.

Anybody can help me with this?


Viewing all articles
Browse latest Browse all 434

Trending Articles