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

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)
1. Maths Distance Formula, 9.7 out of 10 based on 3 ratings

Related posts:

  1. 2. Maths Pythagoras Theorem
  2. 4. Maths Vector Length
  3. 3. Maths Lines
  4. 5. Maths Vector Dot Product
  5. 6. Maths Trigonometry

Write a comment

Improve the web with Nofollow Reciprocity.