View unanswered posts | View active topics It is currently Mon Oct 22, 2018 3:03 am



Reply to topic  [ 8 posts ] 
 Grif's Triangle Tutorial (AKA Lua Lesson Two) 
Author Message
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post Grif's Triangle Tutorial (AKA Lua Lesson Two)
'k so I've heard people are having some issues with triangles. That's cool, it's not the most intuitive thing. Now, the rest of this lesson assumes you know basic trigonometry; this includes "SOHCAHTOA" and not a whole lot else. So, here we go.

Now, for an example, I'm going to use my Teleporter code; it's fairly simple and only really involves one piece of code.

The code is here, stripped to absolute barebones: http://pastebin.com/m15661553

Now, notice the two functions in the Create event. This is a function DEFINITION; it's setting these functions up so that they can be called later. Also take notice of this line: "function lengthdir_x(dist,ang)".
This specifies the arguments that the function will require. So, how does this all work?

Let's reference a diagram (because diagrams are always dandy)
Image

Now, you notice that there's a number of things going on here. First, there's a shitty rendition of a person. Second, there's a number of labelled variables, and several that aren't. So, how does it all work? Basically, using some simple trigonometry on known variables, the two functions return modifiers for self.pos that get you where you were pointing. But how does THAT all work? Quite simply, really.

Look back at the lua script. When the actor is in state body jump (using the jetpack), change the position by lengthdir_x and lengthdir_y. Both lengthdir functions have the same arguments; distance (in this case a constant of 96) and self:GetAimAngle. Since the script is run on an actor, you are able to directly access aim angle. So, with that angle and your distance (which becomes the length of the hypotenuse), you can "draw" your triangle.

So, based on hypotenuse and two angles (since one is always always ALWAYS going to be ninety degrees), how do you get the remaining two sides? Sin and cosine. Now, the math.cos and math.sin functions return a value based on the angle they're given. Since one of the function arguments includes angle, it's covered inside the function. The amount to modify X and Y to is determined through simple multiplication; again, it's all based on trigonometric principles.

So then, it's a simple matter of addition. We know how much to change the position by, and we know what the current position is, so we simply set the position again.

Any questions?


Sun Jun 07, 2009 3:17 am
Profile
User avatar

Joined: Thu Mar 06, 2008 10:54 pm
Posts: 1357
Location: USA
Reply with quote
Post Re: Grif's Triangle Tutorial (AKA Lua Lesson Two)
So your teleporter teleports them a specfic distance every time?


Sun Jun 07, 2009 4:09 am
Profile
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post Re: Grif's Triangle Tutorial (AKA Lua Lesson Two)
Exactly; that's the way I wanted it set up and why I used sin and cos specifically. However, this is just a basic primer; the same principles apply to any use of trigonometry.


Sun Jun 07, 2009 4:12 am
Profile
User avatar

Joined: Thu Mar 06, 2008 10:54 pm
Posts: 1357
Location: USA
Reply with quote
Post Re: Grif's Triangle Tutorial (AKA Lua Lesson Two)
Okay, so using sin and cosine, can you make an imaginary circle around said actor?


Sun Jun 07, 2009 4:24 am
Profile
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post Re: Grif's Triangle Tutorial (AKA Lua Lesson Two)
Yes, pretty much.

That's basically what I did with the plane bomb and gun spawn points.


Sun Jun 07, 2009 4:27 am
Profile
User avatar

Joined: Thu Mar 06, 2008 10:54 pm
Posts: 1357
Location: USA
Reply with quote
Post Re: Grif's Triangle Tutorial (AKA Lua Lesson Two)
Hah. I still dont know how to make one.... *sigh*


Sun Jun 07, 2009 4:28 am
Profile
User avatar

Joined: Sun Jul 13, 2008 9:57 am
Posts: 4886
Location: some compy
Reply with quote
Post Re: Grif's Triangle Tutorial (AKA Lua Lesson Two)
pastebin expired.
would be handy for those that dont know trig, refreshing to see some handy tutorials.


Wed Jul 29, 2009 10:48 pm
Profile WWW
User avatar

Joined: Fri Mar 13, 2009 2:31 am
Posts: 217
Location: Earth... I think
Reply with quote
Post Re: Grif's Triangle Tutorial (AKA Lua Lesson Two)
Grif... how should I put this... :|


THANK YOU!


Less abuse of smilies please.

its not abuse its an army of slaves and I hardly think 4 smilies is an abuse... bah who cares


Fri Aug 21, 2009 2:37 am
Profile
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.017s | 13 Queries | GZIP : Off ]