|
Erinock
Guest
|
 |
« on: July 20, 2012, 10:41:15 PM » |
|
Hey guys, I have a question to issue that you guys can probably answer in a heartbeat, I have been clueless in this problem for a while so if you guys can answer it I would be really grateful. So I was never really the mathematical type, I was and still am always drawing and doing art things. In school I excelled in art and writing, but math was my weak-point. Its not that I dislike math, or cannot grasp it. I only didn't ever really enter anything to do with it. So when it comes to programming, I seem to lack in some areas. I can confidently program in GML, HTML and CSS but when it comes to certain parts of any programming languages I get stuck on 3 major things that I would like to use in my games to possibly enhance their quality; which is sine, cosine and tangent. I am asking if anybody can give me some basic guidelines as to what these do in a basic program? And how I can understand them as easily as addition, subtraction and multiplication in GML. so the crux of what I am trying to ask is: - How do these interact with variables?
- Whats an example of mainly cosine and sine in action?
- Whats a tip that will help me understand these?
Thanks guys, sorry for my naive questions but I would really like to know this better.
|
|
|
|
|
Logged
|
|
|
|
|
Paul Eres
|
 |
« Reply #1 on: July 20, 2012, 11:05:08 PM » |
|
mathematically, they are used to find an unknown angle (or an unknown length) of a triangle when some of the angles / sides are known -- you can read about them here: http://en.wikipedia.org/wiki/Sine (etc.) -- basically sin is the inverse of cos. i believe sin(argument0) will always be equal to (1 - cos(argument0))? tan is a much different thing: http://en.wikipedia.org/wiki/Tangent -- i don't actually use tan() very much in my programs, so i don't know its applications, i mainly use cos() and sin() in games, the main things cos and sin are used for calculations involving angles, and also for periodic 'wavelike' patterns or pulses (such as if you wanted a bullet to travel in a wave pattern instead of a straight line); they were also used in chiptunes for sound effect design. lots of stuff like that for an example of the calculations thing, you know the functions lengthdir_x, and lengthdir_y? they're very simple equations. lengthdir_x(argument0, argument1) is the same as "cos(argument1*pi/180)*argument0" for instance. but don't use the latter, using the former is actually faster due to how GM works. but it's good to know *how* it works
|
|
|
|
« Last Edit: July 20, 2012, 11:33:43 PM by Paul Eres »
|
Logged
|
|
|
|
|
thatshelby
|
 |
« Reply #2 on: July 20, 2012, 11:19:42 PM » |
|
sin(), when given a value from 0 to 359, returns a value from -1 to 1. It smoothly fluctuates between these two ranges by just incrementing a variable! You can then multiply and add to this return value to change it for your needs. input += 1; if (input > 359) { input = 0; } other_variable = sin(input * pi / 180) * 3;
I use this for tweens all the time. Some of the most common: -Making an object smoothly float up and down -With some other math, making an angle smoothly turn towards a destination angle -Pretty much anything with fluctuating a variable between a range. Going from say, 0, to 10, smoothly, would look like sin(input * pi / 180) * 5 + 5 It's pretty magical. You can multiply an input argument by pi / 180 to convert radians and degrees. These functions take radians, and we have degrees, so we put '* pi / 180' after 'input'. You could also make the variable go to 100, but I prefer degrees, because it's about 3.6x more precise... if you just add one. You can also multiply input by a number to make it faster. It's pretty flexible. Play around with it. Another small word of advice: lengthdir_x looks like "len * cos(dir * pi / 180)", but lengthdir_y looks like "-len * sin(dir * pi / 180)" ~ If you're working with coordinates, remember to negate the y-axis because these functions are for the typical coordinate plane where the Y-axis increases as you go up. cos() can be used for the same, but it will be the inverse. tan() I don't use much except for 3D games, and even then, quite uncommon.
|
|
|
|
|
Logged
|
|
|
|
|
rivon
|
 |
« Reply #3 on: July 21, 2012, 01:23:13 AM » |
|
i believe sin(argument0) will always be equal to (1 - cos(argument0))? No, that's wrong. Here's the correct version: sin 2x + cos 2x = 1 sin 2x = 1 - cos 2x sin x = sqrt(1 - cos2x)and vice versa: cos x = sqrt(1 - sin2x)
|
|
|
|
|
Logged
|
|
|
|
|
Erinock
Guest
|
 |
