Jump to content

  • Log In with Google      Sign In   
  • Create Account


DirectX sprite movement seems 'glitchy'


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
35 replies to this topic

#1 Interminable   Members   -  Reputation: 127

Like
0Likes
Like

Posted 13 July 2012 - 04:36 AM

I've got a sprite that moves across the screen, slowly. I've noticed as it moves, there appear to be 'imperfections', by that I mean it looks like it changes size by about single pixel in width as it moves along, then changing back, then doing it again, etc.

I can paste code if needed, but I'm wondering if there may simply be some 'fundamental' aspect of drawing sprites in DirectX that I may have missed.

Edited by Interminable, 13 July 2012 - 04:37 AM.


Sponsor:

#2 DarkRonin   Members   -  Reputation: 604

Like
0Likes
Like

Posted 13 July 2012 - 05:05 AM

Nope, you haven't missed anything. Thankfully, sprites do move very smoothly :)

I am currently writing a game with sprites alone and initially had poor movement. It was jumpy and ugly, and not smooth. But, it turned out it was the way I was calculating movement speed against framerate and I was using INT for positioning (which sounds logical as I was working in screenspace).

How did I fix it?
  • Multiply the sprites movement speed against the time since last frame
  • Position the sprites using float instead of int
Not having seen any of your code I would check these two things. I would love to know how you go (and happy to help where I can) :)

And if you can share more info on your timers etc, that would be great.

#3 Interminable   Members   -  Reputation: 127

Like
0Likes
Like

Posted 13 July 2012 - 06:20 AM

Nope, you haven't missed anything. Thankfully, sprites do move very smoothly Posted Image

I am currently writing a game with sprites alone and initially had poor movement. It was jumpy and ugly, and not smooth. But, it turned out it was the way I was calculating movement speed against framerate and I was using INT for positioning (which sounds logical as I was working in screenspace).

How did I fix it?

  • Multiply the sprites movement speed against the time since last frame
  • Position the sprites using float instead of int
Not having seen any of your code I would check these two things. I would love to know how you go (and happy to help where I can) Posted Image

And if you can share more info on your timers etc, that would be great.


Here's what I was using to test it:

void Object::Update()
{
coordinates.x += (20 * frameRenderTime);
}

coordinates is an instance of D3DXVECTOR3. Update() is called from another Update() function in my main Engine class, whose Update() function is called from main, inside the message loop:

	try
	{
	 GetSystemTime(&systemTime);
	 milliseconds = systemTime.wMilliseconds;
	 if(milliseconds != prevMilliseconds && milliseconds%10 == 0)
	 {
	  if(milliseconds<prevMilliseconds)
	  {
	   prevMilliseconds -= 1000;
	   // This will update roughly once every second.
	  }
	  frameRenderTime = milliseconds - prevMilliseconds;
	  frameRenderTime /= 1000;
	  prevMilliseconds = milliseconds;
	  mainWindow.enginePointer()->Update();
	 }
	
	 mainWindow.enginePointer()->rendererPointer()->Render();
	}
	catch(std::exception e)
	{
	 e.what();
	 return 1;
	}

Edited by Interminable, 13 July 2012 - 06:21 AM.


#4 JWBaker   Members   -  Reputation: 235

Like
0Likes
Like

Posted 13 July 2012 - 10:02 AM

This might help the issue.

http://msdn.microsof...0(v=vs.85).aspx

Edited by JWBaker, 13 July 2012 - 10:03 AM.


#5 Interminable   Members   -  Reputation: 127

Like
0Likes
Like

Posted 13 July 2012 - 02:05 PM

This might help the issue.

http://msdn.microsof...0(v=vs.85).aspx


What the article describes sounds like it could be related to the issue I'm having.

The problem is, I'm not drawing a 2D texture to a surface. It's a standalone sprite. I'm unsure how I can deal with this.

#6 JWBaker   Members   -  Reputation: 235

Like
0Likes
Like

Posted 13 July 2012 - 08:38 PM


This might help the issue.

http://msdn.microsof...0(v=vs.85).aspx


What the article describes sounds like it could be related to the issue I'm having.

The problem is, I'm not drawing a 2D texture to a surface. It's a standalone sprite. I'm unsure how I can deal with this.


Do you mean you are using the Sprite class?

What you need to do is shift your Projection Matrix to account for the issue. This is how i setup mine and it seems to work. I'm not a DX master by any means but give it a shot and see if it helps. My Coordinate system has 0,0 at the center of the screen and +Y is up.

