DirectX sprite movement seems 'glitchy'

Started by
34 comments, last by Interminable 11 years, 8 months ago
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 tongue.png ).

EDIT: I could have sworn when I edited my previous post that your post was not originally there. My edits explain what happened.
Advertisement
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.
Hi,

try to round the x- and y-coordinates to the nearest integer when translating.
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.

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

[quote name='Khatharr' timestamp='1343286202' post='4963209']
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)
[/quote]

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:


[quote name='Interminable' timestamp='1342209934' post='4958898']
[quote name='JWBaker' timestamp='1342195376' post='4958828']
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.
[/quote]
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));
[/quote]

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);
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);
}
Out of interest, is this a windowed application?

Out of interest, is this a windowed application?


It is indeed a windowed application.

This topic is closed to new replies.

Advertisement