Anda di halaman 1dari 4

# Tips and Tricks

Ok, in my short span of programming I have ran into a bunch of small tricks and methods of doing
things that seem to apply to all kinds of programming. Certain algorithms can be used for a variety of
applications...

Index:

## • Circular Motion Formula

• Distance Formula
• Interpolation

## ~The distance formula

This algorithm is very simple, it can be applied to 2d and 3d points. It's purpose is to return the actual
distance between two given points. For this example, (x1,y1) represent one of the points, and (x2,y2)
represents the other point. This equation will give you the 2d distance between those two points:
Distance#=SQR((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))

There you have it, you now know the 2d distance between those two points. Of course, another way to
write that would be:
Distance#=SQR(pow(x1 - x2, 2) + pow(y1 - y2, 2))

Ok, so now you know how to return the 2d distance... What about 3d... I know what you're thinking,
this is going to be a pain... If so, your wrong, its just as easy...
Distance#=SQR((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 -
z2))

## So, what can I do with the distance?

• If you know the radius of two sphere's or circles, you can tell if they are touching by asking if
the distance between their center's is less then the sum of each of their radius's combined. IE: if
you have a circle that has a radius of 10 and another circle that has a radius of 3... If the distance
between them is equal or less than 13 (10+3) then you know they have to be touching!
• If you have a bunch of billboards in a scene, and you want to blend them... You will find that the
order they are rendered in highly affects the way they blend together. The proper way is to
render them from the furthest object away, to the closest object... That way the ones being
drawn last can properly blend OVER the ones drawn first. One way of doing this is to find the
distance from each object to the camera in order to create a list of depth. Then, you can use this
list to determine the order of which they are drawn... No more choppy blending!
• When creating a cellular texture, you first create a bunch of random points... Then you scan
through the X and Y of each pixel in the texture and make that pixels brightness (or alpha) the
distance from the closest cell (or random point)... This may sound very complex, but its actually
a very small code and very easy to use.

## - Faster distance check

Okay, so your going to test the distance between two points, by using the fowowing code:
Distance# = SQR((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 -
z2))
if Distance# < 10 then
..{do stuff}..
endif

However, you notice a slowdown in your program when you use this in a loop. This is becuase the
SQR function is not CPU friendly. A more efficiant way to do this is to square both sides of the
equation, i.e.
Distance# = ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 -
z2))
if Distance# <= 10*10 then
..{do stuff}..
endif

## ~Circular motion formula

This formula will work with all kinds of applications and games. It can be used to... Draw a circle,
move characters in the direction they are facing, create armature systems, and probably alot more.

It simply requires two variables to go into the formula... The angle and the radius.

Using those two variables it will produce two other variables... The X and the Y.

The angle is the current angle around the circle that we are trying to find the location of.
The radius is the distance from the center of the circle to the edge of the circlue. This is a common
variable used in geometry to denote the size of a circle.
The X and Y that are produced are where we would be if we were at the givin angle of a circle with the
With that said... Here's our formula:

It doesnt get much easier if you ask me. Now, let me explain a bit more... Our circle starts on the left
side, and goes clockwise around... So, an angle of 0 degree's would be on the left side of the circle... 90
degrees would be on the top... 180 degrees would be on the right... And 270 would be on the bottom...

Also, you may need to move your circle somewhere... How do we do that? Just add the X and Y of the
circle to the formula... So, if we decide to put the center of our circle at 100, 100 then it would look like
this...

Ok, so... using all of that together, we can draw ourselves a circle like this:
'Setup OpenGL to render in 2d
glMatrixMode(gl_Projection)
glPushMatrix()
glOrtho(0,WindowWidth(),WindowHeight(),0,-1,1)
glMatrixMode(gl_ModelView)
glPushMatrix()
glDisable(gl_depth_test)

dim Angle#
dim CircleX#
dim CircleY#
dim X#
dim Y#

CircleX#=100
CircleY#=100

## 'For/next loop to draw point at each angle

for Angle# = 1 to 360
X# = CircleX# + CosD(Angle#) * Radius#
Y# = CircleY# + SinD(Angle#) * Radius#
'GL-POINTS to draw points
glBegin(GL_POINTS)
glVertex2f(X#, Y#)
glEnd()
next

## 'Swap buffers to update screen

SwapBuffers()
~Interpolation
Interpolating is basically just finding a value for a given percent between two other values. What the
heck does that mean? It means... If point1 equals 0 and point2 equals 1... Then 50% between them
would be equal .5 Ok, so what is interpolation used for? All kinds of stuff! Animating models... Terrain
collision... Color gradients... Basically, anything that requires us to smoothly find all of the values
between point1 and point2. Here is an example of interpolation:
Point1 = 10
Point2 = 15
Percent = .5
NewPoint = (Point1 * (1 - Percent)) + Point2 * Percent

What exactly did we just do? Well, Point1 and Point2 are the two values we are interpolating between...
Percent is the percent between the two values we are trying to find the value of... NewPoint is the result
of the interpolation. The percent is generally between 0 and 1.
0 will give us the value of Point1, 1 will give us the value of Point2... And naturally, .5 will give us the
value that is HALFWAY between Point1 and Point2. And that is what interpolating does! Here are a
few ways you can use interpolation in your programming...
• With 3d models, if you want to morph from one model to another, in such a case as animating
models, you simply use the vertices x,y,z from model1 as the "Point1" in the above equation,
and the vertices x,y,z from model2 as the "Point2" Then, using a percent you can easily morph
from model1 to model2 by starting at 0 and gradually fading to 1. Simply put... Thats how you
animate 3d models.
• To find the Y of any given (X,Z) of a terrain, you simply interpolate the Y of the four corners
for the square that the (X,Z) are within... But how do you interpolate between four values? Well,
first you interpolate the Y between the top two points of the square along the X axis... Then, you
interpolate the Y between the bottom two points of the square along the X axis... THEN, you
interpolate between those two values (the result of those two interpolations) along the Z axis...
The result (after alot of interpolating) is the elevation of the terrain for any given (X,Z) location
on the terrain.
• To create a gradient effect between two colors, simple interpolate between the RGB of color1 as
"Point1" in the above equation... And then the color2's RGB as "Point2" in the equation. Just
gradually increase the percent between the two and you can create some amazing gradient
effects.