One thing that doesn't strike a new programmer's mind is the use of maths that will be needed for they're games/apps. Luckily for us, DarkBasic has some commands that mean we
don't have to use maths, i.e. the Move Object command. We will go over that one later.
Well, first of all, I'm assuming you know your basic arithmetic. + - * / ( ) ^ = and all those signs. It's qute obvious that you will have to use
a=b+c, but sometimes it
can go alot further than just that. Sometime's the complex maths will be needed for more complicated movement, or camera positioning. Our very own RUccus has made some pretty
descent camera functions in the past, but I believe he used limbs. That's fine, I don't have a problem, but I choose to use maths because you don't have to worry about the extra
objects.
Part 1 - Trig
----------------
Nobody likes the sound of this, I can tell, it's like being a maths teacher, haha. Well, don't worry, I find in programming, you don't have to know about the double angle rule,
or your signs, it's actually alot simpler.
If you can imagine a sine wave
it's just a very basic wave. But, look at the angle going along the x axis. If you think of the angle being 0 degrees, the y coordinate will be 0. So, if you try to imagine your
player's angle is 0 degrees, the
X velocity (Speed, but in a certain direction), will be 0. If the angle is 90, the y coordinate wil be 1, so if you're player's turned 90
degrees, i.e. facing to the left, the x velocity will be 1.
So, if you look at the cosine wave
It's almost the same, just shifted 90 degrees. So, if you're angle is 0 degrees, the y coordinate will be 1, if you're angle is 90 degrees, the y coord. will be 0.
Can you see what's happening? Using the sin() and cos() commands, you can easily make some simple movement.
speed = 100 * (upkey()-downkey())
angle#=wrapvalue(angle#+(leftkey()-rightkey()))
vx#=sin(angle#)*speed
vz#=cos(angle#)*speed
x#=x#+vx#
z#=z#+vz#
It's really quite easy to understand. For people who know more about trig, you will know about the 'tan' command... Tan is stupid and you will never need to use it, get it out
of your head
So, how is the 'move object' command made?
Well, it's not that hard. It just takes a bit of understanding.
vx = sin(angy) * cos(angx) * speed
vy = cos(angx) * speed
vz = cos(angy) * cos(angx) * speed
First, you will notice there's no need for the Z angle. Well, I haven't ever needed it.
Well, the vx and vz variables are almost what we just did, but it's multiplied by the cos of the x angle. That's because the more upwards you face, the less velocity we want.
Because of that, we can just make the y velocity the cos of the x angle. It makes perfect sense, but as I already said, take some time to understand it.
You may think there's no point in it since there's the move object command, but it helps for things like a move object command without facing the way yu're moving, or maybe a
3rd person camera that's in space, where you can rotate in all sorts of ways.
Another great use for those
waves, is to make something move up and down. It's good for making something move up and down, i.e. gunswaying.
swang#=wrapvalue(swang#+3)
height#=sin(swang#)*10
That will change the height variable to go up and down
Useful for alot of things.
Part 2 - Some tricks
-----------------------
There are alot of tricks you can do which are performed using maths. For example, maybe making a variable gradually go towards 0. You may want to just use curvevalue(), but,
it's much easier to just multiply it my a number slightly smaller than one. x=x*0.98. Eventually, x will become 0. Similarly, you can multiply it by something bigger than 1, to
make it bigger - x=x*1.1, which will ofcourse make it rise and rise.
Another usefull trick that's widely used, (but since the discovery of vectors has been given up mostly), is the distance formula.
distance = sqrt(dx^2 + dy^2 + dz^2)
Pretty simple, the distance is the square root of the x distance squared, the y distance squared, and the z distance squared. What most people don't know, is how the sqrt()
function is formed.
You can easily make some similar. To get the square root of something, (the square root being that number that if you multiply it by itself, you get the number you put in), is
just the the number, to the power of a half - sqrt = x^0.5 - Basically, to get the root of something, you put it to the power of 1/the root number, so to get the cubed root, you
would do cbrt=x^1/3, the.. quaric (?) root, would be qtrt=x^1/4
Here's a simple root function:
function root(num,root)
new#=num^(1/root)
endfunction new#
Easy as pi...
Speaking of pi (3.1415926537) (you only need to know 3.14), you can work out things like the area of a circle, the circumfarance (perimiter, distance round it), volume of a
sphere, etc.
A=pi * radius^2 - The area, is pi (3.14), multiplied by the radius squared.
C=pi * diatmeter - The distance round the circle, is pi * the diameter.
-------------------------------------------------------------------------------------------------
And that concludes todays maths lesson. All of the techniques above are usefull for something, especially the trig techniques (which I use, alot). This is the basics for another
tutorial I might write, on advanced physics
Enjoy!