View unanswered posts | View active topics It is currently Tue Mar 26, 2019 10:04 am

 Page 1 of 1 [ 8 posts ]
 Print view Previous topic | Next topic
Grif's Triangle Tutorial (AKA Lua Lesson Two)
Author Message
REAL AMERICAN HERO

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

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

Joined: Thu Mar 06, 2008 10:54 pm
Posts: 1357
Location: USA
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
REAL AMERICAN HERO

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

Joined: Thu Mar 06, 2008 10:54 pm
Posts: 1357
Location: USA
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
REAL AMERICAN HERO

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

Joined: Thu Mar 06, 2008 10:54 pm
Posts: 1357
Location: USA
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

Joined: Sun Jul 13, 2008 9:57 am
Posts: 4886
Location: some compy
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

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

THANK YOU!

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
Display posts from previous:  Sort by
 Page 1 of 1 [ 8 posts ]

#### Who is online

Users browsing this forum: ParanX

 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