Sign in to follow this  
xchimera

my model points upwars when i adjust yaw

Recommended Posts

xchimera    129

Hi i am making a space game and i am trying to make my model move. i want my model to tilt a bit and then turn. However when i do that my model begins to point upwards when i press the button and i am thinking that it is because my model is rolled a bit. i have tried subtracting the amount of yaw from pitch, but that did nothing. here is my code:

 

            if (keyboardState.IsKeyDown(Keys.A))
            {
                
                if ( tempRoll  > -MathHelper.ToRadians(45 /2))
                {
                    rollAngle -= MathHelper.ToRadians(turningSpeed);

                }
                   yawAngle -= tempRoll / 10;
                    //direction.X -= rollAngle;
            }

            if (keyboardState.IsKeyDown(Keys.D))
            {
                if (tempRoll < MathHelper.ToRadians(45 / 2))
                {
                    rollAngle += MathHelper.ToRadians(turningSpeed);

                }                    
                yawAngle += -tempRoll / 10;
                pitchAngle += pitchAngle - yawAngle;


                //direction.X += -rollAngle;
            }
            tempRoll += rollAngle;
            tempYaw += yawAngle;
            tempYaw = MathHelper.Clamp(tempYaw, 0, 0.01f);

 

Share this post


Link to post
Share on other sites
xchimera    129

this is my complete update method

 

            ProcessKeyboard(gametime);
            world *= Matrix.CreateTranslation(direction);
            rotation *= Matrix.CreateFromYawPitchRoll(yawAngle, pitchAngle, rollAngle);
            rollAngle = 0;
            yawAngle = 0;
            pitchAngle = 0;
            base.Update(gametime);

i set them back to 0 as i don't want them to keep multiplying.

Share this post


Link to post
Share on other sites
xchimera    129

i create my world matrix in my constructor.

 

  public Player(Model model, Vector3 position, float yawAngle, float pitchAngle, float rollAngle, Vector3 direction)
            : base(model)
        {
            Quaternion rot = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), 0f);
            world = Matrix.CreateScale(1f) * Matrix.CreateFromQuaternion(rot) * Matrix.CreateTranslation(position);
            this.yawAngle = yawAngle;
            this.pitchAngle = pitchAngle;
            this.rollAngle =  rollAngle;
            this.direction = direction;
        }

and i then have a property who multiplies them

 

       public override Matrix GetWorld()
        {
            return rotation * world;
        }

which my draw method then uses as my class is derived from that.

Share this post


Link to post
Share on other sites
Seabolt    781

Hm... I'm not entirely sure of how you're doing it, but it seems like you're missing a rotation matrix somewhere. If you want to tilt, then turn, you need two quaternions/matrices to model that transform, and I'm only seeing one. It looks like you're only getting the yaw rotation, but not the pitch and roll rotations. Just guessing though.

Share this post


Link to post
Share on other sites
xchimera    129

i have a rotation matrix in my update method, which i then add to the world matrix in my property, but it might be that i am missing something.

rotation *= Matrix.CreateFromYawPitchRoll(yawAngle, pitchAngle, rollAngle);

Share this post


Link to post
Share on other sites
Chetanhl    354

try

 

rotation = Matrix.CreateFromYawPitchRoll(yawAngle, pitchAngle, rollAngle); 
 

Also is your post above in constructor - 

 

world = scale * rotation * translate

and later

GetWorld = rotation * world    i.e;    GetWorld = rotation * scale * rotation * translate ????

 

Also try calling Normalize on quaternion before converting into matrix.

 

These are just couple of things i noticed form limited code you provided hope that helps. :)

Share this post


Link to post
Share on other sites
xchimera    129

thank you for your post, however unfortunately it didn't work. if i use

rotation = Matrix.CreateFromYawPitchRoll(yawAngle, pitchAngle, rollAngle);

