[XNA] Problem moving 3D model with keyboard

Started by
2 comments, last by DemonLord 13 years, 3 months ago
Hi.

I have a problem moving 3D model, when I press two keys at the same time. Let's say I want to move my model from center of the screen (camera isn't moving) diagonaly to upper left corner.
Even tho I press two keys, the model will move in the direction of the key pressed first or if I press A key after already holding down W key, the model will continue to travel upwards.

Any ideas on how should I fix this?

protected void UpdateInput(GameTime gameTime)        {            KeyboardState currentKeyState = Keyboard.GetState();            float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds;            float moveX = 0.0f;            float moveZ = 0.0f;                        if (currentKeyState.IsKeyDown(Keys.W))                moveX += 0.1f* time;            else if (currentKeyState.IsKeyDown(Keys.S))                moveX -= 0.1f * time;            else if (currentKeyState.IsKeyDown(Keys.A))                moveZ -= 0.1f * time;            else if (currentKeyState.IsKeyDown(Keys.D))                moveZ += 0.1f * time;            modelPosition += new Vector3(moveX, 0, moveZ);        }


protected override void Update(GameTime gameTime)        {            KeyboardState keyboardState = Keyboard.GetState(PlayerIndex.One);            if (keyboardState.IsKeyDown(Keys.Escape))                this.Exit();            UpdateInput(gameTime);            modelPosition += modelVelocity;            modelVelocity *= 0.95f;            base.Update(gameTime);        }


Thanx.
Advertisement
A quick fix would be, replace:

            if (currentKeyState.IsKeyDown(Keys.W))                moveX += 0.1f* time;            else if (currentKeyState.IsKeyDown(Keys.S))                moveX -= 0.1f * time;            else if (currentKeyState.IsKeyDown(Keys.A))                moveZ -= 0.1f * time;            else if (currentKeyState.IsKeyDown(Keys.D))                moveZ += 0.1f * time;


With:

            if (currentKeyState.IsKeyDown(Keys.W))                moveX += 0.1f* time;            if (currentKeyState.IsKeyDown(Keys.S))                moveX -= 0.1f * time;            if (currentKeyState.IsKeyDown(Keys.A))                moveZ -= 0.1f * time;            if (currentKeyState.IsKeyDown(Keys.D))                moveZ += 0.1f * time;
Thanx a million! Man, I'm blind :[
I'll use this topic for my new problem considering 3D model movement.

Problem is when I move my model from center of the screen to the left (or right), model rolls nicely as I want it to, but when I reach the left side of the screen and start to move back towards the center, model moves right AND up towards the camera, what I don't want.

[media]
[/media]

I don't know how to fix this. It seem as if the pivot point was somewhere below the model, but I imported the model into Maya and set pivot to center and export to FBX. (did I forgot something?)

Here's the code for movement:


protected void UpdateInput(GameTime gameTime)
{
KeyboardState newState = Keyboard.GetState();

if (newState.IsKeyDown(Keys.Escape))
this.Exit();

float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds;

float moveX = 0.0f;
float moveZ = 0.0f;

if (newState.IsKeyDown(Keys.W))
moveX += modelSpeed * time;

if (newState.IsKeyDown(Keys.S))
moveX -= modelSpeed * time;

if (newState.IsKeyDown(Keys.A))
{
moveZ -= modelSpeed * time;
modelRotation -= 0.01f;
if (modelRotation < -0.5f)
modelRotation = -0.5f;
}

if (newState.IsKeyDown(Keys.D))
{
moveZ += modelSpeed * time;
modelRotation += 0.01f;
if (modelRotation > 0.5f)
modelRotation = 0.5f;
}

oldState = newState;

modelPosition += new Vector3(moveX, 0, moveZ);
}


If anyone knows what might be the problem, please do tell.

This topic is closed to new replies.

Advertisement