Skip to main content
GameDev.net gamedev.net
🔒 Locked

my model points upwars when i adjust yaw

Started by xchimera Feb 27, 2013 at 6:55 PM 10 replies 2.1k views
Original Post
xchimera
xchimera

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);
xchimera
xchimera

anyone?

Seabolt
Seabolt

How are you applying your matrices?

Perception is when one imagination clashes with another
xchimera
xchimera

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.

Seabolt
Seabolt

How do you combine world and rotation?

Perception is when one imagination clashes with another
xchimera
xchimera

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.

Seabolt
Seabolt

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.

Perception is when one imagination clashes with another
xchimera
xchimera

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);
xchimera
xchimera
Can anyone tell me if my code looks right?
Chetanhl
Chetanhl

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. :)

xchimera
xchimera

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;
        }
    }
}
xchimera
xchimera
I have posted my complete code. Isnt there anyone who can see any mistakes i`ve made?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.