Data Realms Fan Forums
http://forums.datarealms.com/

Projectile in Front of Muzzle
http://forums.datarealms.com/viewtopic.php?f=73&t=15459
Page 1 of 1

Author:  Kyred [ Sun Jun 21, 2009 1:51 am ]
Post subject:  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) );

Author:  Grif [ Sun Jun 21, 2009 2:09 am ]
Post subject:  Re: Projectile in Front of Muzzle

Yup, trig is useful stuff.

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

Author:  ProjektTHOR [ Sun Jun 21, 2009 2:12 am ]
Post subject:  Re: Projectile in Front of Muzzle

I guarantee you half of this forum saw math and was like "fgsfds"

Author:  mail2345 [ Sun Jun 21, 2009 2:20 am ]
Post subject:  Re: Projectile in Front of Muzzle

Nice.

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

Author:  Kyred [ Sun Jun 21, 2009 2:28 am ]
Post subject:  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.

Author:  Grif [ Sun Jun 21, 2009 6:02 am ]
Post subject:  Re: Projectile in Front of Muzzle

Go over math.atan2, it's ridiculously useful.

Author:  TheLastBanana [ Sun Jun 21, 2009 6:54 am ]
Post subject:  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.

Author:  Roon3 [ Sun Jun 21, 2009 6:56 am ]
Post subject:  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.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/