Jump to content

  • Log In with Google      Sign In   
  • Create Account


my model points upwars when i adjust yaw


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 xchimera   Members   -  Reputation: 129

Like
0Likes
Like

Posted 27 February 2013 - 12:55 PM

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

 



Sponsor:

#2 xchimera   Members   -  Reputation: 129

Like
0Likes
Like

Posted 06 March 2013 - 04:20 AM

anyone?



#3 Seabolt   Members   -  Reputation: 609

Like
0Likes
Like

Posted 07 March 2013 - 04:09 PM

How are you applying your matrices?


Perception is when one imagination clashes with another

#4 xchimera   Members   -  Reputation: 129

Like
0Likes
Like

Posted 08 March 2013 - 05:42 AM

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.



#5 Seabolt   Members   -  Reputation: 609

Like
0Likes
Like

Posted 08 March 2013 - 10:19 AM

How do you combine world and rotation?


Perception is when one imagination clashes with another

#6 xchimera   Members   -  Reputation: 129

Like
0Likes
Like

Posted 08 March 2013 - 07:46 PM

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.



#7 Seabolt   Members   -  Reputation: 609

Like
0Likes
Like

Posted 09 March 2013 - 10:03 AM

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

#8 xchimera   Members   -  Reputation: 129

Like
0Likes
Like

Posted 09 March 2013 - 04:52 PM

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


#9 xchimera   Members   -  Reputation: 129

Like
0Likes
Like

Posted 19 March 2013 - 08:47 AM

Can anyone tell me if my code looks right?

#10 chetanjags   Members   -  Reputation: 173

Like
0Likes
Like

Posted 19 March 2013 - 09:11 AM

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


My Game Development Blog : chetanjags.wordpress.com


#11 xchimera   Members   -  Reputation: 129

Like
0Likes
Like

Posted 19 March 2013 - 04:31 PM

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, 21 March 2013 - 03:36 AM.


#12 xchimera   Members   -  Reputation: 129

Like
0Likes
Like

Posted 02 April 2013 - 03:12 PM

I have posted my complete code. Isnt there anyone who can see any mistakes i`ve made?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS