[gameprogrammer] Re: need help with bullets....
- From: Olof Bjarnason <olof.bjarnason@xxxxxxxxx>
- To: gameprogrammer@xxxxxxxxxxxxx
- Date: Thu, 14 Apr 2005 10:47:38 +0200
Let F be the for ship position, and P be the player ship position.
1. Find the vector from F to P and normalize it. (divide each
coordinate it by the vector's length), call it A
2. Decide a speed for the bullet, BULLET_SPEED.
3. Each time you update the shot, add A*BULLET_SPEED to the current
position of the shot
To normalize a vector A=3D(x,y), you do
length =3D sqrt(x*x + y*y);
x /=3D length;
y /=3D length;
This kind of math is called Vector Algebra and you're gonna read a lot
of it at college / uni.. Or beforehand which is more fun :)
/Olof
On 4/14/05, Anom <adeanom@xxxxxxxxx> wrote:
> hi there all of you,
> right now i'm try to develop my side scrolling spaceship game in java. an=
d i
> got a little bit of trouble with the bullet shot by an enemy to the playe=
r.
>=20
> the problem is, the speed of the bullets shot by an enemy are very fast i=
f
> the player position is far, but if the position is close to the enemy the=
n
> the bullet speed is ver very low...
>=20
> here's what my code looks like....
>=20
> /** shoot the player... */
> vectorX =3D player.getX() - ufos[i].getX();
> vectorY =3D player.getY() - ufos[i].getY();
>=20
> if((ufos[i].getGunCoolTime() =3D=3D 0)) {
> for(int j =3D 0; j < shots.length; j++) {
> if((shots[j] =3D=3D null)) {
> shots[j] =3D ufos[i].shoot(vectorX, vectorY);
>=20
> shootSound.play();
> break;
> }
> }
> } else {
> ufos[i].subsGunCoolTime();
> }
>=20
> and on the method for shoot is just merely create a new Shot instance.
> Here's my method of moving the bullet shot by an enemy :
>=20
> xPos +=3D vectorX / BULLET_SPEED;
> yPos +=3D vectorY / BULLET_SPEED;
>=20
> maybe you'll ask me why am i dividing the vector with BULLET_SPEED, becau=
se
> if i'm not do that, than i merely can't see the bullet shots by the enemy
> because of the high speed.
>=20
> i hope i have submitted the right amount of information for anyone here t=
o
> help me.
>=20
> TiA
>=20
> --
> Ade Anom A
> [http://www.a3rex.info]
>=20
> ---------------------
> To unsubscribe go to http://gameprogrammer.com/mailinglist.html
>=20
>
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- Follow-Ups:
- [gameprogrammer] Re: need help with bullets....
- From: Olof Bjarnason
- [gameprogrammer] Re: need help with bullets....
- From: Anom
- References:
Other related posts:
- » [gameprogrammer] need help with bullets....
- » [gameprogrammer] Re: need help with bullets....
- » [gameprogrammer] Re: need help with bullets....
- » [gameprogrammer] Re: need help with bullets....
- » [gameprogrammer] Re: need help with bullets....
- » [gameprogrammer] Re: need help with bullets....
- [gameprogrammer] Re: need help with bullets....
- From: Olof Bjarnason
- [gameprogrammer] Re: need help with bullets....
- From: Anom