Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: deltamatrix on Thu 24/12/2009 14:17:54

Title: examples of code for shooting objects on an angled trajectory?
Post by: deltamatrix on Thu 24/12/2009 14:17:54
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
Title: Re: examples of code for shooting objects on an angled trajectory?
Post by: Khris on Thu 24/12/2009 15:14:12
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.
Title: Re: examples of code for shooting objects on an angled trajectory?
Post by: Crimson Wizard on Fri 25/12/2009 13:53:01
Khris, you have a typos there in your code:

y = IntToFloat(target.x - origin.x);

should be:

y = IntToFloat(target.y - origin.y);