XNA Creators Club Online
Page 1 of 1 (14 items)
Sort Posts: Previous Next

rotatating and moving the player in facing direction

Last post 11/21/2009 11:12 AM by twiggy024. 13 replies.
  • 10/9/2009 5:40 AM

    rotatating and moving the player in facing direction

    I have a player object that want rotate from side to side and when the user presses up on the thumbstick the player moves in direction its facing. I have allready tryed applying this method from two of the tutorial from the education section but alas with no sucess

    the way my object is coded is as follows

    //GameObject.cs
    public Model model = null;
            
            public Vector3 position = Vector3.Zero;
            public Vector3 rotation = Vector3.Zero;
            public float scale = 1.0f;
            public Vector3 velocity = Vector3.Zero;
            public float speed = 2.0f;
    //

    //Game1.cs
    GameObject player = new GameObject();
    player.model = Content.Load<Model>(
                    "models\\player01");
    DrawGameObject(player);
    //

    If there is something i can change to way the object is loded or anything i can do that would be simple to implement then please let me know, ive done this sort of thing in C++ and OGRE using

    quaternion angles and this is the code is used there:

    Vector3 mInitFacing = Vector3::UNIT_X;
    Vector3 currentFacing = Player->getOrientation() * mInitFacing;                          
    Quaternion quat = currentFacing.getRotationTo(mInitFacing);

    then

    Quaternion quat = currentFacing.getRotationTo(Vector3(-playerX,0,0));
    Player->setOrientation(quat);

    if someone could please help either in a way eother with my model as it is or even converting that code to XNA it would be a great help!!
  • 10/9/2009 8:33 AM In reply to

    Re: rotatating and moving the player in facing direction

    Hi! Well, first of all, I'd define a Vector3 representing the forward direction for every GameObject, that way you could simply move the object adding the scaled forward vector to it's position. For the orientation problem, simply transform the quaternion representing the rotation in the update method:

    Quaternion r = Quaternion.CreateFromAxisAngle(Vector3.Up, -MathHelper.Pi / 180);

    using whichever axis vector and angle you want. Then, you could:

    1.- Tranform the forward vector after computing the new rotation quaternion: q = r * q * r;
    2.- Whenever you need the forward vector, use an accessor method that returns the initial forward vector transformed by the rotation quaternion. With this approach, the forward vector should not be needed if you define a default forward vector, say (0, 0, -1), for every GameObject.

    Hope this helps!
  • 10/9/2009 11:46 AM In reply to

    Re: rotatating and moving the player in facing direction

    Hi cheers for the help but im still not understanding you, if you dish out a bit more sytax/sudo code that would be grand
  • 10/9/2009 4:29 PM In reply to

    Re: rotatating and moving the player in facing direction

    First, store your rotation as a Quaternion, not a Vector3.

    This is a function from my custom game model object that does what I think you are talking about:

    public void lookat(Vector3 target, float Slerp) 
            { 
                Matrix matrixModel = Matrix.CreateFromQuaternion(modelRotation); 
                Matrix matrixLook = Matrix.CreateWorld(modelPosition, (target - modelPosition), matrixModel.Up); 
                Quaternion finalRotation = Quaternion.CreateFromRotationMatrix(matrixLook); 
                Quaternion.Slerp(ref modelRotation, ref finalRotation, Slerp, out modelRotation); 
            } 


    modelRotation is the Quaternion I am using to store the models rotation (obviously). modelPostition is a Vector3 that I am using to store the models position.

    "target" is a Vector3 that is the location that I want to point the model at. Slerp represents the interpolation that I want to use (i.e., in my game I don't want my spaceships to immediately point at a target, I want them to turn slowly towards the target... if you want an instant turn, just make Slerp be 1.0f).

    I think there might be some inefficient garbage collection from creating a new matrixModel and matrixLook every update, but I am not really at the optimization stage and my game runs blindingly fast without the optimization anyway, so someone else can probably give better advice on that.
  • 10/9/2009 6:45 PM In reply to

    Re: rotatating and moving the player in facing direction

    Quaternions are annoying
    I think you should keep it in a vector3.  Here is what you should do:

    First, create a Vector3 representing how much you want it to move:
    Vector3 movement = new Vector3(0f, 0f, 5f); // 5f is the amount of movement.  This means that if there was no rotation, it would move 5f across the z-axis

    Then, create a Matrix for the rotation:
    Matrix RotationMatrix = Matrix.CreateRotationX(Rotation.X) * Matrix.CreateRotationY(Rotation.Y) * Matrix.CreateRotationZ(Rotation.Z);

    This assumes that your rotation is stored in a Vector3 called Rotation, and creates a rotation matrix based off of it.

    Finally, transform your movement vector by the RotationMatrix:

    movement = Vector3.Transform(movement, RotationMatrix);

    Then, you add the resulting Vector3 (movement) to the position (this assumes that your Vector3 representing your position is stored in "position")
    position += movement;

    So, here's the code all in one block:
    Vector3 Movement = new Vector3(0f, 0f, 5f); // The amount of movement forward  
    // Create the rotation matrix  
    Matrix RotationMatrix = Matrix.CreateRotationX(Rotation.X) *  
                            Matrix.CreateRotationY(Rotation.Y) *  
                            Matrix.CreateRotationZ(Rotation.Z);  
    // Apply it to our vector  
    Movement = Vector3.Transform(Movement, RotationMatrix);  
    // And add the resulting vector to our position  
    Position += Movement; 

    If you want to move with respect to GameTime, replace the first line with this:

    Vector3 Movement = new Vector3(0f, 0f, 5f * (float)(gameTime.ElapsedRealTime.TotalMilliseconds / 1000));

    where 5f is the speed per second

    Quanternions are really confusing to me, whereas this can be done quite easily and has worked for me in quite a few games.  I'm not sure how much you know about 3D math, but I certianly don't care for that much brainpower.  If you just use vectors, then it is simple and easy to debug.
    -- Sir Cmpwn
  • 10/9/2009 8:05 PM In reply to

    Re: rotatating and moving the player in facing direction

    I used a Vector3 when I was starting out... but Vector3 leads to gimble lock. If you are doing something like a 3D space simulator, you will have a much easier time with quaternions.
  • 10/9/2009 8:18 PM In reply to

    Re: rotatating and moving the player in facing direction

    what if wanted to now rotate the player with the left and right on the left thumbstick and move with the up and down on the left thumbstick what would be the sytax

    also player.position moves in the facing direction however but due to the rotation of the player in the load content:
     player.rotation = new Vector3(-20.5f, -33.0f, -28.0f);

    - this makes the player move in the wrong direction please help
  • 10/13/2009 9:48 PM In reply to

    Re: rotatating and moving the player in facing direction

    Just so you're aware, XNA rotates with radians, not degrees.  -20.5 in XNA is 3690 degrees.  -20.5 degrees is actually 0.1139 radians.
    In radians, 180 degrees is pi, and 360 degrees is 2 pi.

    To answer your question about syntax:
    player.Position += GamePadState.Thumbsticks.Left.Y * [movement];
    player.Rotation.Y += GamePadState.Thumbsticks.Left.X * [rotation];

    This assumes that you have a class called player, and that it contains a vector3 for position and one for rotation.
    This rotates along the Y axis (left and right).
    Replace [movement] and [rotation] with your desired amounts.
    -- Sir Cmpwn
  • 10/13/2009 10:22 PM In reply to

    Re: rotatating and moving the player in facing direction

    Issue 1:

    how to get the player to rotate and actually move in the facing direction because without that line of code the player is facing the floor is this an export issue? how do i resolve this

    issue 2:
    how do i stop the player moving back on the tumbstick?
    - alternatively rotate the player 180
  • 10/13/2009 10:52 PM In reply to

    Re: rotatating and moving the player in facing direction

    Issue 1:
    Sounds like a camera issue to me, I can help if you are comfortable posting the full source code (preferably a link, don't fill up the thread with code)

    Issue 2:
    Easy -
    If you want to keep the player from moving backwards at all, then:

    1 public void UpdateMovement(Player Player, GameTime GameTime, GamePadState GamePadState)  
    2 {  
    3     // Get the initial vector  
    4     Vector3 Movement = new Vector3(0f, 0f, GamePadState.Thumbsticks.Left.Y);  
    5     if (Movement.Z <= 0)  
    6         return;  
    7     // 5f is the amount of movement per second.  Change it to suit your needs.  
    8     Movement *= 5f * (GameTime.ElapsedRealTime.TotalMilliseconds / 1000);  
    9     // Transform Movement around the rotation of the player  
    10     Movement = Vector3.Transform(Movement,  
    11                         Matrix.CreateRotationY(Player.Rotation.Y)); 
    13     // Move the player by the specified amount  
    14     Player.Position += Movement;  
    15

    Lets break this down.  Line 4 gets the GamePadState's thumbstick positions, and creates a movement vector based on that.
    Line 5 checks for backwards movement, and returns if there is any, not changing the player's position at all.
    Line 8 muliplies the movement by the maximum movement per second.  This means that if the player is holding the thumbstick all the way up, they would move by 5f in one second.  Change this to fit your game.
    Line 10 creates a rotation matrix and rotates the vector around it to factor in for the player's rotation.  Only transforming by the Y axis keeps the player on the ground.
    Line 14 finally applies the change.

    If you want the player to move forwards when the push forwards, and move forwards if they push back, then replace the "return" in line 6 with "Movement.Z = -Movement.Z"

    Issue 2a:
    Just put this in for moving 180 degrees:
    1 if (/*Something related to turning 180 degrees, such as a button press*/)  
    2      Player.Rotation.Y += MathHelper.Pi; 

    In radians, pi is 180 degrees.
    -- Sir Cmpwn
  • 10/20/2009 9:57 AM In reply to

    Re: rotatating and moving the player in facing direction

    what website shall i use to upload my code then
  • 10/20/2009 12:10 PM In reply to

    Re: rotatating and moving the player in facing direction

    There are a ton of options, but skydrive is probably best.  Use your live id at skydrive.live.com and place the code in a public folder, .zipped up.
    -- Sir Cmpwn
  • 10/20/2009 4:54 PM In reply to

    Re: rotatating and moving the player in facing direction

    Have your player class include a world transform matrix. Then you can just go like this

    player.position = player.position + player.world.forward * magnitudeOfMotion;

    A matrix has fields for a forward backward left right up and down vector expressing the directions relative to their transformation.
  • 11/21/2009 11:12 AM In reply to

    Re: rotatating and moving the player in facing direction

    Ive uploaded everything to do with my XNA project to my skydrive, its not meant to be a particually hard or easy game i just want the player to move properly and to be clamped to the world this is for my porfolio to get a job in the industry please dont steal it !!

    not sure how you access my sky drive public folder but my email is
    tfellas@hotmail.co.uk

    the contents needs to be downloaded exactly how its layed out, cheers
    ps the bin folder was too large to upload if you need let me know
Page 1 of 1 (14 items) Previous Next