Featured Posts

8. Bump Mapping in GLSL8. Bump Mapping in GLSL Introduction Bump mapping is essential in todays computer games, and computer graphics in general. Would you like to know the best thing about it? It is extremely simple to implement. Bump mapping works...

Read more

Swiftless GLSL Shader DeveloperSwiftless GLSL Shader Developer Swiftless GLSL Shader Developer   Version 0.1a Currently Swiftless GLSL Shader Developer is in it's first public release, and is currently in alpha status, meaning it is not complete and may contain...

Read more

Wordpress Optimization Wordpress Website Optimizations Introduction Wordpress itself is a fairly wonderful tool. Since switching to it, I find it is a lot quicker to make changes to my website and it is also quicker to get...

Read more

36. OpenGL Framebuffers36. OpenGL Framebuffers Introduction Frame buffers are one of those mythical things that we have all heard of, but many beginner OpenGL developers avoid because there is not much information about them, and they can be confusing...

Read more

1. Terrain Class1. Terrain Class Terrain is one of those things that so far, hasn't been perfectly recreated in computer graphics. But it is almost there! Looking over a beautiful landscape can be one of the most amazing feelings in the...

Read more

  • Prev
  • Next

7. Maths Radians

Posted on : 25-03-2010 | By : Swiftless | In : Maths

Tags: , , ,

0

Welcome to Radians. Radians are a huge concept that really starts taking you into a new level of maths. Although, I personally find it rather simple. It is all about trigonometry. In fact it is the very core of trigonometry, only now we are going to see the whole picture.

Now there are two types of angles, you have your degrees which go up to which can range from 0 to 360 in a circle. And your radians which range from 0 to 2PI.

Hence:

0 degrees = 0 radians

90 degrees = PI/2 radians

180 degrees = PI radians

270 degrees = 3PI/2 radians

360 degrees = 2PI radians

Now lets just take a look at converting Degrees to Radians and Radians back to Degrees.

To convert from Degrees to Radians:

Degrees * PI / 180

Also written as:

Degrees / 180 * PI

To convert from Radians to Degrees:

Radians * 180 / PI

Also written as:

Radians / PI * 180

Now lets take a look at our Unit Circle from which trigonometry was born:

Radian Circle

Lookie, a right angled triangle!

Now this is a unit circle, as you can see, it has a radius of 1 unit.

So lets start taking a look at how we can find point P on that diagram.

Using our lovely trigonometry:

cos@ = x/1

therefore:

cos@ = x

!! We have our x position from our camera class in OpenGL !!

sin@ = y/1

therefore:

sin@ = y

!! We have our y position from our camera class in OpenGL !!

And now you have your point P. Simple isn’t it.

If you have any questions, please email me at swiftless@gmail.com

