Projectile in Front of Muzzle

I thought some people might find this explanation useful. For my Particle Accelerator, I used Lua script to make the an orb stay in front of the muzzle of the gun while the weapon is charging. Here is the code that I used to do it:

**Code:**

--inside function Update(self)

local theta = ToActor(self.owner):GetAimAngle(true);

self.Pos = self.gun.Pos + Vector(math.cos(theta)*40,math.sin(theta)*-40);

This code involves some trigonometry that I will explain shortly.

For the accelerator, I wanted the orb to hover 40 pixels in front of the my player's gun. The easiest way to do this would be to add some vector to the gun's position. However, the gun rotates about a joint, simply just adding 40 to the gun's position won't work. The position in front of the gun changes based on the angle you player is aiming at.

This is given by the function GetAimAngle(); In the code above, the parameter 'true' means that the angle accounts for your character facing either direction. Now we need to use this angle to calculate the x and y coordinates individually.

*NOTICE*

The picture assumes that +y is up and -y is down. In CC, it is the opposite. So you must multiply the y coordinate of the vector by -1.In the picture above:

r = radius (ie. distance between the gun's position and the orb's position in pixels. In my example's case, 40 pixels).

Q = aim angle (GetAimAngle(true);)

P = the position 'r' pixels in front of the gun's center.

In trigonometry, Cos(Q) will always give you the X coordinate of a vector with a magnitude of 1. Likewise, "r * Cos(Q)" will give the same for a vector with a magnitude of 'r'. Which is what we want. Sin(Q) works just the same, but for the Y coordinate.

X = r*cos(Q) = 40cos(Q)

Y = r*sin(Q) = 40sin(Q)

Put these two values into a Vector() and add it to the gun's position. And your done =P.

P = self.gun.Pos + Vector(r * cos(Q), -1 * r * sin(Q) );