3d Block Collision (Diagonal Blocks)

Started by
0 comments, last by kiwibonga 13 years, 4 months ago
I am making a 3d game that I want to have a virtual lego feel with extras.

I have blocks instead of lego pieces and also want a retro feel.

The blocks are snapped the the nearest int making collision simpler or so I thought.

The collision detection works perfect when the player collides with the side of a block, however the problem is diagonal blocks.
Working:   [ ]--X[ ]   [ ]Not Working:[ ]     --->   [ ]/       /[ ]----     [ ]

My original source:
if (speed!=0)    {        float xNew=x-speed*(cos((zRot)*3.14/180));        float yNew=y+speed*(sin((zRot)*3.14/180));        vertex position[3];        position[0].x=xNew;        position[0].y=yNew;        position[0].z=z+.5;                        if (Map->checkCollision(position[0])==false)        {            xNew=x-speed*(cos((zRot)*3.14/180));            yNew=y+speed*(sin((zRot)*3.14/180));            x=xNew;            y=yNew;        }    }


My next failed attempt:
if (speed!=0)    {        float xNew=x-speed*(cos((zRot+45)*3.14/180));        float yNew=y+speed*(sin((zRot+45)*3.14/180));        vertex position[3];        position[0].x=xNew;        position[0].y=yNew;        position[0].z=z+.5;           xNew=x-speed*(cos((zRot-45)*3.14/180));        yNew=y+speed*(sin((zRot-45)*3.14/180));        position[1].x=xNew;        position[1].y=yNew;        position[1].z=z+1.5;                if (Map->checkCollision(position[0])==false && Map->checkCollision(position[1])==false)        {            xNew=x-speed*(cos((zRot)*3.14/180));            yNew=y+speed*(sin((zRot)*3.14/180));            x=xNew;            y=yNew;        }    }


Function used in above source:
bool chunk::checkCollision(vertex Point){    for(int a=0;a<position;a++)    {        if (        grid[a].x <= Point.x && grid[a].x+1 >= Point.x &&        grid[a].y <= Point.y && grid[a].y+1 >= Point.y &&        grid[a].z <= Point.z && grid[a].z+1 >= Point.z        )        {            return true;        }    }    return false;}

^ About Above ^
grid array, is an array that contains the x,y and z of the blocks in the game, where the max amount of blocks are 24576 block.

How this works is:
It figures out where the player will be next step
That point is then checked to see if it is inside a block.
If so then Speed = 0
else let the player move

Does anyone know how I can fix this and get more speed?

Thanks in advance
If this post was helpful please +1 or like it !

Webstrand
Advertisement
Simple: consider each component of the speed individually, like in a 2D top-view tile based game.

Check for a collision down the x axis first (at (currentposition.x+speed.x, currentposition.y)) -- if you detect a collision, "snap" the player against the wall in the x position, and set his x speed to 0.

Then check for a collision down the y axis (at (currentposition.x, currentposition.y + speed.y)) and do the same thing as with the x direction check.

The result is that the player will "slide" along walls rather than get stopped completely, which is a nice bonus.

You'll have to look out for "corner skipping" though -- first moving in the x direction then in the y direction can make the player "go around" an obstacle if he's moving fast enough; a problem that wouldn't have happened if you checked y first then x. So either do a double check (first x, then y, followed by first y, then x, and favor the one that collided), or check the surrounding environment to figure out the order in which to evaluate the directions.

This topic is closed to new replies.

Advertisement