Device.SetTransform(TransformState.Projection, Matrix.Translation(-0.5f, 0.5f, 0.0f) * Matrix.OrthoLH(_form.ClientSize.Width, _form.ClientSize.Height, 0, 1));

#7 Interminable   Members   -  Reputation: 127

Like
0Likes
Like

Posted 14 July 2012 - 02:08 AM



This might help the issue.

http://msdn.microsof...0(v=vs.85).aspx


What the article describes sounds like it could be related to the issue I'm having.

The problem is, I'm not drawing a 2D texture to a surface. It's a standalone sprite. I'm unsure how I can deal with this.


Do you mean you are using the Sprite class?

What you need to do is shift your Projection Matrix to account for the issue. This is how i setup mine and it seems to work. I'm not a DX master by any means but give it a shot and see if it helps. My Coordinate system has 0,0 at the center of the screen and +Y is up.

Device.SetTransform(TransformState.Projection, Matrix.Translation(-0.5f, 0.5f, 0.0f) * Matrix.OrthoLH(_form.ClientSize.Width, _form.ClientSize.Height, 0, 1));


I was just altering the coordinates I was drawing it at with Id3DDevice::Draw(). ><

But I recall some of this transformation stuff now, I have a feeling I made a thread on here a while back which involved those but was to do with rotations...

Anyway, in trying to look at how you're doing that particular line, I am unsure how you're doing 'TransformState.Projection'. It doesn't seem to be possible with D3DTRANSFORMSTATETYPE. And that's the required first argument for iD3DXDevice::SetTransform().

In addition I assume there's some specific function for filling the D3DTRANSFORMSTATETYPE struct, but I can't seem to find it. ><


I have altered my code to how I was using SetTransform() in the past. First I have a PrepareTransform() function:


void Renderer::PrepareTransform(int input)
{
transformStateType->Projection;
d3dXSprite->SetTransform(
D3DXMatrixTransformation2D(&d3dMatrix,
NULL,
0.0,
&D3DXVECTOR2(1.0,1.0),
&D3DXVECTOR2(texturesToRender[input].objectPointer->originPointer()->x,texturesToRender[input].objectPointer->originPointer()->y),
0.0f,
&D3DXVECTOR2(texturesToRender[input].objectPointer->coordinatesPointer()->x,texturesToRender[input].objectPointer->coordinatesPointer()->y));

d3dXSprite->SetTransform(&d3dMatrix);
}

And I draw after that function is called:


if(FAILED(hResult = d3dXSprite->Draw(*texturesToRender[i].objectPointer->texturePointer()->texturePointer(), NULL, NULL, NULL, 0xffffffff)))
{

Unfortunately the issue still exists, but I would assume this is because I'm not dealing with this projection matrix stuff you're mentioning.

Edited by Interminable, 14 July 2012 - 02:10 AM.


#8 DarkRonin   Members   -  Reputation: 604

Like
0Likes
Like

Posted 14 July 2012 - 02:54 AM

I still think it is to do with your timer. GetSystemTime() seems to be only accurate to the millisecond.

If you have a basic scene you might be rendering faster than 1000 fps, so your scene will be choppy as the timer wont be acurate enough.

On my 3 year old video card my application (that also uses sprites) is rendering at 8000 FPS. This is why I had the same problems that your are experiencing. I had to re-think my timer.

Give my timer a try Posted Image

#pragma once
class Timer
{
public:
Timer()
{
  liCurrent.QuadPart=0;
  liPrevious.QuadPart=0;
}
~Timer()
{
}
long double TimeSinceLastFrame()
{
  QueryPerformanceFrequency(&liPerfFreq);
  QueryPerformanceCounter(&liCurrent);
  ddFrameTime=(liCurrent.QuadPart-liPrevious.QuadPart)/long double(liPerfFreq.QuadPart)*1000;
  liPrevious.QuadPart=liCurrent.QuadPart;
  return ddFrameTime;
}
private:
LARGE_INTEGER liCurrent;
LARGE_INTEGER liPrevious;
LARGE_INTEGER liPerfFreq;
LARGE_INTEGER liStart;
long double ddFrameTime;
};
TimeSinceLastFrame() will return exactly that (but using the performance counters)

I would love to know how you go Posted Image

Edited by lonewolff, 14 July 2012 - 02:54 AM.


#9 Interminable   Members   -  Reputation: 127

Like
0Likes
Like

Posted 14 July 2012 - 03:12 AM

I still think it is to do with your timer. GetSystemTime() seems to be only accurate to the millisecond.

If you have a basic scene you might be rendering faster than 1000 fps, so your scene will be choppy as the timer wont be acurate enough.

On my 3 year old video card my application (that also uses sprites) is rendering at 8000 FPS. This is why I had the same problems that your are experiencing. I had to re-think my timer.

Give my timer a try Posted Image

#pragma once
class Timer
{
public:
Timer()
{
  liCurrent.QuadPart=0;
  liPrevious.QuadPart=0;
}
~Timer()
{
}
long double TimeSinceLastFrame()
{
  QueryPerformanceFrequency(&liPerfFreq);
  QueryPerformanceCounter(&liCurrent);
  ddFrameTime=(liCurrent.QuadPart-liPrevious.QuadPart)/long double(liPerfFreq.QuadPart)*1000;
  liPrevious.QuadPart=liCurrent.QuadPart;
  return ddFrameTime;
}
private:
LARGE_INTEGER liCurrent;
LARGE_INTEGER liPrevious;
LARGE_INTEGER liPerfFreq;
LARGE_INTEGER liStart;
long double ddFrameTime;
};
TimeSinceLastFrame() will return exactly that (but using the performance counters)

I would love to know how you go Posted Image


I'll give it a try...but I'm also updating how far they should move using the time it takes to render the last frame. They appear to move smoothly already, just with minute changes in size as they go. Anyway, I'll EDIT this post with how I get on (or reply in a new post if there have been further responses).

EDIT: TimeSinceLastFrame()'s first return value is insanely high (eg 9029811) and screws up anything that relies on it for moving etc at the very beginning of the program. It eventually settles down to values of hundreds.

EDIT2: It appears to do this regardless of when the first call to it was, so I call it a few times needlessly at the beginning of my program to make it settle down a little before I start actually needing to use it.

Unfortunately the issue I am experiencing with the minute changes in size remains.

Edited by Interminable, 14 July 2012 - 03:24 AM.


#10 DarkRonin   Members   -  Reputation: 604

Like
0Likes
Like

Posted 14 July 2012 - 03:16 AM

I'll give it a try...but I'm also updating how far they should move using the time it takes to render the last frame.


No probs, but all you need to do is multiply your movement speed against TimeSinceLastFrame() Posted Image

Edited by lonewolff, 14 July 2012 - 03:17 AM.


#11 Interminable   Members   -  Reputation: 127

Like
0Likes
Like

Posted 14 July 2012 - 03:31 AM

Personally I do not believe this is a timing issue. I think JWBaker was on the right track with regard to texture distortion. I just have no idea how to properly move it (by that I mean without the distortion) with SetTransform(), trouble is I'm unsure what step I need to do for Projection Matrices (as per my question a few posts back Posted Image ).

EDIT: I could have sworn when I edited my previous post that your post was not originally there. My edits explain what happened.

Edited by Interminable, 14 July 2012 - 03:41 AM.


#12 Interminable   Members   -  Reputation: 127

Like
0Likes
Like

Posted 21 July 2012 - 05:19 AM

Sorry to bump this but I am still experiencing this issue. I completely rewrote how I was drawing sprites to use textured quads instead of ID3DXSPRITE but I'm still getting the issue.

I did read that I should adjust the coordinates of my quad by -0.5, but this hasn't helped.

This is exactly what happens:

* My sprite moves across the screen from left to right at a steady rate (calculated using the time it took to render each frame).
* As a sprite moves along, at some point it will appear to shrink by what appears to be one pixel on the right side.
* After it has moved along a bit more it will appear to expand by one pixel on the left side, thus returning to its original size.
* It will repeat this process whilst it moves along.

I'm not using any custom defined Projection Matrices or anything like that.

This is the code I'm using for configuring my vertices, it's based on some other code I found online a while back.

void Sprite::ConfigureVertices()
{
if(vertexBuffer)
{
  vertexBuffer->Release();
}
if(FAILED(hResult = (*mTexturePointer->rendererPointer()->d3dDevicePointer())->CreateVertexBuffer
  (sizeof(TLVERTEX)*4,
  D3DUSAGE_WRITEONLY,
  mTexturePointer->rendererPointer()->GetD3DFVF_CUSTOMVERTEXFORMAT(),
  D3DPOOL_MANAGED,
  &vertexBuffer,
  NULL)))
{
  logWindow.AddLogEntry(3, TEXT("IDirect3DDevice9::CreateVertexBuffer() FAILED"), hResult, GetLastError());
  MessageBox(*mTexturePointer->rendererPointer()->hwndPointer(), TEXT("IDirect3DDevice9::CreateVertexBuffer() failed!  This program will now close."), TEXT("CRITICAL FAILURE"), MB_OK | MB_ICONERROR | MB_TOPMOST);
  CheckCriticalExit();
}
else
  logWindow.AddLogEntry(6, TEXT("IDirect3DDevice9::CreateVertexBuffer() succeeded"), hResult);
vertices.resize(4);
vertices[0].colour = 0xFFFF00FF;
vertices[0].x = (float) objectPointer->coordinatesPointer()->x - 0.5f; // Left
vertices[0].y = (float) objectPointer->coordinatesPointer()->y - 0.5f; // Top
vertices[0].z = 0.0f;
vertices[0].rhw = 1.0f;
vertices[0].u = 0.0f;
vertices[0].v = 0.0f;
vertices[1].colour = 0xFFFF00FF;
vertices[1].x = vertices[0].x+mTexturePointer->textureDescPointer()->Width;// - 0.5f; // Right
vertices[1].y = objectPointer->coordinatesPointer()->y - 0.5f; // Top
vertices[1].z = 0.0f;
vertices[1].rhw = 1.0f;
vertices[1].u = 1.0f;
vertices[1].v = 0.0f;
vertices[2].colour = 0xFFFF00FF;
vertices[2].x = (float) vertices[0].x+mTexturePointer->textureDescPointer()->Width;// - 0.5f; // Right
vertices[2].y = (float) vertices[0].y+mTexturePointer->textureDescPointer()->Height;// - 0.5f; // Bottom
vertices[2].z = 0.0f;
vertices[2].rhw = 1.0f;
vertices[2].u = 1.0f;
vertices[2].v = 1.0f;
vertices[3].colour = 0xFFFF00FF;
vertices[3].x = (float) objectPointer->coordinatesPointer()->x - 0.5f; // Left
vertices[3].y = (float) vertices[0].y+mTexturePointer->textureDescPointer()->Height;// - 0.5f; // Bottom
vertices[3].z = 0.0f;
vertices[3].rhw = 1.0f;
vertices[3].u = 0.0f;
vertices[3].v = 1.0f;

if(FAILED(hResult = vertexBuffer->Lock(0,0, (void**)&vertexVoidPointer, 0)))
{
  logWindow.AddLogEntry(3, TEXT("IDirect3DVertexBuffer9::Lock() FAILED"), hResult, GetLastError());
  MessageBox(*mTexturePointer->rendererPointer()->hwndPointer(), TEXT("IDirect3DVertexBuffer9::Lock() failed!  This program will now close."), TEXT("CRITICAL FAILURE"), MB_OK | MB_ICONERROR | MB_TOPMOST);
  CheckCriticalExit();
}
else
  logWindow.AddLogEntry(6, TEXT("IDirect3DVertexBuffer9::Lock() succeeded"), hResult);

memcpy(vertexVoidPointer, vertices.data(), (sizeof(TLVERTEX)*vertices.capacity()));
vertexBuffer->Unlock();
}

I would greatly appreciate any assistance with this issue, I've searched online and found stuff relating to texel-pixel inconsistencies and whatnot. This SOUNDS like my issue, but what I tried to do to compensate for it (based on stuff I read online) does not appear to be doing anything to help.

Edited by Interminable, 21 July 2012 - 05:19 AM.


#13 OppiXP   Members   -  Reputation: 105

Like
0Likes
Like

Posted 24 July 2012 - 03:58 AM

Hi,

try to round the x- and y-coordinates to the nearest integer when translating.

#14 Khatharr   Crossbones+   -  Reputation: 2962

Like
1Likes
Like

Posted 26 July 2012 - 01:03 AM

What kind of filtering are you using?

I'm not sure if filtering applies to the edges of sprite renders but it may be worth looking at, since it's just changing a flag.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#15 Icebone1000   Members   -  Reputation: 1052

Like
0Likes
Like

Posted 26 July 2012 - 03:33 PM

What kind of filtering are you using?

I'm not sure if filtering applies to the edges of sprite renders but it may be worth looking at, since it's just changing a flag.


This. If Im understanding the issue right, thats definitely the problem. In the texture sampler filter, set everything to POINT instead of LINEAR. I remember finding the same "issue" the first time I did my pixel perfect sprite class.

Linear is good if you will rotate the sprite, but for translation it leaves a ghost when the sprite moves/lies across/on coords that arent pixel perfect ( i.e. 1.06 pixels to righ)

#16 Madhed   Crossbones+   -  Reputation: 2786

Like
0Likes
Like

Posted 26 July 2012 - 04:14 PM

If you want pixel perfect sprites you have to make sure that coordinates get mapped exactly to pixels. I think that is the problem you seem to experience.

#17 Interminable   Members   -  Reputation: 127

Like
0Likes
Like

Posted 28 July 2012 - 04:25 AM


What kind of filtering are you using?

I'm not sure if filtering applies to the edges of sprite renders but it may be worth looking at, since it's just changing a flag.

This. If Im understanding the issue right, thats definitely the problem. In the texture sampler filter, set everything to POINT instead of LINEAR. I remember finding the same "issue" the first time I did my pixel perfect sprite class.
Linear is good if you will rotate the sprite, but for translation it leaves a ghost when the sprite moves/lies across/on coords that arent pixel perfect ( i.e. 1.06 pixels to righ)


I just tried doing as you describe with no discernible effect on the issue I am experiencing. Unless I'm doing something wrong.

To write the lines below I used this page for help which I found from Googling what you stated: http://www.toymaker....ler_states.html

If I have misinterpreted your instructions or not carried them out appropriately please let me know.

md3dDevice->SetSamplerState( 1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
md3dDevice->SetSamplerState( 1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
md3dDevice->SetSamplerState( 1, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );
md3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT );
md3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
md3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_POINT );

If you want pixel perfect sprites you have to make sure that coordinates get mapped exactly to pixels. I think that is the problem you seem to experience.


I understand this to most likely be the case, but my last post here was about how it wasn't fixing the issue (or indeed making any noticeable change).

EDIT:



This might help the issue.

http://msdn.microsof...0(v=vs.85).aspx

What the article describes sounds like it could be related to the issue I'm having.
The problem is, I'm not drawing a 2D texture to a surface. It's a standalone sprite. I'm unsure how I can deal with this.

Do you mean you are using the Sprite class?
What you need to do is shift your Projection Matrix to account for the issue. This is how i setup mine and it seems to work. I'm not a DX master by any means but give it a shot and see if it helps. My Coordinate system has 0,0 at the center of the screen and +Y is up.
Device.SetTransform(TransformState.Projection, Matrix.Translation(-0.5f, 0.5f, 0.0f) * Matrix.OrthoLH(_form.ClientSize.Width, _form.ClientSize.Height, 0, 1));


I've finally worked out that the reason I can't find these DirectX functions is because they don't exist, they're part of .NET but I'm not using it, I'm using the pure Win32 API. Do you know how I can replicate what you're doing here using normal DirectX functions?

Having since altered my code so I set the View Matrix and Projection Matrix, I'm currently setting my Projection Matrix in this manner:

D3DXMATRIXA16 projectionMatrix;
D3DXMatrixPerspectiveFovLH(&projectionMatrix, fieldOfView, aspectRatio, closestZLimit, furthestZLimit);
md3dDevice->SetTransform(D3DTS_PROJECTION, &projectionMatrix);

Edited by Interminable, 28 July 2012 - 04:40 AM.


#18 Interminable   Members   -  Reputation: 127

Like
0Likes
Like

Posted 30 July 2012 - 04:28 AM

JWBaker, based on your original post I have eventually managed to put together this which I 'think' is the equivalent of what you put, however this may not be the case as it does not fix the issue unfortunately.

void Renderer::SetProjectionMatrix(float fieldOfView, float aspectRatio, float closestZLimit, float furthestZLimit)
{
D3DXMATRIXA16 projectionMatrix;
//D3DXMatrixPerspectiveFovLH(&projectionMatrix, fieldOfView, aspectRatio, closestZLimit, furthestZLimit);

D3DXMatrixTranslation(&projectionMatrix, -0.5f, 0.5f, 0.0f);
D3DXMatrixOrthoLH(&projectionMatrix, window_width, window_height, 0, 1);
md3dDevice->SetTransform(D3DTS_PROJECTION, &projectionMatrix);
}

Edited by Interminable, 30 July 2012 - 04:32 AM.


#19 DarkRonin   Members   -  Reputation: 604

Like
0Likes
Like

Posted 30 July 2012 - 05:19 AM

Out of interest, is this a windowed application?

#20 Interminable   Members   -  Reputation: 127

Like
0Likes
Like

Posted 30 July 2012 - 05:54 AM

Out of interest, is this a windowed application?


It is indeed a windowed application.




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