I need to do what I thought would be a fairly simple and common task - move one game object (some sort of throwing/shooting weapon) to another object (the target of the shot). The target will be moving slowly and I want the attack to always hit.
I googled extensively for this, but only found a few things that might help and none of them were Java or libGDX specific. The resources I did find were this one and this one. The last one references libGDX, but the answer doesn’t seem to me to be libGDX specific.
As a result of trying to translate those solutions into libGDX/Java I now have the following in the class representing the object to be moved:
Vector2 direction;
public void init(TigerActor target) {
this.target = target;
direction = new Vector2();
direction.x = target.getX() - position.x;
direction.y = target.getY() - position.y;
direction = direction.nor();
}
@Override
public void act() {
position.x += direction.x;
position.y += direction.y;
}
The class TigerActor is a purely custom class inspired by a Scene2D actor but not a Scene2D Actor because I’m not using a Stage here.
The code above doesn’t work - it simply causes the moving object to fly along very quickly on a horizontal line.
Any suggestions on how to improve on this code or how to do it properly from “scratch” would be very much appreciated.
Thanks in advance.