Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: naylorbrandon on Tue 08/06/2010 09:22:32

Title: projectiles
Post by: naylorbrandon on Tue 08/06/2010 09:22:32
How would I go about writing a script for firing a gun? Would I do a drawimage of a bullet and move it? Or is there a better way
Title: Re: projectiles
Post by: Jim Reed on Tue 08/06/2010 10:15:04
It really depends on how are you displaying it ie. what is happening on the screen. You should tell us more so we can understand better, and post a screenshoot (a sketch would do) of the problem if you can.
Title: Re: projectiles
Post by: naylorbrandon on Tue 08/06/2010 20:58:37
when I click, i want the image of the bullet to travel from the character and move in the direction of the cursor, but to continue past the cursor such that if points A, B, and C make a line, A being the character, B being the mouse, the projectile would travel past B and go on through C.

One way I've thought about doing this is using the draw line tool instead of an image, but when I try to erase the line it removes my background.
Title: Re: projectiles
Post by: Wyz on Tue 08/06/2010 21:10:27
I'd use a unit vector calculated from the two points. Then you can use that to calculate a velocity vector which you add to the origin of the bullet.

example:
Code (pseudo code) Select

U.x = B.x - A.x
U.y = B.y - A.y

norm = Maths.sqrt(U.x * U.x + U.y * U.y)
U.x = U.x / norm
U.y = U.y / norm

speed = something
V.x = U.x * speed
V.y = U.y * speed

I'd take floats for U and integers for V. The bullet will go a bit off course due to rounding errors, but it shouldn't be too bad.


function repeatedly_execute()
{
Bullet.x += V.x;
Bullet.y += V.y;
}
Title: Re: projectiles
Post by: naylorbrandon on Tue 08/06/2010 23:11:25
How should I summon the bullet image? If i use draw, can I move the drawing, and erase just the bullet after it's run its course? Or should I summon it as a character?
Title: Re: projectiles
Post by: Wyz on Wed 09/06/2010 00:04:20
There are different approaches and I can't really tell which is the best. It also depends on what you're making.

If you have a single bullet at the time you can use an object and move it around.
If you need a lots of bullets all over the place you might be better off drawing them on a overlay or transparent GUI. In the latter case, there are particle systems around here somewhere that do exactly that.
Title: Re: projectiles
Post by: naylorbrandon on Wed 09/06/2010 01:24:21
I got it working by making the bullets a character. I'm going to see what I can do about duplicate characters, or possibly make multiple copies of the bullet char.
thanks for the help!
Title: Re: projectiles
Post by: Khris on Wed 09/06/2010 09:59:43
Note that for maximum accuracy, multiply the vector by speed, then divide by length (norm).
Also store the bullet coordinates as floats and convert only to draw.

There are several existing threads about this exact same problem, btw.