VN:F [1.9.3_1094]
Please rate so I know where to improve the site. 1 means needs a lot of improvement, 10 means perfect. If you leave a low rating, please state why. I don't want people just coming to bash the site.
Rating: 8.3/10 (4 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

6. Maths Trigonometry

Posted on : 25-03-2010 | By : Swiftless | In : Maths

Tags: , , ,

0

Trigonometry is based on triangles. The point of trigonometry is to find either the unknown side or angle of a triangle, when we know a side and an angle.

Here is a little diagram to get you started (pretty isnt it).
Trigonometry Triangle

Now:
O stands for opposite.
A stands for adjacent.
H stands for hypotenuse.
alpha is the angle in the corner where the angle is either known, or needed to be found.

Ok, now the 3 trigonomic functions are:
Sine, Cosine and Tangent

These will be known as:
sin, cos and tan on your calculator.

And respectively they each have their own inverse function known as:

sin^-1, cos^-1 and tan^-1

Here I am going to let alpha be @ as I dont have any alpha key to press :P
so to remind you alpha is our angle, so @ will be known now as our angle.

Now the
sin@ = o/h
cos@ = a/h
tan@ = o/a

You can easily remember this by a little saying:
soh cah toa

So why do we need this? We need this when we want to find the length of a side in a triangle. If you have the length A and the angle @, then you can use trigonometry to find either H or O. And likewise for the lengths H and O.

Next we can use trigonometry to find the unknown angle.

To do this, you start with your basic formula, eg:
sin@ = o/h

Then we have to get sin on the other side, so we times both sides by the opposite
of sine which is sin^-1, which should also be on your calculator.

so by doing this we end up with:

@ = sin^-1(o/h)

And that is all there is to trigonometry. You can find the sides and angles of a triangle using this.

But how would we use this in a game you might say. Well take a little look at my next diagram (god I love mspaint).

Trigonometry Circle

In the image above it shows you a rough idea of how you would make this into a camera for a game. You have yourself in the centre and have 360 degrees of rotation around. You can use trigonometry, and with your current angle, and knowing your current position to A(where you wish to go) you can determine your point at the corner of H and O.

But that is just a quick reference I whipped up in 10 minutes. In the Radian tutorial you will find out just how the camera class relates to trigonometry in detail.

If you have any questions, please email me at swiftless@gmail.com

VN:F [1.9.3_1094]
Please rate so I know where to improve the site. 1 means needs a lot of improvement, 10 means perfect. If you leave a low rating, please state why. I don't want people just coming to bash the site.
Rating: 9.5/10 (2 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

5. Maths Vector Dot Product

Posted on : 25-03-2010 | By : Swiftless | In : Maths

Tags: , , , ,

0

Here we are going to be looking at the infamous dot product AKA scalar product.

Dot products are used in computer programming to work out normals for 3d objects. A normal is a vector, perpendicular to the plane it is on.

I couldn’t really find a meaning to what the dot product actually is. So I am going to class it as a means of finding an angle or a length of a vector.

Now there are 3 main ways for writing a dot product equation.

a.b = a1b1 + a2b2 = |a| |b| cos @

Where a.b = [a1, b1] . [a2, b2]

a.b = a1 * b1 + a2 * b2

Which comes out the same as our second formula. While our third formula takes the length of a times by the length of b times by the cosine of theta where theta is the angle between vectors a and b.

Now if the angle between a and b is equal to 90 then a is perpendicular to b.

And that is all there really is to the infamous dot product. It is a rather simple concept really.

So lets have a little play with that. Say we have the points:

AB = [4, -2]

BC = [-1, -2]

And we want to find out if AB is perpendicular to BC. We now have to do the dot product. We cannot use the third method as we don’t know any angles, although we could work them out, but I will explain that in a different topic. As the third equation is simply ‘plug n play’, whack in the numbers and press the calculate button on your calculator.

So to find the dot product we get:

AB . BC

AB . BC = (4 * -1) + (-2 * -2)

AB . BC = -4 + 4

AB . BC = 0

Therefore AB is perpendicular to BC.

If you have any questions, please email me at swiftless@gmail.com

VN:F [1.9.3_1094]
Please rate so I know where to improve the site. 1 means needs a lot of improvement, 10 means perfect. If you leave a low rating, please state why. I don't want people just coming to bash the site.
Rating: 8.0/10 (2 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

4. Maths Vector Length

Posted on : 25-03-2010 | By : Swiftless | In : Maths

Tags: , , ,

1

The first thing we will look at is how to work out the length of a vector.

Vector Length 1

Take a look at the diagram and try to grasp that we can determine the length of every vector there.

————————Figure 1 – The triangle on the left———————-

First go to the iamge in the left. You will see a triangle with sides of length a, b and c.

The vector a is facing towards c, the vector b is facing towards a and the vector c is facing towards b.

So to work out the length of the vectors we start at the end of the vector WITHOUT the arrow and work our way around to the end with the arrow.

To work out the length of a, we would start at the end of a, so the start of b and work around to the end of a with the arrow. So our length would be:

a = -b + -c

Which comes down to:

a = -b-c

If we were to work out negative a, we would get:

-a = c + b

Because to work out the negative value, we start at the other end.

For our formulae for a to be:

a = -b-c

You have to take into account that moving in the same direction is positive and moving opposite to the arrow is negative.

So all our lengths for our triangle would be:

a = -b-c

b = -c-a

c = -a-b

————————Figure 2 – The square on the right———————-

Vector Length 2

If you follow our rules from above, the lengths of our vectors come out to:

a = -d-c+b

b = c+d+a

c = b-a-d

d = -c+b-a

Keep in mind that no matter what shape you have, you ALWAYS work it out the same way. And thanks to vectors being what they are, this will always work.

__LENGTH OF A VECTOR PART 2__

In this part, we are going to be looking at the length of a vector in almost the exact same way as before. Only this time, the characters, a,b and c are not lines, but points.

Vector Length 3

To work out the length of these, you just have to work it out a little differently.

Take point A and point C. Now the length of that vector is AC (with a line above it)

Vector Length 4

The arrow above it just represents a vector instead of a normal line.

So in this diagram we have lengths:

AC

CB

BA

And to work that out is exactly the same as before.

AC = |AC| or -|AB|-|BC|

Where the lines | | represent the length.

__LENGTH OF A VECTOR PART 3__

Here we are going to be acctually calculating the length of the vector using numbers.

Vector Length 5

A is going to be located at (1,1)

B is going to be located at (1,0)

C is going to be located at (2,0.5)

Now the line AC comes to:

AC = [cX - aX, cY - aY]

Therefore:

AC = [2 - 1, 0.5 - 1]

AC = [1, -0.5]

Now to work out the length/distance:

d = sqrt(x^2 + y^2)

And vectors dont acctually have a length measuring system like cm, m, km etc. They are just measured in units.

so AC = sqrt(1^2 + (-0.55)^2)

AC = sqrt(1 + 0.25)

AC = sqrt(1.25) units.

If you have any questions, please email me at swiftless@gmail.com

VN:F [1.9.3_1094]
Please rate so I know where to improve the site. 1 means needs a lot of improvement, 10 means perfect. If you leave a low rating, please state why. I don't want people just coming to bash the site.
Rating: 1.0/10 (1 vote cast)
VN:F [1.9.3_1094]
Rating: -1 (from 1 vote)

3. Maths Lines

Posted on : 25-03-2010 | By : Swiftless | In : Maths

Tags: , ,

3

I am going to start this tutorial now, but I probably will not have time to finish it. But here goes…

The equation of a line can be shown in 2 different forms. There is the General Equation and the Slope-Intercept form. They are:

General Equation: aX + bY = C
Slope-Intercept form: Y = mX + C

I find the slope intercept form best, as it tells you straight out where the formula crosses the y axis at X and the slope of the line is M.

Let us reuse this diagram:
Distance Formula Triangle

Now first we will look at the slope:
The formula for a slope between 2 points is:
M = y2-y1
——
x2-x1

Simple enough. Now to find C, we use the equation Y = mX + C:
(y2) = M * (x2) + C

So we are substituting in one of the set of points, also adding in M which we solved before.

so you end up with:
(y2)
—– – M = C
(x2)

Now that we have both C and M we can determine that the formula is:
y = mX + C by adding in the numbers for C and M.

Now that we have our formula in the slope-intercept form, what about converting that to the General form?

Y = mX + C

Y
— – X = C
M

I will add more onto this later, as it is a tad longer than the others. Stay tuned….

If you have any questions, please email me at swiftless@gmail.com

VN:F [1.9.3_1094]
Please rate so I know where to improve the site. 1 means needs a lot of improvement, 10 means perfect. If you leave a low rating, please state why. I don't want people just coming to bash the site.
Rating: 7.0/10 (1 vote cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

2. Maths Pythagoras Theorem

Posted on : 25-03-2010 | By : Swiftless | In : Maths

Tags: , , ,

2

Most people who have done maths in high school, would have heard of Pythagoras theorem.

This is used to work out the unknown side of a triangle, if you know the other 2 sides.

Pythagoras Triangle

Here you can see the sides, A, B and C.

Now Pythagoras theorem states that A^2 + B^2 = C^2

So if you know any two sides, you add them in, rearrange the formula if needed, and then you can solve for the unkown sides.

So how would you use this in a game? What about collision detection? Using this you can find quite easily in a 2 dimensional plane when 2 points will meet.

They will meet when A^2 + B^2 = 0

Quite simple really, you take the position of A and B on the X axis and do the equation, then if it comes up with an answer of 0, you then take the position of A and B on the Y axis, then if that also comes to 0, they must be colliding.

So to put that in other words, we are finding the distance :-) If the distance is nothing, then they are located in the same position.

Not sure if that is right, sounds right from my position, although it is 1:18 am.

If you have any questions, please email me at swiftless@gmail.com

VN:F [1.9.3_1094]
Please rate so I know where to improve the site. 1 means needs a lot of improvement, 10 means perfect. If you leave a low rating, please state why. I don't want people just coming to bash the site.
Rating: 7.0/10 (1 vote cast)
VN:F [1.9.3_1094]
Rating: +1 (from 1 vote)

1. Maths Distance Formula

Posted on : 25-03-2010 | By : Swiftless | In : Maths

Tags: , ,

0

The distance formula, like pythagoras theorem, can be used to determine collision detection quite easily.

First off, you must know the points at which the objects are located.

Say object 1 is located at (x1, y1) and object 2 is located at (x2, y2). Both on a 2 dimensional plane.

Distance Formulae Triangle

Now I must clarify here that sqrt means the square root. There is no square root key on my keyboard, and in most programming languages it is called sqrt or sqr.

Ok now the distance formula is:

D = sqrt( (x2 – x1)^2 + (y2 – y1) ^2)

Which expanded looks like:

D = sqrt( (x2 – x1)*(x2 – x1) + (y2 – y1) *(y2 – y1))

By using this, if D is either 0 or less than a set collision distance we need, then there is a collision.

But how would we do this in a 3D world you might be wondering.

Well don’t wonder any longer, it is almost the same. You take your points which are located at:
(x1, y1) and (x2, y2)

Then simply add in the z values. So they would be located at:

(x1, y1, z1) and (x2, y2, z2)

And to get this into our equation, we add the z2 value take the z1 value and square it.

So our formulae now looks like:

D = sqrt( (x2 – x1)^2 + (y2 – y1) ^2 + (z2 – z1) ^2)

Which when expanded looks like:

D = sqrt( (x2 – x1)*(x2 – x1) + (y2 – y1) *(y2 – y1) + (z2 – z1) *(z2 – z1))

And there we have it, we can calculate the distance between 2 points in either 2d or 3d space.

If you have any questions, please email me at swiftless@gmail.com

VN:F [1.9.3_1094]
Please rate so I know where to improve the site. 1 means needs a lot of improvement, 10 means perfect. If you leave a low rating, please state why. I don't want people just coming to bash the site.
Rating: 9.7/10 (3 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
Improve the web with Nofollow Reciprocity.