# Android QuestionHow to calc Enemy Bullets direction ?

#### Mirco Crepaldi

##### Member
Longtime User
Hi , i'm writing an orizzontal shot em up , and i need to write the enemy bullets routine.
My problem is how to calculate the direction of the bullet and make it go to this direction since is out of the screen.

I know the start position of the bullet (enemy position) and the destination (ship position) but i've not idea
how to transform into code for have the correct direction , and then transform in values to add or sub to the bullet position.

Can anyone help me pls ?

Thanks

#### Mirco Crepaldi

##### Member
Longtime User
HI ,
sorry for take long time and thanks for your reply , for now i cant found a way to do what i need but i'll try to check your example.
I have another question : Is there a way to pass variable to new activity ?
i insert my variables here :

Sub Process_Globals
Dim Score As Int
Dim ShipHP As Int = 100
Dim GameOver As Int = 0

End Sub

but when i start a new activity i cant read them.
how can i do ?

Thanks.

#### derez

##### Expert
Longtime User
You should refer to the variable with the name of the activity where it is set.
Like main.shiphp

Longtime User
Thank you !!!

#### James Chamblin

##### Active Member
Longtime User
There are several ways of moving a bullet across the screen. One way would be to use some simple trigonometry functions.
B4X:
'You need a structure to hold the information on the bullet.  Notice that all the members are Float to avoid problems with rounding
'   You could also use Double for more accuracy
'   X and Y are the current coordinates of the bullet
'   xSpeed is the number of pixels to move horizontally each frame
'   ySpeed is the number of pixels to move vertically each frame
Type tBullet(x As Float, y As Float, xSpeed As Float, ySpeed As Float)
...
'Execute this  when the enemy fires on the ship
'   Speed is the distance the bullet will travel per frame
Dim Bullet As tBullet
Dim Speed as Float = 2.0 '<- Mess with this value to change the bullet's speed
Dim Angle As Float = ATan2(Ship.y-Enemy.y,Ship.x-Enemy.x) 'Notice that Y is before X.  This will give you the angle from the enemy to the ship
Bullet.xSpeed = Cos(Angle)*Speed
Bullet.ySpeed = Sin(Angle)*Speed
...
'Update the position of the bullet each frame with this code
Bullet.x = Bullet.x + Bullet.xSpeed
Bullet.y = Bullet.y + Bullet.ySpeed
Of course, these are just code fragments. You will need to modify them and place them in your code where they make sense.

#### Mirco Crepaldi

##### Member
Longtime User
Really Thanks a lot, now everything is more clear and works great !!

Replies
3
Views
1K
Replies
11
Views
7K
Replies
9
Views
4K
Replies
0
Views
3K
Android Question game in TD style
Replies
1
Views
786