[gameprogrammer] Re: need help with bullets....

Ok here is your code in a format I can read:
/** shoot the player... */
vectorX = player.getX() - ufos[i].getX();
vectorY = player.getY() - ufos[i].getY();

if((ufos[i].getGunCoolTime() == 0))
{
        for(int j = 0; j < shots.length; j++)
        {
                if((shots[j] == null))
                {
                        shots[j] = ufos[i].shoot(vectorX, vectorY);

                        shootSound.play();
                        break;
                }
        }
}
else
{
        ufos[i].subsGunCoolTime();
}

xPos += vectorX / BULLET_SPEED;
yPos += vectorY / BULLET_SPEED;

vectorX is the distance between the ufo and the ship on the X axis and
vectorY is the distance between the ufo and the ship on the Y axis.

So what you are doing to move the bullet is incrementing its position by
the distance divided by a time constant. (ie, the bullet will always take
the same amount of time to reach the target)

What you want is the position to be incremented by a set speed. It would
be really simple if you were dealing with straight horizontal or 
vertical lines,
but since you are dealing with diagonal lines you need to increment the 
x and
y positions differently for every angle.

Here is the code you want: (I've put in float to indicate the variables 
that have to be floating point)

float length = sqrt(vectorX * vectorX + vectorY * vectorY);
float xInc = (float) vectorX / length * BULLET_SPEED;
float yInc = (float) vectorY/ length * BULLET_SPEED;
float xPos += xInc;
float yPos += yInc;

This is basically what Olof told you.

Hope this helps.
Dominic McDonnell

Anom wrote:

>hi there all of you,
>right now i'm try to develop my side scrolling spaceship game in java. and i 
>got a little bit of trouble with the bullet shot by an enemy to the player.
>
>the problem is, the speed of the bullets shot by an enemy are very fast if 
>the player position is far, but if the position is close to the enemy then 
>the bullet speed is ver very low...
>
>here's what my code looks like....
>
>/** shoot the player... */ 
>vectorX = player.getX() - ufos[i].getX();
>vectorY = player.getY() - ufos[i].getY();
>
>if((ufos[i].getGunCoolTime() == 0)) {
>for(int j = 0; j < shots.length; j++) {
>if((shots[j] == null)) {
>shots[j] = ufos[i].shoot(vectorX, vectorY);
>
>shootSound.play(); 
>break;
>}
>}
>} else {
>ufos[i].subsGunCoolTime();
>}
>
>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 :
>
>xPos += vectorX / BULLET_SPEED;
>yPos += vectorY / BULLET_SPEED;
>
>maybe you'll ask me why am i dividing the vector with BULLET_SPEED, because 
>if i'm not do that, than i merely can't see the bullet shots by the enemy 
>because of the high speed.
>
>i hope i have submitted the right amount of information for anyone here to 
>help me.
>
>TiA
>
>  
>



---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: