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

Constant decrease in movement speed in DirectX

Started by Exomoto Jan 16, 2010 at 2:06 PM 1 replies 950+ views
Original Post
Exomoto
Exomoto
Hey guys, I recently moved to DirectX and I've been putting together a little component-based framework to make a prototype with, but I'm having what seems like a bizarre problem. Basically, I'm rendering a box to the screen, and each frame I want to be able to move it forward, left, back or right using the 'w', 'a', 's' and 'd' keys respectively. At the minute my code is very simple. If any of these buttons are pressed, the appropriate x, y, z variable is set, and afterwards a D3DXMATRIX for movement is created, set with D3DXMatrixIdentity, and then set again with D3DXMatrixTranslation. After this, the movement matrix is added to the position matrix for the box, and this matrix is used when setting the WVP matrix. The problem is, the speed of the objects movement in any direction seems to decrease rapidly the more each button is pressed, and if no button is pressed, the box slowly moves back toward the (0, 0, 0) position. Can anyone help me understand why this is happening?

D3DXMATRIX boxPosition;

void App::Init()
{
	//Other code
	D3DXMatrixIdentity(&boxPosition);
	//More code
}

void App::Update(float dt)
{
	float x = 0.0f,  y = 0.0f, z = 0.0f;

	if(GetAsyncKeyState('A') & 0x8000)	x = -1000.0f * dt;
	if(GetAsyncKeyState('D') & 0x8000)	x =	 1000.0f * dt;
	if(GetAsyncKeyState('W') & 0x8000)	z =  1000.0f * dt;
	if(GetAsyncKeyState('S') & 0x8000)	z = -1000.0f * dt;

	D3DXMATRIX temp;
	D3DXMatrixIdentity(&temp);
	D3DXMatrixTranslation(&temp, x, y, z);

	boxPosition += temp;
	//More code
}

void App::Draw()
{
    //More code
	pass->Apply(0);
	mWVP = boxPosition * mView * mProj;
	mfxWVPVar->SetMatrix((float*)&mWVP);
	//Render box
	mPlayer->Draw();
	//More code
}


Apologies if this isn't DirectX related. [Edited by - Exomoto on January 16, 2010 2:37:55 PM]
Buckeye
Buckeye
You have:
player += temp;

What is "player" used for? Is that your error?

Instead of:
D3DXMatrixIdentity( &temp ); // unnecessary. gets overwritten by next line
D3DXMatrixTranslation(...);
etc.

How about:
boxPosition._41 += x;
boxPosition._43 += z;
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.
Exomoto
Exomoto
Quote:
Original post by Buckeye
You have:
player += temp;

What is "player" used for? Is that your error?

Instead of:
D3DXMatrixIdentity( &temp ); // unnecessary. gets overwritten by next line
D3DXMatrixTranslation(...);
etc.

How about:
boxPosition._41 += x;
boxPosition._43 += z;


My boxPosition matrix was initially named player, but I renamed it and tidied some of the code before posting it on here (clean code went out of the window about 2 hours ago), apologies for that.

Your suggestion worked perfectly, so thanks a lot for that it's greatly appreciated. I'll have to read up on the D3DXMATRIX class some more.

Ah, while writing this post I just fiddled a little with the code and realised I have to multiply boxPosition by temp rather than adding them...

Topic Locked

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

Sign in to reply to this topic.