View unanswered posts | View active topics It is currently Sat Apr 20, 2024 12:16 am



Reply to topic  [ 8 posts ] 
 Projectile in Front of Muzzle 
Author Message
User avatar

Joined: Sun May 31, 2009 1:04 am
Posts: 308
Reply with quote
Post 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.

Image
*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) );


Last edited by Kyred on Sun Jun 21, 2009 9:01 pm, edited 1 time in total.



Sun Jun 21, 2009 1:51 am
Profile
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post Re: Projectile in Front of Muzzle
Yup, trig is useful stuff.

I use a similar (though somewhat more complicated) method with the plane.


Sun Jun 21, 2009 2:09 am
Profile
Banned
User avatar

Joined: Tue Feb 27, 2007 4:05 pm
Posts: 2527
Reply with quote
Post Re: Projectile in Front of Muzzle
I guarantee you half of this forum saw math and was like "fgsfds"


Sun Jun 21, 2009 2:12 am
Profile YIM
User avatar

Joined: Tue Nov 06, 2007 6:58 am
Posts: 2054
Reply with quote
Post Re: Projectile in Front of Muzzle
Nice.

Willing to make more trig tutorials so I can stop copy pasting trig code?


Sun Jun 21, 2009 2:20 am
Profile
User avatar

Joined: Sun May 31, 2009 1:04 am
Posts: 308
Reply with quote
Post Re: Projectile in Front of Muzzle
mail2345 wrote:
Nice.

Willing to make more trig tutorials so I can stop copy pasting trig code?

I could, though I can't think of any more practical uses for Trig off the top of my head.

Really, if you can get sin() and cos() down, your pretty much set on trig for CC.


Sun Jun 21, 2009 2:28 am
Profile
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post Re: Projectile in Front of Muzzle
Go over math.atan2, it's ridiculously useful.


Sun Jun 21, 2009 6:02 am
Profile
DRL Developer
DRL Developer
User avatar

Joined: Wed Dec 13, 2006 5:27 am
Posts: 3138
Location: A little south and a lot west of Moscow
Reply with quote
Post Re: Projectile in Front of Muzzle
I've got a mod that I should be releasing as soon as I find Thunder Toes for sprites that uses trig pretty heavily for this kind of stuff. Hopefully it will provide a good example.
Usually when I'm using this kind of stuff I just throw in two global functions to the game called "lengthdir_x" and "lengthdir_y", which do these calculations for you. Makes typing things out a bit easier.
Good job explaining it to folks, by the way.


Sun Jun 21, 2009 6:54 am
Profile WWW
User avatar

Joined: Sun May 11, 2008 12:50 pm
Posts: 899
Reply with quote
Post Re: Projectile in Front of Muzzle
Oh nice, for someone who has never learned trig, this was very clear and easy to understand. Thanks for the explanation.


Sun Jun 21, 2009 6:56 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 8 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.078s | 15 Queries | GZIP : Off ]