Help with finding right position.

Started by
4 comments, last by andyb716 19 years, 7 months ago
Hi, I have a vertexbuffer class that has a OnMouseOver function, and I pass the X, and Y into and compare it to the dimensions of the VertexBuffer. I return TRUE if its within it. But I am having problems. I have tiles in a 800x600 window with 16x12 tiles. It works Ok on the first line but every line after that its off by a little but each row. So by the time I get to the last row I have to mouse over the middle and below to get it to return true? This is my code for positioning things at 0,0

D3DXMATRIX mProjectionMatrix, mViewMatrix, mWorldMatrix, mTextureMatrix;

	D3DXMatrixOrthoOffCenterLH(&mProjectionMatrix, 0.0f, m_ScreenWidth, m_ScreenHeight, 0.0f, 0.0f, 1.0f);
    D3DXMatrixIdentity(&mViewMatrix);
    D3DXMatrixIdentity(&mWorldMatrix);
    D3DXMatrixIdentity(&mTextureMatrix);

    m_pD3DDevice->SetTransform(D3DTS_PROJECTION,     &mProjectionMatrix);
    m_pD3DDevice->SetTransform(D3DTS_VIEW,           &mViewMatrix      );
    m_pD3DDevice->SetTransform(D3DTS_WORLDMATRIX(0), &mWorldMatrix     );
    m_pD3DDevice->SetTransform(D3DTS_TEXTURE0,       &mTextureMatrix   );




And this is my code for positioning the VertexBuffer

BOOL c2DRect::MoveAbsolute(float x, float y, float z){
	m_X = x; 
	m_Y = y; 
	m_Z = z;
	Move = TRUE;
	return TRUE;
}

BOOL c2DRect::Render(){
	m_pD3DDevice->SetStreamSource(0, m_pVB, 0, sizeof(s2DVertex));
	m_pD3DDevice->SetFVF(VERTEXFVF);
	if(Move){
		D3DXMatrixTranslation(&m_matPosition, m_X, m_Y, m_Z);
		Move = FALSE;
	}
	m_pD3DDevice->SetTransform(D3DTS_WORLD, &m_matPosition);
	m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
	return TRUE;
}



And the code for on mouse over


This is to get the mouse cursor.
POINT pt;
	GetCursorPos(&pt);
	ScreenToClient(m_hWnd, &pt);
	m_XPos = pt.x;
	m_YPos = pt.y;

BOOL c2DRect::OnMouseOver(float x, float y){

	if(x >= m_X && x <= m_X + m_Right && y >= m_Y && y <= m_Y + m_Bottom){
		return TRUE;
	}

	return FALSE;

}



Thank you in advance.
Advertisement
Does your window have a border/titlebar and all that? It sounds to me that your m_ScreenWidth and m_ScreenHeight variables are based on the window size, while your mouse coordinates are based on client size. Make sure that when you create your device, your backbuffer is the same size as the client area, not the overall window area.

If this is indeed the case, then what is happening is that your vertices aren't actually identical to pixel coordinates. The backbuffer size is the window size, but is scaled down just a little to fit into the slightly smaller client area. So when you're near the bottom of the window, and it looks like you're, say, 300 pixels down, judging from the objects drawn using vertices, you're actually only 290 pixels down, because a vertex unit is a bit smaller than a pixel unit. So you have keep on going a bit further before your actual pixel value is 300, but it now looks like its 310 or something, thus looking like around the middle of a tile.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
How would I go about setting that? I'm looking in msdn and I see a BackBufferWidth and BackBufferHeight in the D3DPRESENT_PARAMETERS struct. I set that to the GetClientRect() values but I still get the same results. Is this what you mean? Or is it something else that your talking about?
Can anybody enlighten this situation?
anybody?
so nobody knows what this agony user means by setting the backbuffer width and height to the client area? What I've tried so far gives me the same results.
NEVERMIND I didnt use the GetClientRect in my create device function I was still using m_ScreenWidth and m_ScreenHeight. I can't beleive no one said anything as to how stupid I was. Thanks for the help agony.

This topic is closed to new replies.

Advertisement