   Collision detection and response Routine
  Hi.I am having a hard time with the routine of checking collisions and then response to it. What i have is a basic FPS-game with camera as the player.in psuedo-code i would like it to be like this:A -> Check if the player can move where it wants to go. (lastSafePosition to new ocation)If Nothing is hit then moveIf something is hit A Check Intersectionpoint and move player there B Give new velocity vector C Check everything again recursivly with the new velocity vector.But i have a hard time of getting this to work. What i have now is:Vector3 GetClosestPointOnTriangle(mPos,a,b,c);float length_to_closest = (mPos-closest).length;i have a global D3dXVECTOR3 that is my move vector.When pressing 'W' the movevector get a certain velocity and the camera moves.But i don´t know how to break it down so that i check the collision routine BEFORE i move the player?My problem now is if i set MIN_DISTANCE_TO_WALLS = 1, i always end up with smaller distance than 1 due to speed.With my method now i am getting closer to the wall then i should because i lack of intersection point and not checking recursivly.What is the best approach for:A, you want to move hereB, check if you can and to recursive stuffC, based on the result move or don´t moveBecuase of everything i happening in a loop i have trouble with this problem.   
  A very simplistic approach would be to consider the time it takes for each movement.Given a time-slice DT for movement:New Position = old position + velocity*DTIf nothing is hit, done.If something is hitA Check Intersectionpoint and move player there. DT -= time_to_intersectionB Give new velocity vectorC Check everything again recursivly with the new velocity vector and revised DT.;  Hi, and thanks for your answer, this is what i have got now. But i can´t solve the intersection point clearly. My function is recursive and returns a velocity vector after checking all triangles for collision. If hit then slide och check the new position and velocity again. If too many recursions then just stop, set velocity to (0,0,0).But i end up to close and i think this is beacuse i don´t move the sphere close with a small distance to spare from radius before sliding. When i slide i am allready to close to triangle. This is what ive got so far.It is the middle part that is the problem. Any suggestions?//This function clips the velicity and give a new slide velocity if we hit any triangle.D3DXVECTOR3 Collide(const D3DXVECTOR3 &position, const D3DXVECTOR3 &velocity, const float radius, const int recursionDepth){//if we test too many times, we are in a corner or some tricky place, just stop (this is the problem)if (recursionDepth==0) return D3DXVECTOR3(0,0,0);//Find out where the position is after the player moved with the velocity that is given.D3DXVECTOR3 new_pos = position+velocity;//The length to closest point on ANY triangle in the world.float distance = D3DXVec3Length(&(new_pos-ClosestPtPointAllTriangle(new_pos)));/*DbgPrintf(distance);DbgPrintf(recursionDepth);*///if length is over radius of sphere then just return the velocity, the player is free to move. otherwise, send player off with new velocity//that is the "sliding" velocity.if (distance>radius){return velocity;}//We have a hit somewherefloat veryCloseDistance = 0.05f;//DbgPrintf(recursionDepth);float delta = radius - distance;float velocityLength = D3DXVec3Length(&velocity);//cut_percent = how much we must cut velocity so we don´t overshoot our radius.float cut_percent = delta / velocityLength;//Check how much margin we have.float collision_distance = D3DXVec3Length(&(velocity * cut_percent));DbgPrintf(collision_distance);////if player not as close as veryCloseDistance, we have to move player closer without braking radius.if (collision_distance >= veryCloseDistance){//This is the part i don´t solve, i always end up to tight to sliding plane.D3DXVECTOR3 dir = velocity;D3DXVec3Normalize(&dir,&dir);D3DXVECTOR3 newPos = position + dir * (collision_distance - veryCloseDistance);D3DXVECTOR3 closest_point = ClosestPtPointAllTriangle(newPos);D3DXVECTOR3 n = (newPos - closest_point);D3DXVec3Normalize(&n,&n);D3DXVECTOR3 newVelocity = newPos;newVelocity = (n * (D3DXVec3Dot(&newVelocity,&n)));return Collide(newPos,newVelocity,radius,recursionDepth-1);}//v -= n * (v . n)D3DXVECTOR3 closest_point = ClosestPtPointAllTriangle(position);D3DXVECTOR3 n = (position - closest_point);D3DXVec3Normalize(&n,&n);D3DXVECTOR3 newVelocity = velocity;newVelocity -= (n * (D3DXVec3Dot(&velocity,&n)));return Collide(position,newVelocity,radius,recursionDepth-1);}[Edited by - Meshboy on November 19, 2010 5:06:41 PM];  First, it would help tremendously if you would use [code][/code] or [source][/source] tags and format your code. See the faq.I didn't look through your code for the math aspects (it's a bit difficult to look at unformatted code, I'm afraid). However, it appears you didn't read my previous post.D3DXVECTOR3 new_pos = position+velocity;Unless you're setting some small velocity related to the update time, that line tests for a position one second later. Does it help your results if you use a smaller time increment?;  i think i need to know the intersectionpoint before i move my sphere.If it intersencts then set it exactly close to plane minus the radius (and a small value).Either i use ray tracing or some sweep test, but thats my problem.I don´t really know the best way of testing if my sphere is going to intersect and at what point.I think if i can solve that and move my sphere close then my problems would be solved. (of course something else will come up in the end though! =)   