Magnitude represents the distance between two objects on a plane. In this tutorial I'll teach you about 2-Dimensional magnitude, but I'll also tell you how to convert it onto a 3D plane.
First off, you'll need to know what a hypotenuse is. If you already do, skip this segment.
A hypotenuse is a basic function of trigonometry: a^2+b^2=c^2. So if you square two values, say, 2 and 3, you get 4 and 9, and you add the two together. 4+9=13. Now you can find the answer by finding the square root of 13, or 3.6.
Now that we know this, we can continue.
Now, the basic function for finding the distance between two points is m=abs((x^2+y^2)^.5).
Let's say we have two points: a (5,3) and b (2,6). We want to find the distance between them.
First, let's subtract a from b using basic subtraction.
5-2=33-6=(-3)
Now we have the distance on both axis. The x axis is fine since the answer is an absolute value, but we need to convert your to an absolute since distance can't be negative. Let's call this new value f (3,3).
Now that everything have the distance falue, we need to convert it to a single value by finding the hypotenuse of the two values.
3^2=93^2=9
So, since a^2+b^2=c^2, we find the square root of 9, which is 3, which is out final answer: the distance between a and b is 3.
That was a very simple example, but that's because we were using traditional math to find the answer.
But how does this apply in SmileBASIC?
You can use this in as many ways as there are. I primarily use magnitude to measure the distance between a player character and a an enemy, when they get to the range of "noticing" each other. It can also be used to make objects that are x far away from your view not render as a lag remover/game mechanic.
I wrote a function here that finds the magnitude of two points in one line of code. Here's what it basically looks like:
'Define arrays POSA and POSB and assign two numbers to them
M=SQR((ABS(POSA[0]-POSB[0])*ABS(POSA[0]-POSB[0]))+(ABS(POSA[1]-POSB[1])*ABS(POSA[1]-POSB[1]))))
I also created a program that shows an example of this in action: 42W35W3V
If you want to use this in 3D terms the equation changes to m=abs(x^2+y^2+z^2)^.5.
Thanks for reading, all!
4 Comment(s)SimeonScholarReceived for knowing a great deal about programming topicsAchievementsAmazing PageHiddenAchievementsDrawingI like to draw!Hobbies
'Consider ( X1, Y1 ) as is a point on the screen, and ( X2, Y2 ) as another point.
'A smart way to go is to split this equation into three, so:
VAR DX=X2-X1
VAR DY=Y2-Y1
VAR DIST=SQR(DX*DX+DY*DY)
If you do it this way, you can then calculate the normalized angle without any sort of distance in it:
VAR AX=DX/DIST
VAR AY=DY/DIST
Now AX and AY are both values between -1 and 1, and combined, that give you just an angle12Me21Head AdminSecond YearMy account is over 2 years oldWebsiteSyntax HighlighterReceived for creating the code syntax highlighter on SBSNight PersonI like the quiet night and sleep late.Express Yourselfyou can also (I think) just pass DY and DX directly into ATAN without any normalizationChemicalexDrawingI like to draw!HobbiesRNGHiddenEaster EggsVideo GamesI like to play video games!HobbiesYeah, there's way easier ways to figure this out, but I put it in a way that's easier to understand yet longer.12Me21Head AdminSecond YearMy account is over 2 years oldWebsiteSyntax HighlighterReceived for creating the code syntax highlighter on SBSNight PersonI like the quiet night and sleep late.Express Yourself|X|² = X²
'Consider ( X1, Y1 ) as is a point on the screen, and ( X2, Y2 ) as another point. 'A smart way to go is to split this equation into three, so: VAR DX=X2-X1 VAR DY=Y2-Y1 VAR DIST=SQR(DX*DX+DY*DY)
If you do it this way, you can then calculate the normalized angle without any sort of distance in it:VAR AX=DX/DIST VAR AY=DY/DIST
Now AX and AY are both values between -1 and 1, and combined, that give you just an angle