This bug/feature has been plaguing me for a while and i want to know the best way to fix it. I'm testing simple hit detection with a wall, like:

 if (Forward button)
        if(Inv.w.z < -49 || Inv.w.z > 49)
            pos.z = 0.0f;
        else if(Inv.w.x < -49 || Inv.w.x > 49)
            pos.z = 0.0f;
            pos.z = +1.0f;

where Inv.w. is the camera positions. Now obviously when i now hit that certain point i can no longer move away from the wall or anywhere in fact. How can i change this code to allow for the camera to be turned away from the wall so therefore i should be allowed to move?

for example, the player hits the wall and i cant move until i turn around or to the side?

I know its something to do with velocity but im pretty new to this so please bare with me if this is easy.


1 Answer 1


Try adding the direction vector to the check. What you really do then is to check where you are going the next frame, and if there is no collision then you can simply move the camera. Otherwise, you don't move the camera, and therefore you won't get stuck!

See it like this:

You are one of those Warhammer table figures, and you have a ruler pointing in the direction you're going to travel. You just place that in front of you every frame, and if that ruler is intersecting anything, you know you can't move there - and therefore don't move the extra length that the ruler holds

Position + ( direction * speed );

SPEED in this case is how far you want to move every frame, since DIRECTION is normalized (or you should be using a normalized vector)

  • \$\begingroup\$ i get what your saying but the trouble im having is implementing it. i have my camera positon (Inv.w) and possibly the speed im travelling at but how can i then say which direction im at from these? and then what do i test it against? sorry if this is simple stuff but im new to this \$\endgroup\$
    – SD1990
    Commented Nov 1, 2012 at 14:29
  • \$\begingroup\$ you probablt have an camera matrix, from that matrix you can extract, left, up, at and position. everything you need :) And lets say you are testing collission in 2d (but in a 3d world) so you only use x,z for testin. you can then simply do bounding box collision tests. pretty much like this. Vector3 camPosition = cam.matrix.pos + cam.matrix.at; if(camPosition.x > object.x && camPosition.x < object.x + object.width) { //you have collission in the x-axis } if(camPosition.z > object.z && camPosition.z < object.z + object.depth) { //you have collission in the z-axis } \$\endgroup\$
    – Tordin
    Commented Nov 1, 2012 at 14:34
  • \$\begingroup\$ sorry for the messy reply. \$\endgroup\$
    – Tordin
    Commented Nov 1, 2012 at 14:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .