# [.net] calculating projectile path in 2D game in C#

This topic is 4526 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, im working on a 2D vertical scroller shooting game in C#. the game is near its completion but i'm stuck on one problem. in my code now the enemy's bullet could only be fired in 4 directions - up, down, left, or right. however i'd like the enemies to fire at the player sprite. i know how to get the coordinates of both enemy and player sprites, could someone pls enlighten me on how to calculate the projectile path? For example, the enemy sprite is at 200,300(X,Y coordinate) and the player sprite is at 500,500, and the enemy fires a bullet at the player. now, how do i get the path of the enemy's bullet that will travel toward 500,500. currently in my code the bullet moves according to its Matrix system like this: bullet.bulletMatrix = Matrix.Multiply(bullet.bulletMatrix, Matrix.Translation(0, Bullet.bulletSpeed, 0)); as u can see the bullet moves only on Y coordinate, since X coordinate is set to 0. is there a way to calculate 2 numbers that i could put into X,Y coordinate to make the bullet travel toward the player's position? that way i can do: bullet.bulletMatrix = Matrix.Multiply(bullet.bulletMatrix, Matrix.Translation(X, Y, 0)); Your input is greatly appreciated.

##### Share on other sites
You need to find the direction to the player, and then multiply that vector by the speed of the bullet

[code lang="cpp"]
//This will calculate the initial direction of travel for the bullet, and set it to move along the direction at a speed defined by "bulletSpeed"

float bulletX=0.0f;
float bulletY=0.0f;
float distance=0.0f;

//step 1: find the vector of the player from the enemy:

bulletX = player.position.x - enemy.position.x;
bulletY = player.position.y - enemy.position.y;

//step 2: normalize the vector to give a direction of length 1 (so that we can make it move at the correct speed)

distance = sqrt(bulletdirection.x*bulletdirection.x + bulletdirection.z*bulletdirection.z);

bulletX /= distance;
bulletY /= distance;

//step 3: multiply by the speed you want the bullet to move at:
bulletX *= bulletSpeed;
bulletY *= bulletSpeed;

//step 4: Now do your snizzle
bullet.bulletMatrix = Matrix.Multiply(bullet.bulletMatrix, Matrix.Translation(bulletX , bulletY, 0));

##### Share on other sites
If you do "playercoords - enemycoords" then the result will be a vector that points at the player position with respect to the enemys position. If you then normalize this vector (make it have a length of 1) then you will have a direction to the player. Multiply this by the bullets speed and you have the direction that you need to move in at the right speed in order to fire at the player.

I hope this helps.

EDIT: Argh, beaten to it.

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 101
• 11
• ### Forum Statistics

• Total Topics
632974
• Total Posts
3009664
• ### Who's Online (See full list)

There are no registered users currently online

×