Drag object with mouse - lock axis

Started by
1 comment, last by Buckeye 9 years, 3 months ago

I have created object draging by mouse. I am using this code to calculate dx, dy in screen space

pos = object position in world space


    float objDistCam = pos.Distance(this->camera->GetPosition());

    MyMath::Vector4 P = MyMath::Vector4(0, 0, objDistCam, 1.0f);
    MyMath::Vector4 S = MyMath::Vector4::Transform(P, this->camera->GetCameraMatrices()->projectionMatrix);
    S *= 1.0f / S.W;


    MyMath::Vector4  S2 = S;
    S2.X = S2.X + (((float)-pointBasedControl->GetChangeX()) / (this->camera->GetViewport()->Width * 0.5f));
    S2.Y = S2.Y + (((float)pointBasedControl->GetChangeY()) / (this->camera->GetViewport()->Height * 0.5f));



    MyMath::Matrix4x4 inv = MyMath::Matrix4x4::Invert(this->camera->GetCameraMatrices()->projectionMatrix);
    MyMath::Vector4 P2 = MyMath::Vector4::Transform(S2, inv);
    P2 *= 1.0f / P2.W;


    dx = P.X - P2.X;
    dy = P.Y - P2.Y;

Final object position is calculated like this


pos += camera_right * dx + camera_up * dy;

Now, this works.

If I lock single axis, lets say X (pos.X += camera_right.X * dx + camera_up.X * dy), it wors as well. Problem is, if axis system is not aligned with world axis and object is rotated. Then this leads to object moving in "world" X axis, not in "object" one. How to correct this?

Advertisement

Solved...

Solved...

You post a problem, asking others to take the time to help you find a solution, but you're not willing to take the time to help others by posting your solution?? Phooey.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement