• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
football94

Making a 3D model face the direction it's moving

6 posts in this topic

Hi guys

 

Ive been experimenting with the xna skinning sample(link below) and have begun trying to implement controller/keyboard input, right now Im able to move the model forward,backwards, left, and right but after different approaches I havent been able to find info on how to get the model to face the direction in which it is moving if anyone could point me in the right direction on how this is done it would be much appreciated.(the code I have so far is below)

 

                     

                                                 Thankyou

 

 

 

 

 

skinning sample link

http://xbox.create.msdn.com/en-US/education/catalog/sample/skinned_model

 

 

 

below is what I have so far in the draw method

 protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice device = graphics.GraphicsDevice;

            device.Clear(Color.CornflowerBlue);

            Matrix[] bones = animationPlayer.GetSkinTransforms();
            for (int i = 0; i < bones.Length; i++)
            {
                bones[i] *=
                      Matrix.CreateRotationX(Rotation.X) //Computes the rotation
                    * Matrix.CreateRotationY(Rotation.Y)
                    * Matrix.CreateRotationZ(Rotation.Z)
                    * Matrix.CreateTranslation(position)
                    * Matrix.CreateWorld(Translation, Vector3.Forward, Vector3.Up); //Move the models position
            }
            

          

           
            Matrix[] modelTransforms = new Matrix[currentModel.Bones.Count];
            currentModel.CopyAbsoluteBoneTransformsTo(modelTransforms);

            // Render the skinned mesh.
            foreach (ModelMesh mesh in currentModel.Meshes)
            {
                foreach (Effect effect2 in mesh.Effects)
                {
                   
                    
                    effect2.Parameters["Bones"].SetValue(bones);
                    effect2.Parameters["View"].SetValue(camera.viewMatrix);
                    effect2.Parameters["Projection"].SetValue(camera.projectionMatrix);
                    
                   
                }

                mesh.Draw();

                DrawCity();
                drawDebug();
                fine1();
                
                
            }

            base.Draw(gameTime);
        }

this is what I have so far for the input method

private void HandleInput(GameTime gameTime)
        {
            KeyboardState currentKeyboardState = Keyboard.GetState();

            // Check for exit.
            if (currentKeyboardState.IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            float speed = 0.02f; //Dictates the speed
            if (currentKeyboardState.IsKeyDown(Keys.Left))
            {
                position.Z -= speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
              
            }
            if (currentKeyboardState.IsKeyDown(Keys.Right))
            {
                position.Z += speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Up))
            {
                position.X += speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Down))
            {
                position.X -= speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            }
           
           
          

        
        }
0

Share this post


Link to post
Share on other sites

Hi football94!

You can calculate the rotation between the two world points (Vector3) 

This is what I used in my game:

	public Quaternion GetRotation(Vector3 targetPos)
		{
			return GetRotation(this.Position, targetPos);
		}

		public Quaternion GetRotation(Vector3 entityPos, Vector3 targetPos)
		{
			// the new forward vector, so the avatar faces the target
			Vector3 newForward = Vector3.Normalize(entityPos - targetPos);

			// calc the rotation so the avatar faces the target
			var src = Vector3.Forward;

			var dest = newForward;

			src.Normalize();
			dest.Normalize();

			float d = Vector3.Dot(src, dest);

			if (d >= 1f)
			{
				return Quaternion.Identity;
			}
			if (d < (1e-6f - 1.0f))
			{
				Vector3 axis = Vector3.Cross(Vector3.UnitX, src);

				if (axis.LengthSquared() == 0)
				{
					axis = Vector3.Cross(Vector3.UnitY, src);
				}

				axis.Normalize();
				return Quaternion.CreateFromAxisAngle(axis, MathHelper.Pi);
			}


			float s = (float)Math.Sqrt((1 + d) * 2);
			float invS = 1 / s;

			Vector3 c = Vector3.Cross(src, dest);
			Quaternion q = new Quaternion(invS * c, 0.5f * s);
			q.Normalize();

			return q;
		}

when drawing your model use:

NewPosition = should be the target (future) position that your model is moving towards.


Quaternion RotationQuat = GetRotation(NewPosition);

 

then you need to store your RotationQuat and do NOT update it until your new "future" position is changed.

otherwise the model will rotate back the moment you stop moving.

for (int i = 0; i < bones.Length; i++)
{
bones[i] *=
Matrix.CreateFromQuaternion(RotationQuat)
* Matrix.CreateTranslation(position)
* Matrix.CreateWorld(Translation, Vector3.Forward, Vector3.Up); //Move the models position
...

I hope this helped :)

1

Share this post


Link to post
Share on other sites

Please don't use all caps for your thread titles, it makes it look like you're yelling at everyone. smile.png

0

Share this post


Link to post
Share on other sites

Hi.

heres a not so wright way of doing it it works with x , z y should be set to 0.

 

first you need to define a 3dvector for the direction the mesh was facing when exported.

only set UnitsFacingDirection at init time when you create your asset

 

D3DXVECTOR3 toTarget = UnitsFacingDirection;

 

D3DXVec3Normalize(&toTarget,

&toTarget);

 

D3DXVec3Normalize(&vHeading, &vHeading);//vheading is the vector you should have for the motion of the object

 

 

 

//first determine the angle between the heading vector and the target

 

//double angle = acos(m_vHeading.Dot(toTarget));

FLOAT dot = D3DXVec3Dot(&vHeading, &toTarget);//this bit is a bit off needs fixing

dot = Clamp( dot, - 1.0f, 1.0f );

 

double Angle = acos(dot);

 

 

//return if the player is facing the target

 

if(Angle < 0.00001)

 

return;

 

 

//The next few lines use a rotation matrix to rotate the player's heading

//vector accordingly

 

//notice how the direction of rotation has to be determined when creating

//the rotation matrix

 

 

 

int s = Sign(vHeading, toTarget);

 

D3DXMatrixRotationY(&Rotation, Angle * s);

 

and that rotates it about the Y axis.

 

 

//------------------------ Sign ------------------------------------------

//

// returns positive if v2 is clockwise of this vector(v1),

// minus if anticlockwise (Y axis pointing down, X axis to right)

//------------------------------------------------------------------------

enum

 

{clockwise = 1, anticlockwise = -1};

int

 

Sign(const D3DXVECTOR3& v1, const D3DXVECTOR3& v2);

 

int Sign(const D3DXVECTOR3& v1, const D3DXVECTOR3& v2)

{

 

if (v1.z*v2.x > v1.x*v2.z)

{

 

return anticlockwise;

}

 

else

{

 

return clockwise;

}

blink.png

0

Share this post


Link to post
Share on other sites

Please don't use all caps for your thread titles, it makes it look like you're yelling at everyone. smile.png

 

 

laugh.png So sorry I had no idea, and I appreciate the replies to my post thanks guys Ill let you know what I come up with.
 

0

Share this post


Link to post
Share on other sites

 It Worked!!!

 

Thanks Zerratar that worked perfectly!

 

code below is just a small sample of how I tested it

 

 

Vector3 NewPosition = new Vector3(55, 0, 77);

  if (currentKeyboardState.IsKeyDown(Keys.Left))
            {
               
                RotationQuat = GetRotation(NewPosition); 
            }

and Thanks again guys for responding to my post biggrin.png

0

Share this post


Link to post
Share on other sites

 It Worked!!!

 

Thanks Zerratar that worked perfectly!

 

code below is just a small sample of how I tested it

 

 

Vector3 NewPosition = new Vector3(55, 0, 77);

  if (currentKeyboardState.IsKeyDown(Keys.Left))
            {
               
                RotationQuat = GetRotation(NewPosition); 
            }

and Thanks again guys for responding to my post biggrin.png

Hurray! I'm glad it worked for you! :)

0

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  
Followers 0