Converting a 2d top down map to a virtual 360 degree world
- 3dtut3a.zip
- updated version - ged files
- (57.78 KiB) Downloaded 404 times
UPDATED VERSION
EDITED>>
This is the first stage of building a 3d engine.
The general idea is to use a normal top down 2d map for the game engine, so all collisions/ movements / fighting etc etc all happen in the 2d world... this solves most maths problems as all activity happens in 2d, so only standard 2d programming skills are required. ( and routines like moveto_avoid work well.)
then every thing is transferred to a 360 degree playing surface where the player ALWAYS looks UP. (towards top of screen.)
now this "virtual 360 world" can easily be converted over to FULL 3D
you will now notice that all x coordinates are now to the left or right of player, and all y coordinates are to the top or bottom of player, therefore there is NO complicated maths required to convert to 3D
eg: if y is less then player it must be infront of him. this cannot be done in 2d if the player can turn around and all sorts of trigonometry would normally be required, so this step was originally meant simply to solve this problem.
However it also created a pretty neat engine for RPG's
Also note that the "360 world" is created on the fly by the engine and is "rotated" using the same method used in the "compass" example in tutorial 1.
NOTE: the zdepthing is only rudimentary at the moment and there are no enemies or collisions yet.
I wanted each stage of the tutorial to be easy to follow.
The keys are the same as in the previous post. use the compass to find your way
HOW IT WORKS SO FAR
Firstly, i have built a simple 2d map, but allowed the player to move in 360 degrees.
At the moment the map only has "obstacles", things like trees wall etc that should stop the player and enemies from moving, so only simple collision rules are required.
In order to apply the same collision rule to all obstacles i have made 1 single characters called "obstacles" and added several different animations to it. this way the same rules apply to all objects in this "class"
to create the "virtual 3d world" all "obstacles when "created" pass their x,y coordinates to an array
on create
virtualx[cloneindex]=x;
virtualy[cloneindex]=y;
// store x and y as virtual positions
Then during the "draw" process of each 2d object, all the coordinates are updated according to where the player currently is
virtualx[cloneindex] = dist*sin(degtorad(ANG-direction_angle))+player.x;
virtualy[cloneindex] = dist*cos(degtorad(ANG-direction_angle))+player.y;
//turn virtual x and y according to players direction
This does not effect anything on the original 2D map, so all game calculations can still be applied to the 2D map.
The next step is simply to convert the "obstacles" current animation to a 360 degree world and store it in an array ready to build the 360 world - this means, if the 2d obstacle is a "rock" then the 3d obstacles should also be a "rock" but in 3D
I do this by taking the existing name of the current animation in 2D and storing it in a string array ready to make the 360 world
I simply ADD the string "360" to the existing animation...
NOTE: this occurs during the DRAW routine of every obstacle clone. It could happen during the create process to speed up the game , but if any "obstacles" change animations during the game they would not be passed on.. for example if a "gate" opens , or a wall collapses..(this will be more use when we have enemies that die or change animations)
int persp=360; // this is the string to add to the end of the current animations name
sprintf(obstacles.text,"%s%d", getAnimName(animindex), persp); //gets the current animations name and adds "360"
strcpy(vscenerytype[cloneindex],obstacles.text); // copies the new name to a string array placeholder for that particular
clone.
NOTE: this means the animations in the 360 degree clones must all match the original names plus 360
--in the 2d world animation=rock
-- in 360 world animation =rock360
I could have done this simply with animindex but this means I have to make sure every animation is loaded in the SAME order in both worlds. But, this way they can be loaded in any order ( useful if you delete one and recreate it later which messes up the animindex)
Lastly, I create a single 360 degree obstacle Actor, and load it with all the xxxx360 animations ( remove 'create' at start up as you don't need this actor)
then in the create routine of the all the 2d sprites I make every "obstcale" create a seperate matching version of the 360 clone - one for each, and park them off screen.
CreateActor("obstacles360", "icon", "(none)", "(none)", 9990, 9990, true);
from here the game starts and all the 360 clones update themselves to the virtual coordinates, and change there individual animations to match the appropriate one using the premade "tables" of arrays. (the x,y, and animation string arrays), and then the 360 world is created. ( and updated during runtime automatically.)
Thats it for this stage. I hope that is not to long winded , if it is let me know and I will shorten it. or if something doesn't make sense.
the following tutorials will be much shorter as most of the main concepts have now been covered.
also let me know if this is boring people, or you want me to continue. I know some of this could be done different ways so any advice is welcome.
the next step is zdepthing the on screen actors, and walking under and through objects such as through doors and under big trees (things that are higher then ground level)
cheers
feral
(feralX3d)