then it simply doesn't turn, same as if i change my GetWorld() to just return the world matrix. And if i try to Normalize the quarternion it makes no difference.

it just seems as it is rotating around its own axis. have i multiplied it wrong?

 

this is the complete class:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace SpaceFight
{
    class Player : BasicModel
    {
        Matrix rotation = Matrix.Identity;
        float yawAngle = 0;
        float pitchAngle = 0;
        float rollAngle = 0;
        Vector3 direction;
        float movementSpeed = 2000.0f;
        float turningSpeed = 1000.0f;

        float tempRoll = 0;
        float tempYaw = 0;
        float tempPitch = 0;

        public Player(Model model, Vector3 position, float yawAngle, float pitchAngle, float rollAngle, Vector3 direction)
            : base(model)
        {
            Quaternion rot = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), 0f);
            rot.Normalize();
            world = Matrix.CreateScale(1f) * Matrix.CreateFromQuaternion(rot) * Matrix.CreateTranslation(position);
            this.yawAngle = yawAngle;
            this.pitchAngle = pitchAngle;
            this.rollAngle =  rollAngle;
            this.direction = direction;
        }


        public override void Update(GameTime gametime)
        {
            
            ProcessKeyboard(gametime);
            world *= Matrix.CreateTranslation(direction);
            rotation *= Matrix.CreateFromYawPitchRoll(yawAngle, pitchAngle, rollAngle);
            rollAngle = 0;
            yawAngle = 0;
            pitchAngle = 0;
            base.Update(gametime);
        }

        private void ProcessKeyboard(GameTime gametime)
        {
            KeyboardState keyboardState = Keyboard.GetState();
            
            if (keyboardState.IsKeyDown(Keys.W))
            {
                if (direction.Z < 1.5f)
                    direction.Z += (float)gametime.ElapsedGameTime.TotalMilliseconds / movementSpeed;
                    // direction.Z += 0.1f;
            }
            if (keyboardState.IsKeyDown(Keys.S))
            {
                if(direction.Z > -1.5f)
                    direction.Z -= (float)gametime.ElapsedGameTime.TotalMilliseconds / movementSpeed;
            }

            if (keyboardState.IsKeyDown(Keys.Space))
            {
                if (direction.Z > -1.5f && direction.Z < 0f)    //check bevægelse tilbage
                    direction.Z += (float)gametime.ElapsedGameTime.TotalMilliseconds / movementSpeed*2;

                else if (direction.Z < 1.5f && direction.Z > 0) //check bevægelse fremad
                    direction.Z -= (float)gametime.ElapsedGameTime.TotalMilliseconds / movementSpeed*2;
                
                
                if (direction.Z < 0.1f && direction.Z > 0  || direction.Z > -0.1f && direction.Z < 0)
                    direction = Vector3.Zero;
            }


            float turningSpeed = (float)gametime.ElapsedGameTime.TotalMilliseconds / 80.0f;
            turningSpeed *= 1.6f * 1;

            if (keyboardState.IsKeyDown(Keys.A))
            {

                if (tempRoll > -MathHelper.ToRadians(45 / 2))
                {
                    rollAngle -= MathHelper.ToRadians(turningSpeed);

                }
                yawAngle -= tempRoll / 10;
                //direction.X -= rollAngle;
            }

            if (keyboardState.IsKeyDown(Keys.D))
            {
                if (tempRoll < MathHelper.ToRadians(45 / 2))
                {
                    rollAngle += MathHelper.ToRadians(turningSpeed);

                }
                yawAngle += -tempRoll / 10;
                pitchAngle += pitchAngle - yawAngle;


                //direction.X += -rollAngle;
            }
            tempRoll += rollAngle;
            tempYaw += yawAngle;
            tempYaw = MathHelper.Clamp(tempYaw, 0, 0.01f);

        }

        public override Matrix GetWorld()
        {
            return  world;
        }
    }
}
Edited by xchimera

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this