View unanswered posts | View active topics It is currently Sat Jan 18, 2020 9:49 am

 Page 1 of 1 [ 8 posts ]
 Print view Previous topic | Next topic
Projectile in Front of Muzzle
Author Message

Joined: Sun May 31, 2009 1:04 am
Posts: 308
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) );

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

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

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
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
Banned

Joined: Tue Feb 27, 2007 4:05 pm
Posts: 2527
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

Joined: Tue Nov 06, 2007 6:58 am
Posts: 2054
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

Joined: Sun May 31, 2009 1:04 am
Posts: 308
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
REAL AMERICAN HERO

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

Sun Jun 21, 2009 6:02 am
DRL Developer

Joined: Wed Dec 13, 2006 5:27 am
Posts: 3136
Location: A little south and a lot west of Moscow
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

Joined: Sun May 11, 2008 12:50 pm
Posts: 899
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
Display posts from previous:  Sort by
 Page 1 of 1 [ 8 posts ]

Who is online

Users browsing this forum: No registered users

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

Search for:
 Jump to:  Select a forum ------------------ Cortex Command    Dev Log    Wiki    Game Discussion    Game and Licensing Support    Mod Making       Lua Scripting       Requests    Mod Releases       Older Mods    Scene Releases       Scene Making Planetoid Pioneers    Game Discussion    Pioneer Program Lounge Data Realms Off-Topic    Off Topic Chat       Gaming       Forum Games    Site Support & Feedback