i'm walking through walls!

Started by palody, May 23, 2007, 23:08:27

Previous topic - Next topic

palody

how can i make it so players in my game won't walk through my walls? my walls are just quads... 22 total. they're coordinates are either 1,2, or 3 and are just scaled...

i tried this:

        // move forward in current direction
        if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
            System.out.println("Camera Position: {" + cameraPos[0] + ", " + cameraPos[1] + ", " + cameraPos[2] + "}");
            if(cameraPos[0] <=-1.0 || cameraPos[2] <=-1.0)
            {   System.out.println("stop");
            if(cameraPos[0]<=-1.0)
                  {    cameraPos[0] = -0.99f;
            }
            if(cameraPos[2] <= -1.0)
            {   cameraPos[2] = -0.99f;
            }
         }
              else
            {   cameraPos[0] -= (float) Math.sin(cameraRotation * piover180) * .01f;
                  cameraPos[2] -= (float) Math.cos(cameraRotation * piover180) * .01f;
         }
        }
        // move backward in current direction
        if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
            System.out.println("Camera Position: {" + cameraPos[0] + ", " + cameraPos[1] + ", " + cameraPos[2] + "}");
            if(cameraPos[0] > 1.0 || cameraPos[2] > 1.0)
            {   System.out.println("stop");
            if(cameraPos[0] > 1.0)
               {   cameraPos[0] = 0.99f;
            }
            if(cameraPos[2]> 1.0)
            {   cameraPos[2] = 0.99f;
            }
         }
            else
            {   cameraPos[0] += (float) Math.sin(cameraRotation * piover180) * .01f;
               cameraPos[2] += (float) Math.cos(cameraRotation * piover180) * .01f;
         }
        }

just to see if it works. i kind of does.. but when i start pressing up-left or up-right, down-left, down-right buttons.. it doesn't go inside the if statement anymore.. it shouldn't go past 1.0 or -1.0 but when i combine the buttons it goes past it.......

what am i doing wrong? or is there a better way of implementing that? coz i have no idea how.

bobjob

a comment on your current code. with collision testing i find it easy to add momevent into a dummy position, without any checks
then after all key presses have been done, then run a collision test in a seperate function against the dummy position, then the dummy position is altered (if it needs to be). then make the real positon equal to the dummy position.


some other collision ideas:

if you want to be able to collide with any wall on any angle, look into triangle collision testing (if a point is within a triangle)
working with quads will be annoying. (worth the time, my personal favorite)




here are some ideas to implement effient collision techniques best for indoor collision (not a terrain engine):

if there are no diagnal walls (like original wolvenstien)
An easy way of testing collsion is imagining it from top down.
break the map into cubes, and flat walls can only be placed between the cubes
then run a check depending on what cube the character is in


if  you want diagnal walls and there are no ramps in your map (so like original doom)
An easy way of testing collsion is imagining it from top down.

Making a sector object:
Define a sector as a number of ZX dot co-ordinates in order (they will be joined together to create walls)
store the highest and lowest Z and X points as the  primary collision test conditions
give the sector a max height and minimum height. (depending on how high or low you want the area to be)

Checking for a collision with the sector object:
first check the ZX position of the player against the sector low and high Points (primary collision valuess) to determine what sector you may be in.
then run a line equation check between the wall points in order eg. 1 - 2, 2 - 3, 3 - 4, 4 - 1 ( or any number of points), against the players ZX postion.
then check the Y position against the sectors maxHeight and minHeight to determine if the height of the character position is possible, if not correct(roof or flaw collision): if lower than minHeight, y = minHeight, if higher than maxHeight y = maxHeight.
(this is easy to understand if you ever used a old game cadding program like duke Build or doomcad)
note: it is possible to have ramps using this method(like Duke3D), instead of the minHeight and maxHeight of a sector you can use a line equation, but i would not recommend it as it would be annoying to have to perfect each sector to get it just the way you want.