Welcome to part three of our Special Edition tutorials. In this tutorial we will cover collision detect. In this tutorial, and in future tutorials we will not be posting the entire tutorials source code. We will simply discuss which functions are changed in our rpg.c and rpg.h files, and any new files that we are adding. You can download the source code by clicking the link on the bottom of our page.

I wanted to note that we already know that you cannot actually move in the direction you are facing, and of the Direct3D problems. These are relatively small changes that need to be made, however I did not have time(okay I forgot until after I already had this nearly finished) to add them into this tutorial. They will be fixed for tutorial 4.

First off open up your rpg.h file. Because of the relatively small size of this file, I am simply going to post the entire file up here. Changes are in Yellow.

geEngine *Engine = NULL;
geDriver_System *DrvSys = NULL;
geDriver *Driver = NULL;
geDriver_Mode *Mode = NULL;
geWorld *World = NULL;
geCamera *Camera = NULL;
geRect Rect;
char *modename = NULL, *drvname = NULL;
MSG                 Msg;
geXForm3d Xform;
geVec3d Angles;
geVec3d oldpos;                  //old and new pos are temporary values
geVec3d newpos;
geVec3d            In;    
geVec3d            Mins;         //this is for collision detect
geVec3d            Maxs;        //they set your minimum and maximum size.

geXForm3d            ViewXForm;                        
int run, Width, Height,collision;
GE_Collision Collision;

BOOL    Result;            
int        CWidth =640;    //Our Clients Width And Height, and driver(loaded from prefs.ini)
int        CHeight=480;
char    ourdriver = 'G';
geTextLib_Texture *Black = NULL;

int        forwardspeed = 0; //speed we are moving forward/backward

// a few player specific variables
int        playerheight =140; //how tall are we
int        playerspeed =8; //how fast are we


Most of the functions are self explanatory(with the comments at least), so I will not touch up on the matter too much. On to rpg.c.

At the very beginning of the file, where we are setting up our subroutines you need to add a new function by the name of Setup, as shown below:

#include "include\Genesis.h"

void Setup(void);
void MoveCamera(void);
void LoadPrefs(void);

We are moving several player specific functions to this location. The reason being that later in our work we will be having multiple character races, and will need to adjust the height, width, etc of our players. This is just a step in that direction.

Next go to our MoveCamera routine. Edit it to resemble the code below.

void MoveCamera(void)
{
POINT pos;
geVec3d Pos;

GetCursorPos(&pos);
ScreenToClient(mainwindowhandle, &pos);
// are we in spin mode(RMB down) then check for angles
if (spin ==1)
{

    // checks where the pointer is at(left, right, up, or down)
    // notice that we adjust it according to our height and width, that way
    // it is proper in every resolution;
    if (pos.x > ((CWidth/2) + 50)) {    // is it to the left?
    Angles.Y = Angles.Y-(geFloat)((pos.x-(CWidth/2))*.00005);//if so spin left
    }
    else if (pos.x < ((CWidth/2) - 50)) {//is it to the right?
    Angles.Y = Angles.Y+(geFloat)(((CWidth/2)+pos.x)*.00005);//if so spin right
    }

    if (pos.y > ((CHeight/2) + 50)) {//is it to the top?
    Angles.X = Angles.X-(geFloat)((pos.y-(CHeight/2))*.00005);//if so look up

    }
    if (pos.y < ((CHeight/2) - 50)) {
    Angles.X = Angles.X+(geFloat)(((CHeight/2)+pos.y)*.00005);//if so look down
    }
}
else // we are not in spin mode
{
    if (pos.y > ((CHeight/2) + 50)) //is it to the top?
        forwardspeed = -playerspeed;     // then increase speed
    if (pos.y < ((CHeight/2) - 50)) //is it to the bottom?
        forwardspeed = playerspeed;    // then decrease speed
}
//make sure we arent looking too far up or down
if (Angles.X>.5)
    Angles.X = .5;
if (Angles.X<-.5)
    Angles.X =-.5;

// Execute any movement that may have taken place.

    oldpos = Xform.Translation;    //old position
        newpos = Xform.Translation;    //new position

        geXForm3d_GetIn(&Xform,&In); //get forward vector
        geVec3d_MA(&oldpos,forwardspeed,&In,&newpos);//Move

        Result=geWorld_Collision(World,&Mins,&Maxs,&oldpos,&newpos,GE_COLLIDE_MODELS,0xffffffff,&Collision); //checks for collision

        if(Result==1)//Your new position collides with something
        newpos=Collision.Impact;//set new position to point of col.

        Xform.Translation =newpos;        


// Now Change our view for direction we are facing.

ViewXForm = Xform; // Copy our Xform into ViewXForm(so we can deform it to
// where our player is looking etc)

Pos = ViewXForm.Translation;

// Clear the matrix
geXForm3d_SetIdentity(&ViewXForm);

// Rotate then translate.
geXForm3d_RotateZ(&ViewXForm, Angles.Z);
geXForm3d_RotateX(&ViewXForm, Angles.X);
geXForm3d_RotateY(&ViewXForm, Angles.Y);
   
geXForm3d_Translate(&ViewXForm, Pos.X, Pos.Y, Pos.Z);

ViewXForm.Translation.Y += playerheight; // Adjust to our players eye level
   
geCamera_SetXForm(Camera, &ViewXForm);
}

Lastly we must create our new Setup routine. At the end of your file add the following bit of code:

void Setup(void)
{
// Setup will become very important later as we add different player races
   Mins.X = -20.0f;    //Mins/Maxs set up the bounding box around our player which is used for collision
    Mins.Y = -20.0f;   //Detect purposes. It tells it when we are colliding.
    Mins.Z = -20.0f;
    Maxs.X = 20.0f;
    Maxs.Y = 20.0f;
    Maxs.Z = 20.0f;
// We will set this values up here only for an easier transition later.
    playerheight =120;    // this is actually our players eye level, and not real height
    playerspeed =8;
}


As always you can send comments or suggestions for this tutorial to erasmushurt@earthlink.net.

[Hyperlinkleisten stehen in diesem Web nicht zur Verfügung]