Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Tutorial - Maths tricks for dummies

Author
Message
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 29th Dec 2006 02:46
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.




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.



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:


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!

Kieran
18
Years of Service
User Offline
Joined: 6th Aug 2006
Location: Hamilton, New Zealand
Posted: 29th Dec 2006 09:51
Nice tutorial it would certainly help someone understand how maths can help/be used in making games

Bubbly world
17
Years of Service
User Offline
Joined: 30th Dec 2006
Location: In the speaker.
Posted: 30th Dec 2006 12:59
You've got Pi all wrong. It goes:
3.1415926535
Not:
3.1415926537
Nice Tut BTW.

Current Pi digits known off by heart:
3.14159265358979346264
Rudolpho
18
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 4th Jan 2007 17:19
Quote: "just multiply it my a number slightly smaller than one. x=x*0.98. Eventually, x will become 0. "

I believe it would not

"I kören hörs de brummande busarna Björnligan och Gondolen"
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 4th Jan 2007 17:51 Edited at: 4th Jan 2007 17:51
Quote: "Quote: "just multiply it my a number slightly smaller than one. x=x*0.98. Eventually, x will become 0. "
I believe it would not "


You usually add a little extra code to state that if X < n then X = 0, where n is a value you are happy to concede is close enough to 0 to not worry about. Otherwise, you can spend eternity performing unnecessary calculations.



Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 4th Jan 2007 19:19
I guess. But even if it's not, it is practically lol.

Bubbly World:
Same difference

lower logic
18
Years of Service
User Offline
Joined: 15th Jun 2006
Location:
Posted: 4th Jan 2007 22:13
Nice tutorial but the trig section doesn't feel complete without mentioning atanfull().
dononeton
20
Years of Service
User Offline
Joined: 12th Jun 2004
Location: Tusaloosa, AL : USA
Posted: 5th Jan 2007 01:00
Part 1 - Trig

Quote: "
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.
"


I am not understaning this
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 5th Jan 2007 02:08
I think that might be because I was careless and didn't make the images properly Basically, if you look at a regular sine wave, if you go along 0 on the x axis, the corresponding y coordinate will be 0. The distance along the x axis is an angle, so if the angle is 0, the magnitude (in this case, the speed along the x axis) will be 0. Now if you think about the player turning an extra 90 degress (facing completelly to the side) all the speed ill be in the X direction - so look at a pic of a sine wave again, at along 90, the y coordinate is 1 (the maximum a (co)sine wave can go). The whole thing is exactly the same for the Z axis, instead it uses Sine's cousin, cosine (which is a sine wave shifted along 90 degrees)

lower logic
18
Years of Service
User Offline
Joined: 15th Jun 2006
Location:
Posted: 5th Jan 2007 08:04
Quote: "I am not understaning this"

Here's an image that basically sums up how I first learned trig, I've never been good at explaining things, but there's always a chance it could help:

Basically if you know any 2 of the 4 parts that make up a 2d vector, you can get the other 2 parts, and this is useful because some things are easier to do in component form, such as bouncing, and other things are easier to do in length and angle form, such as setting a walking animation to the right speed, rotating objects to point in the direction they are moving, and turning the direction they are moving.
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 5th Jan 2007 23:03
Quote: "Our very own RUccus has made some pretty descent camera functions in the past, but I believe he used limbs. "


Hey! My latest one uses the curvevalue commands

Nice little tutorial zoto, you've brought a few things to my attention for sure.

- RUC'

Login to post a reply

Server time is: 2024-09-25 15:27:45
Your offset time is: 2024-09-25 15:27:45