Hi,
I want to develop some code that enables a robot to shoot at a moving target (i.e: the player character). but rather than spawning a laser object and simply MoveObject, I want the object to continue moving past the target if it misses (i.e: move along an angled trajectory).
My question is, does anyone know of any existing code or pseudo code available that already does this which I can reuse. I am currently researching this myself but thought I'd ask here as well.
Thanks
delta
I remember a thread about this, too lazy to search though.
float x = IntToFloat(target.x - origin.x), y = IntToFloat(target.x - origin.x);
float speed = 5.0; // speed in pixels/frame
float len = Maths.Sqrt(x*x + y*y); // to normalize (x;y) i.e. get a movement vector of length speed
x = x*speed/len;
y = y*speed/len;
Now store the position of the laser object in two floats, add x and y to them every frame, then position it at FloatToInt of that.
Khris, you have a typos there in your code:
y = IntToFloat(target.x - origin.x);
should be:
y = IntToFloat(target.y - origin.y);