Original Post
Hi, I've just started adding collision detection to my engine using sphere to sphere to begin with and I've managed to get it working without a problem. However the problem I am having is I don't know what to do to the camera when it does collide with something. Here is the code that deals with the movement of the camera Movement: Camera controls: As you can see from the following code, I have tried inverting the movement vector, which just gave strange behaviour. At the moment as soon as the camera collides with something it's movement vector is locked to 0,0,0 so you basically just get stuck. How can I make it so that if the camera is moving does and collide with something, it can still move in all the directions excluding the direction that is blocked? Cheers! [Edited by - Slyfox on March 3, 2008 11:59:35 AM]
if(input->keyDown(DIK_W))
{
D3DXVECTOR3 newMove = D3DXVECTOR3 ( -(cos(heading)), 0.0f, (sin(heading)) );
D3DXVec3Add(&moveDir, &moveDir, &newMove);
}
// Normalize the vector so that it's unit length
D3DXVec3Normalize(&moveDir, &moveDir);
// Scale the movement vector so that it's working off the player's speed
D3DXVec3Scale(&moveDir, &moveDir, playerSpeed);
if (cameraCollision == true)
{
//moveDir.x = (-moveDir.x);
//moveDir.z = (-moveDir.y);
//moveDir.y = (-moveDir.z);
moveDir = D3DXVECTOR3(0.0f,0.0f,0.0f);
}
// Add the movement vector to the current position
D3DXVec3Add(&position, &position, &moveDir);
D3DXVECTOR3 lookAtPos;
// This combines the position and the lookAt position, so that the lookAt is relative to the camera position
D3DXVec3Add(&lookAtPos, &position, &lookAt);
camera->setPosition(position);
camera->setLookAt(lookAtPos);
// Mouse movement
{
// (PI / 180.0f) converts from degrees to radians
heading += (input->getMouseMovingX() * (D3DX_PI / 180.0f)/2);
elevation += (input->getMouseMovingY() * (D3DX_PI / 180.0f)/2);
// Eliminate gimble lock
if (elevation < 0.5f)
elevation = 0.5f;
else if (elevation > 2.5f)
elevation = 2.5f;
// Construct the lookAt vector from the heading angle
lookAt.x = -((sin(elevation))* (cos(heading)));
lookAt.z = (sin(elevation))* (sin(heading));
lookAt.y = cos(elevation);
// update the lookAt position because the player may not be moving at the moment
D3DXVec3Add(&lookAtPos, &position, &lookAt);
camera->setLookAt(lookAtPos);
if (cameraCollision == true)
{
//moveDir.x = (-moveDir.x);
//moveDir.z = (-moveDir.y);
//moveDir.y = (-moveDir.z);
moveDir = D3DXVECTOR3(0.0f,0.0f,0.0f);
}