Float to Pixel

Started by
16 comments, last by vs322 15 years, 7 months ago
Hey guys this is my first post! Recently I had the same problem as someone known as WinRad - How to convert a world space value to a screen space one, but by reading the replies figured that out. Suprisingly though I also had the size problem same as WinRad, which was answered by someone. Although I understood it my function still messed up and I'm hoping you pros could take a look ---> RECT ParticleSystem::GetParticleRect(D3DXVECTOR3 GPRPos, FLOAT GPRSize) { INT Left, Top, Right, Bottom; RECT ParticlePosition; D3DXMATRIX View, Projection, Identity; D3DVIEWPORT9 VP; D3DXVECTOR3 ParticlePos, TempSize1, ActualSize; GameInformation.D3DDevice-&gt;GetTransform(D3DTS_VIEW, &View); GameInformation.D3DDevice-&gt;GetTransform(D3DTS_PROJECTION, &Projection); GameInformation.D3DDevice-&gt;GetViewport(&VP); D3DXMatrixIdentity(&Identity); D3DXVec3Project(&ParticlePos, &GPRPos, &VP, &Projection, &View, &Identity); D3DXVec3Project(&TempSize1, &D3DXVECTOR3(GPRPos.x + GPRSize, GPRPos.y + GPRSize, 0.0f), &VP, &Projection, &View, &Identity); ActualSize.x = (TempSize1.x - ParticlePos.x); ActualSize.y = (TempSize1.y - ParticlePos.y); Left = ParticlePos.x - ActualSize.x; Top = ParticlePos.y - ActualSize.y; Right = ParticlePos.x + ActualSize.x; Bottom = ParticlePos.y + ActualSize.y; SetRect(&ParticlePosition, Left, Top, Right, Bottom); return ParticlePosition; } </code> Basicly it should work out the borders of my particle yet it doesn't. What happens instead is the it works out the centre of my particle so left, top, right and bottom are equal to the centre. Thanks!!!
Advertisement
Guys please help out I really need to know.
I was thinking about this post yesterday but couldn't come to a conclusion worth posting.

Part of me thinks your approach is mathematically flawed as the shape is view dependent, and whilst that is desirable I wondered if you had to handle it in a different way...

You can fairly easily break down the D3DXVec3Project() operation into component matrix operations and step through it. I'd highly recommend you do some step-through debugging of known test data and identity matrices and go back to first principles. It should become clear which operation or which bit of data is not doing what you expect it to.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Hard to say from your code. I'd suggest running your program in debug, and verifying that GPRSize is non-zero.
Thanks for the replies, currently I do get the correct results for the centre of the particle, but when it comes to the size it just ain't workin' for me... so I think the problem may be with the GPRSize variablr... I'll check it when I get home and tell you guys my report.

Well I tested the variable and it seems that the variable is being passed the correct value... but there was a problem and it was that the value ended being negative so I just reversed it and it started working but yet there is another problem; the rectangle around the circle is too small for some reason so I multiplicated the value for it by two... but I think there is something wrong and I just don't get it. Here's the new code --->

RECT ParticleSystem::GetParticleRect(D3DXVECTOR3 GPRPos, FLOAT GPRSize){ INT Left, Top, Right, Bottom; RECT ParticlePosition; D3DXMATRIX View, Projection, Identity; D3DVIEWPORT9 VP; D3DXVECTOR3 ParticlePos, TempSize1, ActualSize; GameInformation.D3DDevice->GetTransform(D3DTS_VIEW, &View); GameInformation.D3DDevice->GetTransform(D3DTS_PROJECTION, &Projection); GameInformation.D3DDevice->GetViewport(&VP);  D3DXMatrixIdentity(&Identity); D3DXVec3Project(&ParticlePos, &GPRPos, &VP, &Projection, &View, &Identity); D3DXVec3Project(&TempSize1, &D3DXVECTOR3(GPRPos.x + GPRSize, GPRPos.y + GPRSize, 0.0f), &VP, &Projection, &View, &Identity); ActualSize.x = -(TempSize1.x - ParticlePos.x); //Reversed ActualSize.y = -(TempSize1.y - ParticlePos.y); //Reversed Left = ParticlePos.x - ActualSize.x * 2;   //Temporarily the Top = ParticlePos.y - ActualSize.y * 2;    //values have been multiplicated Right = ParticlePos.x + ActualSize.x * 2;  //as the rectangle is just too  Bottom = ParticlePos.y + ActualSize.y * 2; //small... SetRect(&ParticlePosition, Left, Top, Right, Bottom); return ParticlePosition;}


Any ideas on what went wrong?

Thanks.

[Edited by - SlickKobra on August 22, 2008 9:57:19 PM]
LEFT, TOP, RIGHT, and BOTTOM are integers. If the particle size is very small (less than a pixel), it'll be rounded down to zero. Try making float versions of those and checking them out in a debugger.
You mind me tellin' you that I love you? Hehe joking :).

It seems so obvious now! I'll go home and use D3DXVECTOR4(from memory) for the rectangle instead of a RECT struct. It will probably work though.

Unless I reply again - This thread is resolved.
Unblievable! For some reason I'm basically getting the same results! Well here's the updated code and yet I still don't know what's wrong :( -

D3DXVECTOR4 ParticleSystem::GetParticleRect(D3DXVECTOR3 GPRPos, FLOAT GPRSize){ D3DVIEWPORT9 VP; D3DXMATRIX View, Projection, Identity; D3DXVECTOR3 ParticlePos, TempSize1, ActualSize; D3DXVECTOR4 ParticleRectangle; FLOAT Left, Top, Right, Bottom; GameInformation.D3DDevice->GetTransform(D3DTS_VIEW, &View); GameInformation.D3DDevice->GetTransform(D3DTS_PROJECTION, &Projection); GameInformation.D3DDevice->GetViewport(&VP);  D3DXMatrixIdentity(&Identity); D3DXVec3Project(&ParticlePos, &GPRPos, &VP, &Projection, &View, &Identity); D3DXVec3Project(&TempSize1, &D3DXVECTOR3(GPRPos.x + GPRSize, GPRPos.y + GPRSize, 0.0f), &VP, &Projection, &View, &Identity); ActualSize.x = -(TempSize1.x - ParticlePos.x); ActualSize.y = -(TempSize1.y - ParticlePos.y); Left = ParticlePos.x - ActualSize.x; Top = ParticlePos.y - ActualSize.y; Right = ParticlePos.x + ActualSize.x; Bottom = ParticlePos.y + ActualSize.y; ParticleRectangle.w = Left; ParticleRectangle.x = Top; ParticleRectangle.y = Right; ParticleRectangle.z = Bottom; return ParticleRectangle;}


If you even have the slightest of clues on what may be wrong then please say so.

Thanks :(.
Guys isn't there anything wrong with the code? I mean its throwing me false values everywhere... Can someone please just tell me what is wrong with the code? Or please make a function of your own that takes a central position of a particle and size and then returns a D3DXVECTOR4 variable which is the boundary of the particle.

The central position(where the particle is) variable = D3DXVECTOR3 Pos.
The size of the particle = FLOAT Size;

Thanks you!

This topic is closed to new replies.

Advertisement