For the people joining the forums or people that want to understand programming better this is the place for you. The code is written using Lua, C++, and Ogre.
Personally I would recommend learning C++ from this tutorial. A good Ogre tutorial is here Ogre's main functionally is to render (or display an image on the sceen) and set up windows and start the basic program.
Now some people may be familiar with programming in these languages but have no concept of how a 3d game works.
We must first understand how the computer sets up a three dimensional space. Programs like OpenGL and Ogre use the Cartesian Coordinate System, aka coordinate plane.
As you see in the image, basic shapes are drawn on this plane. If you place these basic shapes next to each other you can for a model or wireframe, which is almost always used by the aid of 3d modeling programs such as Blender. There is also a Z coordinate that simulates depth on your 2d computer screen, which the basic (X,Y) points will become (X,Y,Z).
As you can see from Princesses Peach castle from Mario 64, The landscape and the castle are all made from triangles. The computer program tells whether or not to put lines or render the objects as faces.
Rendering a simple cube in blender and exporting it as a .obj file, then opening it in notepad.
- Note:
The original format has this as one line, the multiple lines were put in for visual ease.
We can clearly the (X,Y,Z) pattern at the top, the v tells the program that there is a new vertex. The people who created the .obj format try to use a little memory as possible so it keeps the same points in space and then defines the faces of the polygon which is read by the bottom lines by indicating an f followed by the points that will be used to render the polygon. There are other 3d model file types .obj is the most basic other file types compress or encrypt the data.
Now as a programmer, understanding this, you can use mathematics to understand the world that is created. This is of great use for collision detection. Collision detection is checking if the object or character is within a certain distance to another object, lets say a tree. If you do not have hit detection the character will walk through the tree. Detection makes sure that the character does not do this, and keeps realism in the game. This is also important for limiting the size of the world, because it cannot go on forever the computer will run out of memory, this also includes games such as minecraft where they appear to be infinite are limited by the amount of memory your computer uses.
Let us step all the way back to 1 dimensional space and to build off of this idea.
Let this represent this numberline represent the X-axis.
Say we have a wall at 0 that extends all the way to infinity.
so if the character x value is greater then 0, don't move the character.
- Code:
if(CharacterX >= 0)
{
Do not move.
(if you wanted to check to see if the character could move into this position is stead of not moving you would have a boolean value, checking and moving for multiple collision could have the character move as many times as you check.)
}
That's nice but how often would you have a wall that goes on for infinity?
This shows the other side of the wall that stops at 3.
The wall is from 0 to 3. If the player is > 0 and the player is < 3 don't move the character.
- 1d Wall Code:
if(CharacterX >= 0 && CharacterX <= 3)
{
Do not Move. (Collision is not unique to a character position you could use it for enemies and or checking the mouse position for menu and, or anything you may need.)
}
Lets move up to a 2d collision detection.
The player has 3 coordinates at any time, moving to 2d this is what our previous wall at 0 to 3 would look like.
As you see this is correct, we specified nothing on the Y axis. We are checking for X > 0 and X < 3. The Y axis will go on forever, infinity.
As you have probably figured out, if we specify the Y axis we can then make a box where the character can not enter.
As you see we now have a rectangle such as a box on a 2d game in addition to the X-axis code we now add, if the Y is less then 6 and greater then -1.
- Spoiler:
if(CharacterX >= 0 && CharacterX <= 3 && CharacterY >= -1 && CharacterY <= 6)
{
Do not Move.
}
Circular collision can also be done very simply by using the distance formula
We are checking from the origin, (0,0). The distance to the outer edge is 3 anywhere around it.
realistically the hit detection on our character will not be a single point, but a box, simply check all the vertices that make up the box to see if they are inside the circle.
To get the detection of two circle simply check the distance of both radi.
Circles and squares are nice but we need some hardcore detection.
Take the slope formula Y = mX+b.
This formula is Y = 1*X -1.
Now say we have a ramp for a 2d game and we want to keep them on the ramp, we would stop gravity if they fell below the line.
so if the CharacterY is less then 1*X - 1, stop them from falling.
- SlopeCollision:
if(CharacterY <= slope*CharacterX -offset)
{
Stop Gravity.
}
Now we can go more advanced.
A parabola, the formula X^2 - X - 6
now foiling that out we can get (X+2)(X-3), which means the X will cross at +2 and at -3, while the Max Y value is shown in the first formula as -6.
Say we want the character to yet again fall on the hill but not let gravity make him fall through.
if the characterY greater than CharacterX to the power of 2 minus CharacterX minus 6
stop gravity
- Parabolic:
if(CharacterY >= (CharacterX*CharacterX) - CharacterX - 6)
{
don't move.(CharacterX * CharacterX) is equivalent to X pow(float(CharacterX),2), but multiplying them together is faster.
}
Lets say you would only want half of the slope you would simply take the midpoint and do the AABB collision.
- Half Parabolic:
if(CharacterY >= (CharacterX*CharacterX) - CharacterX - 6 && CharacterX > (2+-3)/2)
{
don't move.
}
Now 3d Collision you already know all the formula,
Remember when we made the jump from 1d to 2d how the Y axis extended forever? The same applies with the Z axis.
You can get a simple box using AABB and adding the and for the Z axis.
To get a Cylinder check the distance of the X and Y values but use AABB just for the Z, to rotate it change the X and Y values.
A sphere is just the 3d distance formula.
if you wanted to make a board leaning on a wall you would use the slope formula.
One thing i'm leaving out are angle, which do amazing things, like cones, triangles. You could use the distance formula to give a enemy cell its attack range and then use triangles to check the front of it if it has eyes to give it an extended attack range if it sees you. Other then that you really don't want to be checking a million collision that are high processing.
I also tried to write this with a level that everyone can understand so if there is problems of things that need clarity don't mind posting.
And I will leave you with this which you can type formulas in and it will make a graph or solve the pesky parabolic problems for you.