0
\$\begingroup\$

This code should follow the game Object in every step, but it does not. In the beginning we can rotate the object wherever we want and it works, but after moving it some the camera starts doing weird moves. I don't understand it because the camera position should always be along the line of object position + target

GameObject::GameObject()
{
    type = 1;
    speed = 1.0f;
    position = { 0,0,0 };
    direction = {-1,0,0};
    up = { 0,1,0 };
    D3DXMatrixIdentity(&rotation);
    graphic_model = NULL;

}

GameObject::GameObject(LPDIRECT3DDEVICE9 dev, Model *gModel, D3DXVECTOR3 p , D3DXVECTOR3 d,float s,float t)
{
    type = t;
    speed = s;
    position = p;
    direction = d;
    up = { 0,1,0 };
    D3DXMatrixIdentity(&rotation);
    D3DXMatrixIdentity(&scalem);
    
    graphic_model = gModel;
    
}


void GameObject::createMatrix()
{
    D3DXMATRIX translation;
    D3DXMatrixIdentity(&translation);

    D3DXMatrixTranslation(&translation, position.x, position.y, position.z);
    model = (scalem*rotation*translation);
    //D3DXMatrixMultiply(&model, &rotation, &translation);
}
void GameObject::translate(float x, float y, float z)
{
    position.x = x;
    position.y = y;
    position.z = z;

}

void GameObject::scale(float sx,float sy, float sz)
{
    D3DXMATRIX sca;
    D3DXMatrixScaling(&sca, sx, sy, sz);
    scalem *= sca;
}
void GameObject::move() {
    D3DXVECTOR3 f;
    D3DXVec3Normalize(&f, &direction);
    position += (f*0.05);
    
}

D3DXMATRIX GameObject::getModelTransform()
{
    createMatrix();
    return model;
}

int GameObject::redirect(D3DXVECTOR3 target) {
    D3DXVECTOR3 P1, P2, L, T, tempV, axisOfRotation;
    float angle;
    P1 = position; //The D3DXVECTOR3 location of the object
    P2 = target; //The D3DXVECTOR3 location of the target

                    //Calculate the normalized vector from the object to the target
    D3DXVec3Normalize(&L, &direction);
    D3DXVec3Subtract(&tempV, &P2, &P1);
    D3DXVec3Normalize(&T, &tempV);

    //Take the cross product of T and L to compute the axis rotation
    D3DXVec3Cross(&axisOfRotation, &L, &T);
    if(D3DXVec3Length(&tempV)<0.5)
        return 0;
    float d = D3DXVec3Dot(&L, &T);
    if (d > 1)
        d = 1;
    if (d < -1)
        d = -1;
    //Calculate the angle of rotation
    angle = acosf(d);
    
    //Now create the rotation matrix
    D3DXMATRIX matRotate;
    D3DXMatrixRotationAxis(&matRotate, &axisOfRotation, angle*0.1f);

    D3DXVec3TransformNormal(&direction, &direction, &matRotate);
    D3DXVec3TransformNormal(&up, &up, &matRotate);
    //direction = direction - position;

    D3DXVec3Normalize(&direction, &direction);
    D3DXVec3Normalize(&up, &up);

    rotation *= matRotate;
    return 1;

}


InputHandler::InputHandler()
{
}

void InputHandler::handleInput(Player *p, ThirdPersonCamera *camera)
{
    /*if (isPressed(BUTTON_X)) buttonX_->execute();
    else if (isPressed(BUTTON_Y)) buttonY_->execute();
    else if (isPressed(BUTTON_A)) buttonA_->execute();
    else if (isPressed(BUTTON_B)) buttonB_->execute();*/

    DIJOYSTATE2 state;
    Keyboard.poll(&state);
    Mouse.poll(&state);


    xoffset = state.lX * sensetivity;
    yoffset = state.lY * sensetivity;

    camera->adjustCamera(p, xoffset, yoffset, state.lZ);
    p->redirect(camera->getTLAT());
    lastMouseX = state.lX;
    lastMouseY = state.lY;


    //W is pressed
    if (state.rgbButtons[0] & 0x80)
    {

        p->move();
        //camera->kamera_fahrt(xoffset, yoffset, p->getSpeed(), state.lZ);
    }
    //S is pressed
    if (state.rgbButtons[1] & 0x80)
    {

    }
    //A is pressed
    if (state.rgbButtons[2] & 0x80)
    {

    }
    // D is pressed
    if (state.rgbButtons[3] & 0x80)
    {

    }

    
    //p->reachTarget(camera->getTarget());


}


