Updating projectiles in my program

Hello everyone,

Right now I am working on a program and I am having some difficulty with a function within one of my classes. I should also mention that I am using Allegro 5, but my question does not have anything to do with Allegro 5 and is more about classes.

What I want to do is shoot a projectile from my player's (x, y) coordinates to my reticle's (x, y). When I click the left mouse button I set my projectile's starting coordinates, then I update the coordinates to travel toward the mouse reticle. The problem, however, is that the projectile does not travel toward the cursor and instead travels at a 45 degree angle from the player no matter what my cursor is.

Here is where I initiate the projectile coordinates:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void PlayerProjectile::ProjectileFire(Player player, Reticle reticle)
{
	int playerX = player.GetPlayerX();
	int playerY = player.GetPlayerY();
	int reticleX = reticle.GetReticleX();
	int reticleY = reticle.GetReticleY();

	projectileX = playerX;
	projectileY = playerY;
	startX = playerX;
	startY = playerY;
	maxX = reticleX;
	maxY = reticleY;
	reticleDistance = std::sqrt(std::pow(maxX - startX, 2) + 
		std::pow(maxY - startY, 2));
	projectileFire = true;
}

And here is where I update the projectile:

1
2
3
4
5
6
7
8
9
10
11
12
13
void PlayerProjectile::UpdateProjectile()
{
	if(maxX < startX && maxY < startY)
	{
		if(projectileX > maxX && projectileY > maxY)
		{
			projectileX -= (startX - maxX) / reticleDistance;
			projectileY -= (startY - maxY) / reticleDistance;
		}
		else
			projectileFire = false;
	}
}

Right now it only updates the projectile if my mouse cursor is in the top-left quadrant.

Then in my game loop this is how I am using the update function with an array of projectiles, PlayerProjectile projectiles[MaxProjectiles]:

1
2
3
4
5
for(int i = 0; i < MaxProjectiles; i++)
{
	if(projectiles[i].GetProjectileFire())
		projectiles[i].UpdateProjectile();
}

(GetProjectileFire just returns projectileFire).

From what I can tell, the projectiles get initiated just fine. The problem lies in the update function, but I have been looking at it all day and still cannot figure out what is wrong. I am fairly new to using classes, so maybe I am not using the functions correctly with the array? When I did this stuff using structs it worked perfectly, so I am not sure what I messed up.

Thanks in advance for any help.
Last edited on
Topic archived. No new replies allowed.