0
\$\begingroup\$

I am new to game programming and still trying to clear up my basics. I am stuck with a problem and unable to resolve on my own knowledge. I am trying to rotate a 3d cube in MonoGame (Similar to XNA) Windows Phone 8 application. The rotation works fine but it has a problem. Following is the scenario

  • Y axis (up/down) rotation works fine. I am able to rotate it as many times as possible and everything works fine.

  • But let's say that the cube was in its default position. I rotate
    my cube 180 degree on y-axis. Now when i rotate my cube on X axis. It rotates in opposite direction. Forexample if i try to move it towards positive x-axis it moves in negative x-axis.

  • If i again move the cube 180 degree in y-axis, the x-axis
    rotations works fine again.

    public void Update(GameTime gameTime) { _userInputHandler.HandleTouchEvents();

    Matrix unrotatedView = Matrix.CreateLookAt(
        cameraPosition, ModelPosition, Vector3.Up);
    
    viewMatrix = Matrix.CreateRotationY(MathHelper.ToRadians(Angle.Y)) *     // right/left rotation
                  Matrix.CreateRotationX(MathHelper.ToRadians(Angle.X)) *   // up/down rotation
                  unrotatedView;
    
    CameraCurPos = Cube.MatrixVectorMultiply(cameraPosition, viewMatrix);
    
    projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Game.GraphicsDevice.Viewport.AspectRatio, 1.0f, 100.0f);
    

    }

Here is the Input handle function

public void HandleTouchEvents()
        {
            TouchCollection tc = TouchPanel.GetState();

            if (tc.Count > 0)
            {
                TouchLocation current = tc[0];
                TouchLocation prev;
                if (current.TryGetPreviousLocation(out prev))
                {
                    Vector3 newAngle = _gameEngine.Angle;
                    //newAngle.X += MathHelper.ToRadians((current.Position.X - prev.Position.X) * TURN_SPEED); // pitch
                    //newAngle.Y += MathHelper.ToRadians((current.Position.Y - prev.Position.Y) * TURN_SPEED); // yaw
                    newAngle.X += MathHelper.ToRadians((current.Position.Y - prev.Position.Y) * TURN_SPEED); // pitch
                    newAngle.Y += MathHelper.ToRadians((current.Position.X - prev.Position.X) * TURN_SPEED); // yaw

                    _gameEngine.Angle = newAngle;                   

                }
}
}

I know the problem is simple but i am unable to resolve it. Thanks in advance.

\$\endgroup\$
1
\$\begingroup\$

Being able to rotate your cube regardless of the axis has to do with the matrix manipulations you apply to the cube.

The best way I have found to keep track of your transformations is to keep a reference location that you create when the object is instantiated. Then you apply your rotations relative to that reference point. In XNA an easy way to do this is using the MathHelper.Clamp() function to keep rotations between 0-2pi.

That way, regardless of how many rotations you perform, you will end up at the correct spot every time. So let's say you are spinning to the left, instead of applying a transformation each frame, you apply refPosition*1deg, then refPosition*2deg etc.

If you're new to 3D transformations there is a ton of reference material to read up on, there is a good article here, and some googling will get you a ton of information.

I've also done similar projects, if you need me to pull up some code I can dig into my past projects

\$\endgroup\$
  • \$\begingroup\$ Thank you for the reply. I still do not understand how to solve the problem. I have looked alot on the internet but due to limited ability to visualize 3D at early stages is creating problems for me. Code will be helpful if you can have it. (I am trying to rotate the cube based on user input on Windows Phone) \$\endgroup\$ – Behroz Sikander Jun 17 '14 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.