Jump to content
  • Advertisement
Sign in to follow this  
Predator_MF

Object rotation problem...

This topic is 3739 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm basically making a 2D engine, I've started with DirectX few months ago, so I'm still learning alot of stuff while working with it...I mean, don't get angry with my questions xD My situation - I'm using orthogonal projection in my application, it's set with this code: D3DXMatrixOrthoLH(&m_matProj, width, height, 0.2f, 20.0f); , where width and height are those from my window's size. When I rotate an object 90 degrees, it kinda shrinks it's width with visibly 20-30%, I can't tell exactly how much and what is causing it, I know I see it. I'm not an expert in math, but I think I correctly ordered the calculation of the scaling, rotation and translation matrixes, as I somewhere read it should be S-R-T. So I'm just guessing it's not that code that causes this problem, I must be missing something. This is my code that's calculates the world matrix for every object: float fOffX, fOffY, fScaleX, fScaleY; fScaleX = (float)m_nObjectWidth /m_nScreenWidth; fScaleY = (float)m_nObjectHeight / m_nScreenHeight; fOffX = ( 0 + (float) m_nObjectPosLeft / m_nScreenWidth ) - 0.5; fOffY = ( 0 - (float) m_nObjectPosTop / m_nScreenHeight ) + 0.5; D3DXMatrixTranslation(&m_PositionMatrix, fOffX, fOffY, 1); D3DXMatrixScaling(&m_ScalingMatrix, fScaleX, fScaleY, 0); // scale the quad with same aspect ratio // rotation D3DXMatrixRotationYawPitchRoll(&m_RotateMatrix, m_fObjectRotationX, m_fObjectRotationY, m_fObjectRotationZ); m_WorldMatrix = m_ScalingMatrix * m_RotateMatrix * m_PositionMatrix; I hope someone knows why do I get shrinking when rotating. Thanks,

Share this post


Link to post
Share on other sites
Advertisement
Your explanation of what your seeing is a little vague ('it kinda shrinks it's width"). [smile] Something a bit more technical might be helpful.

However, this looks suspicious:

D3DXMatrixScaling(&m_ScalingMatrix, fScaleX, fScaleY, 0); // scale

Scaling by 0 on the z-axis should give some peculiar results. Maybe try scaling z by 1 instead?

Share this post


Link to post
Share on other sites
I've tried with 1 also, I've read somewhere that when using orthogonal projection, moving (scaling) objects on Z axis doesn't matter, so that's why it's 0 there. I can't give you more precise except a screenshot if that would make things clearer:

http://img258.imageshack.us/my.php?image=d3drotproblemry1.jpg

Both are just 2-triangle meshes, first one is with rotation 0 degrees, second one is close to 45 degrees...

Share this post


Link to post
Share on other sites
Drop that kind of scaling stuff. You've specified the size (inclusive aspect ratio) of the view in the world already with the parameters of D3DXMatrixOrthoLH. No need to do it a second time. If you want to scale your object, then do it equally in both dimensions.

Share this post


Link to post
Share on other sites
What do you mean by "drop.." ? If I don't have scaling (my meshes are 1x1 squares), how to scale something on the screen ? From the vertex buffer ?

Share this post


Link to post
Share on other sites
Quote:
Original post by Predator_MF
What do you mean by "drop.." ? If I don't have scaling (my meshes are 1x1 squares), how to scale something on the screen ? From the vertex buffer ?

The sequence of transformations done are
model-to-world .. world-to-view(camera) .. projection .. device-mapping
Therein the projection and the device-mapping deal with the screen aspect ratio. But you've integrated it also into the model-to-world portion.

You can scale your objects, of course, but it is in general not correct to do screen related scaling before the projection.

Share this post


Link to post
Share on other sites
Think this way:

You have a world. Inside this world there is your camera "looking" along the line-of-sight. So far the camera specifies the world-to-view transformation with its position and orientation.

Furthur, the camera restricts what is seen at the sides and at the top and bottom, e.g. to a view of 1m width and 0.75m height. This is the camera's view, still specified in the world, so it is a kind of "virtual" view. You tell D3D this by invoking D3DXMatrixOrthoLH routine (perhaps you have to give it the half width and height, I don't know ATM).

The aspect ration of the view above is width/height, 4:3 in this case. Internally, the set-up done by D3DXMatrixOrthoLH, scales the visible portion of the world down to fit into a unit cube. It then maps the horizontal and vertical dimensions to fit into the pixels of the screen. Let's assume that the window is 800 by 600 pixels, and hence also shows an aspect ratio of 4:3 (i.e. the same aspect ratio than those given to D3DXMatrixOrthoLH!).

Following this procedure for an object of 0.5m by 0.5m size planar in front of the camera, means that
(1) it is scaled down as part of the projection to 0.5m/1m = 0.5 horizontally and 0.5m/0.75m = 0.667 vertically
and then
(2) mapped to 0.5*800px = 400px horizontally and 0.667*600px = 400px vertically onto the screen.
Voila: As you can see, the square is mapped to a square without any screen related scaling by yourself!

What you have to guarantee is that the aspect ratio given to D3DXMatrixOrthoLH and the aspect ratio of the screen (or window, whereever to display the scene) are identical.

Your attempt, on the other hand, has scaled the object something like:
width of 1m/4=0.25m, so that 0.25m/1m = 0.25, 0.25*800px = 200px
and height of 1m/3 = 0.333m, so that 0.333m/0.75m = .444, 0.444*600px = 266.4px
and that isn't a square anymore. That comes, of course, from the fact that the object itself isn't a square anymore after its scaling.

[Edited by - haegarr on June 30, 2008 2:27:43 PM]

Share this post


Link to post
Share on other sites
Thanks guyz for the replies, I think I got it, I'll try it tomorrow and hopefully I would not have to write in this thread anymore xD

__
Pencheff

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!