ThirdPersonCamera::ThirdPersonCamera(GameObject * o, int Screen_Height, int Screen_Width,float clip, float min, float max)
{
    this->min = -min;
    this->max = -max;

    here_is_up = { 0,1,0 };
    there_i_look_at = o->getDirection();
    target = there_i_look_at;
    here_i_am = o->getPosition() + this->min * (there_i_look_at);
    here_i_am.y = 0;


    D3DXVec3Normalize(&target, &target);

    D3DXMatrixPerspectiveFovLH(&projection, D3DX_PI / 2, Screen_Width / Screen_Height, 0.1f, clip);
    CreateViewMatrix();
}

void ThirdPersonCamera::adjustCamera(GameObject * o,float mouseX,float mouseY,float scroll)
{
    scroll = clampScroll(scroll);
    here_is_up = o->getUp();
    kamera_schwenken(mouseX, mouseY);
    there_i_look_at = o->getPosition() + target;
    here_i_am = o->getPosition() + scroll * target;
    CreateViewMatrix();
    
}

void ThirdPersonCamera::CreateViewMatrix()
{
    D3DXMatrixLookAtLH(&view, &here_i_am, &target, &here_is_up);
}


ThirdPersonCamera::~ThirdPersonCamera()
{
}


float ThirdPersonCamera::clampScroll(float s)
{
    if (s > min)
        return min;
    if (s < max)
        return max;
    return -s;
}

void ThirdPersonCamera::kamera_schwenken(float seite, float hoch)
{
    D3DXVECTOR3 norm, blick;
    D3DXMATRIX rotation;



    /*if (target.x*target.x + target.z*target.z < 0.005)
    {
    if (((target.y > 0) && (hoch > 0)) || ((target.y < 0) && (hoch < 0)))
    {
    hoch = 0;
    }
    }*/

    D3DXVec3Cross(&norm, &target, &here_is_up);

    D3DXMatrixRotationAxis(&rotation, &norm, hoch);

    D3DXVec3TransformNormal(&target, &target, &rotation);


    D3DXMatrixRotationAxis(&rotation, &here_is_up, seite);

    D3DXVec3TransformNormal(&target, &target, &rotation);

    D3DXVec3Normalize(&target, &target);

}
void ThirdPersonCamera::kamera_bewegen(float seit, float hoch, float vor)
{
    D3DXVECTOR3 v, s, h, t;
    v = there_i_look_at - here_i_am;
    v.y = 0.0f;
    D3DXVec3Normalize(&v, &v);
    s = D3DXVECTOR3(-v.z, 0, v.x);
    h = D3DXVECTOR3(0, 1, 0);
    t = seit*s + hoch*h + vor*v;
    here_i_am += t;
    there_i_look_at += t;
}

void ThirdPersonCamera::kamera_fahrt(float seit, float hoch, float speed, float scroll)
{
    D3DXVECTOR3 delta;
    kamera_schwenken(seit, hoch);
    delta = there_i_look_at - here_i_am;
    D3DXVec3Normalize(&delta, &delta);
    delta = 1*delta;
    here_i_am += delta;
    there_i_look_at += delta;
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Use there_i_look instead of target:

void ThirdPersonCamera::CreateViewMatrix()
{
    D3DXMatrixLookAtLH(&view, &here_i_am, &there_i_look, &here_is_up);
}
\$\endgroup\$

You must log in to answer this question.

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