projectiles

Started by naylorbrandon, Tue 08/06/2010 09:22:32

Previous topic - Next topic

naylorbrandon

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

Jim Reed

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.

naylorbrandon

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.

Wyz

#3
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

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.

Code: ags

function repeatedly_execute()
{
	Bullet.x += V.x;
	Bullet.y += V.y;
}
Life is like an adventure without the pixel hunts.

naylorbrandon

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?

Wyz

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.
Life is like an adventure without the pixel hunts.

naylorbrandon

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!

Khris

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.

SMF spam blocked by CleanTalk