« Reply #4 on: July 21, 2012, 02:23:36 AM » |
|
|
|
|
|
|
Logged
|
|
|
|
|
BorisTheBrave
|
 |
« Reply #5 on: July 21, 2012, 03:12:44 AM » |
|
You may find it easier to use the polar functions in gml: point_distance, point_direction, lengthdir_x,lengthdir_y
They cover some of the basic uses of sin/cos and tan, but are phrased in a way that it is more obvious what they do.
|
|
|
|
|
Logged
|
|
|
|
|
Paul Eres
|
 |
« Reply #6 on: July 21, 2012, 01:40:42 PM » |
|
i believe sin(argument0) will always be equal to (1 - cos(argument0))? No, that's wrong. Here's the correct version: sin 2x + cos 2x = 1 sin 2x = 1 - cos 2x sin x = sqrt(1 - cos2x)and vice versa: cos x = sqrt(1 - sin2x)ah, yeah, forgot the squares, thanks
|
|
|
|
|
Logged
|
|
|
|
|
nikki
|
 |
« Reply #7 on: July 22, 2012, 08:11:05 AM » |
|
what really made sin/cos click for me is the insight in where they actually come from: draw a circle, when you trace the outline of it with your finger and only look at the horizontal movement your talking sinus. the vertical movement is cos (or viceversa i don't try to remember). then you see where the wave shapes come from (since your moving from -1 to +1). you also can imagine you fit a triangle in half that circle, then you know why [edit] almost [/edit] every post above goes on about triangles and angles. my highschool math teacher never made it as easy two nitpicking remarks by the way : I can confidently program in GML, HTML and CSS css and html are not programming languages (markup languages i believe they are called) input += 1; if (input > 359) { input = 0; } why not use : input += 1 input = input % 360
|
|
|
|
|
Logged
|
|
|
|
|
thatshelby
|
 |
« Reply #8 on: July 22, 2012, 07:26:02 PM » |
|
thaaaaaaaaaat's genius im dum thanks can i do input = (input + 1) mod 360;
?
|
|
|
|
|
Logged
|
|
|
|
|
Evan Balster
|
 |
« Reply #9 on: July 22, 2012, 08:06:51 PM » |
|
Do you have something in your kitchen (like a lime juicer) that you never use? sin and cos may be like this for you, for the time being. Game Maker's simpler functions lengthdir_x, lengthdir_y, point_distance and point_direction make for faster, simpler, more readable code in that system. Just because a tool is in the toolbox doesn't mean you need to use it. That said, it's a great way to make wavy motions. Try making an object move left and right and running this code each frame: y = ystart + 32 * sin(pi * x/32);
|
|
|
|
|
Logged
|
|
|
|
|
JakobProgsch
|
 |
« Reply #10 on: July 23, 2012, 12:16:26 AM » |
|
sin and cos are periodic so they totally work outside of [0,2*PI] (or [0,360] degrees, but degrees generally make stuff more annoying actually), so there isn't a need to explicitly convert the arguments to that range.
|
|
|
|
|
Logged
|
|
|
|
|
Evan Balster
|
 |
« Reply #11 on: July 23, 2012, 07:59:53 AM » |
|
Degrees are a bit more conventional in Game Maker; users are likely to be used to them.
|
|
|
|
|
Logged
|
|
|
|
|
Erinock
Guest
|
 |
« Reply #12 on: July 24, 2012, 04:23:22 AM » |
|
Whats this "PI" you keep putting inside the parentheses? I cant tell if its a variable or an actual function. Just because a tool is in the toolbox doesn't mean you need to use it.
Smart words right there, but if a tool is made, it has a purpose. And I intend to find its purpose to further my knowledge.
|
|
|
|
« Last Edit: July 24, 2012, 04:33:53 AM by Erinock »
|
Logged
|
|
|
|
Glyph
Level 6
Your ad here
|
 |
« Reply #13 on: July 24, 2012, 04:31:41 AM » |
|
pi (π)Really, you should probably have known about pi before delving into sines and cosines... It's pretty necessary when dealing with the unit circle and such. So read up!
|
|
|
|
|
Logged
|
Feel visible matter... Feel invisible matter... There is life everywhere...
|
|
|
|
Erinock
Guest
|
 |
« Reply #14 on: July 24, 2012, 04:38:01 AM » |
|
Ohk, I knew kindof what PI was, but I didn't know how it effected the code. Damn, I need to take a class in maths 
|
|
|
|
|
Logged
|
|
|
|
|