 Magnitude Tutorial ● SmileBASIC Source

Register
Space-Restriction Challenge QSP Phase Ends on March 26!

# Magnitude Tutorial

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=3 3-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=9 3^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-POSB)*ABS(POSA-POSB))+(ABS(POSA-POSB)*ABS(POSA-POSB))))``` 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!
Updated
Rating
4 Comment(s) Simeon Scholar Received for knowing a great deal about programming topics Achievements Amazing Page Hidden Achievements Drawing I 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 angle 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself you can also (I think) just pass DY and DX directly into ATAN without any normalization Chemicalex Drawing I like to draw! Hobbies RNG Hidden Easter Eggs Third Year My account is over 3 years old Website Yeah, there's way easier ways to figure this out, but I put it in a way that's easier to understand yet longer. 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself |X|² = X²