Translating multiple objects in GUI based on average position?

Posted by user1423893 on Game Development See other posts from Game Development or by user1423893
Published on 2012-08-31T16:05:30Z Indexed on 2012/08/31 21:50 UTC
Read the original article Hit count: 188

Filed under:
|

I use this method to move a single object in 3D space, it accounts for a local offset based on where the cursor ray hits the widget and the center of the widget.

        var cursorRay = cursor.Ray;

        Vector3 goalPosition = translationWidget.GoalPosition;
        Vector3 position = cursorRay.Origin + cursorRay.Direction * grabDistance;

        // Constrain object movement based on selected axis
        switch (translationWidget.AxisSelected)
        {
            case AxisSelected.All: goalPosition = position; break;
            case AxisSelected.None: break;
            case AxisSelected.X: goalPosition.X = position.X; break;
            case AxisSelected.Y: goalPosition.Y = position.Y; break;
            case AxisSelected.Z: goalPosition.Z = position.Z; break;
        }

        translationWidget.GoalPosition = goalPosition;

        Vector3 p = goalPosition - translationWidget.LocalOffset;

        objectSelected.Position = p;

I would like to move multiple objects based on the same principle and using a widget which is located at the average position of all the objects currently selected.

I thought that I would have to translate each object based on their offset from the average point and then include the local offset.

        var cursorRay = cursor.Ray;

        Vector3 goalPosition = translationWidget.GoalPosition;
        Vector3 position = cursorRay.Origin + cursorRay.Direction * grabDistance;

        // Constrain object movement based on selected axis
        switch (translationWidget.AxisSelected)
        {
            case AxisSelected.All: goalPosition = position; break;
            case AxisSelected.None: break;
            case AxisSelected.X: goalPosition.X = position.X; break;
            case AxisSelected.Y: goalPosition.Y = position.Y; break;
            case AxisSelected.Z: goalPosition.Z = position.Z; break;
        }

        translationWidget.GoalPosition = goalPosition;

        Vector3 p = goalPosition - translationWidget.LocalOffset;

        int numSelectedObjects = objectSelectedList.Count;

        for (int i = 0; i < numSelectedObjects; ++i)
        {
            objectSelectedList[i].Position = 
        (objectSelectedList[i].Position - translationWidget.Position) + p;
    }

This doesn't work as the object starts shaking, which I think is because I haven't accounted for the new offset correctly. Where have I gone wrong?

© Game Development or respective owner

Related posts about c#

Related posts about transformation