Just Ask
Just Ask

Reputation: 339

Moving a 3D object after rotation

I'm having a problem with moving a 3D object after I apply a rotation. Both the move and rotation functions work perfectly on their own. But the problem is when I move an object after a rotation, the object doesn't follow the mouse and goes in weird directions. If anyone can see my flaw, I'd appreciate it. Thanks! Here's my code:

private void Rotate()
{
    double angle;
    bool willangle = Double.TryParse(AngleRot.Text.ToString(), out angle);
    RectangleVisual3D rect = (RectangleVisual3D)LastSelectedObject;
    AxisAngleRotation3D r = new AxisAngleRotation3D(new Vector3D(0, 0, 1), angle);
    RotateTransform3D rot = new RotateTransform3D(r, rect.Origin);
    rect.Transform = Transform3DHelper.CombineTransform(rect.Transform, rot);
    LastSelectedObject = rect as ModelVisual3D;
}
private void MoveObject(MouseEventArgs e)
{
    if (LastSelectedObject is RectangleVisual3D)
    {
        RectangleVisual3D rect = (RectangleVisual3D)LastSelectedObject;
        Point3D? origin = GetPoints(e);
        if (origin == null)
            return;
        rect.Origin = (Point3D)origin;
        LastSelectedObject = rect as ModelVisual3D;
    }
}

Upvotes: 0

Views: 1874

Answers (2)

Alex
Alex

Reputation: 266

Moving your object by setting its origin is generally a bad move. If your helper library ( I don't think Transform3DHelper is .Net? ) is doing matrix math in the basic way, then you're messing it up by setting rect.Origin.

Instead, try finding the distance vector moved and apply that translation matrix.

I'm assuming

Vector2D dist=new Vector2D((oldPosition - newPosition).x, (oldPosition - newPosition).y);

TranslateTransform3D trans = new TranslateTransform3D(dist.x,dist.y,0);

rect.Transform = Transform3DHelper.CombineTransform(rect.Transform, trans);

The other possible error is that CombineTransform should reverse rect.Transform and rot, but I'm not sure if the API is handling that for you. See if an overloaded version of the method allows you to reverse those two.

Upvotes: 0

Gerhard Powell
Gerhard Powell

Reputation: 6175

I hope this help: The order of rotation and move is very important. If you move, then rotate, then it move according to the x,y,z co-ordinates. If you rotate, then move, then it will move according to the rotations co-ordinates.

Upvotes: 3

Related Questions