Sign in to follow this  

Scale an Object After Rotation With Mouse Movement

This topic is 2631 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

Hello, I want to scale an object in the direction of where my mouse is moving. So far my method works fine, but after I rotate my object and go back to scale it, it seems like my mouse movement won't align of where the object rotated which causes the object scale in odd directions.

Here is how I scale an object when it's not rotated:

Vector2D Dir = Vector2D((x-MouseLastPos.m_X)*0.005, (y-MouseLastPos.m_Y)*0.005);

MyObject.Scalef.m_X += Dir.m_X;
MyObject.Scalef.m_Y += Dir.m_Y;

I pretty much just subtract the current mouse position of where the last mouse position was and add to the scale to the object. So how can I use the rotation value of my 2D object to scale it in the direction I want it to with the mouse? I'm not sure what method this is called.

I hope you all can understand my question, it's pretty late for me at the time being.

Thanks, Andrew.

[Edited by - ajm113 on September 28, 2010 11:24:14 AM]

Share this post

Link to post
Share on other sites
I assume that you scale in Global coordinates system, while rotations are made in local. That may be the reason why it works before you rotate, and after scaling acts strangely.

Share this post

Link to post
Share on other sites
Yes, your correct. Can you explain how I can fix this issue after the object rotates? I was thinking somewhere along the lines how a first person camera kinda works to fix this issue, but everything I thought of doesn't work.

This works, but if you have anything better then this go ahead. The flaw to this is that if the layer is inverted in some way scale wise the mouse movement is going to be inverted as well with the object.

MyObject.Scalef.m_Y += Dir.m_Y + (float)cos(MyObject.Rotation-90) * 0.0005;
MyObject.Scalef.m_X += Dir.m_X + (float)sin(MyObject.Rotation-90) * 0.0005;

[Edited by - ajm113 on September 30, 2010 12:18:10 AM]

Share this post

Link to post
Share on other sites
For example if you store object orientation as three vectors and you rotate them, after this you have to apply the change also to the matrix of your rendering system (DX or OpenGL). Here take a look at my camera class that can rotate and move.

class CameraV //matrix rotations camera
Vector Position, //position in global coordinates system (gcs)
e1,e2,e3; //versors orienting camera in global coordinates system (gcs)
ClassViewport Viewport;
ClassFrustum Frustum;

CameraV(float x=0.0f,float y=0.0f,float z=0.0f)
//ustalenie układu


inline void ApplyChange(void)
GLMatrix[0] = e1.x;
GLMatrix[4] = e1.y;
GLMatrix[8] = e1.z;
GLMatrix[12] = 0.0f;

GLMatrix[1] = e2.x;
GLMatrix[5] = e2.y;
GLMatrix[9] = e2.z;
GLMatrix[13] = 0.0f;

GLMatrix[2] = -e3.x;
GLMatrix[6] = -e3.y;
GLMatrix[10] = -e3.z;
GLMatrix[14] = 0.0f;

GLMatrix[3] = 0.0f;
GLMatrix[7] = 0.0f;
GLMatrix[11] = 0.0f;
GLMatrix[15] = 1.0f;

glTranslatef(-Position.x, -Position.y, -Position.z);

inline void MoveForward(float step)
inline void MoveLeft(float step)

inline void MoveUp(float step)

inline void MoveTo(Vector v)

inline void Translate(Vector v)

inline void RotateYLoc(float AngleDeg)
RotationMatrix(AngleDeg, e2.x, e2.y, e2.z, GLMatrix);
e3=RotateVector(e3, GLMatrix);

inline void RotateXLoc(float AngleDeg)
RotationMatrix(AngleDeg, e1.x, e1.y, e1.z, GLMatrix);
e3=RotateVector(e3, GLMatrix);

inline void RotateZLoc(float AngleDeg)
RotationMatrix(AngleDeg, e3.x, e3.y, e3.z, GLMatrix);
e2=RotateVector(e2, GLMatrix);

inline void UseVieportData()

GLfloat GLMatrix[16];

inline void RotationMatrix(float angle, float x, float y, float z,float mMatrix[16])
if(x == 0.0f && y == 0.0f && z == 0.0f){SingularMatrix(mMatrix); return;}

float vecLength=(float)sqrt( x*x + y*y + z*z ),
sinSave= (float)sin(angle),
cosSave= (float)cos(angle),
oneMinusCos= 1.0f - cosSave;

// Rotation matrix is normalized
x = x/vecLength;
y = y/vecLength;
z = z/vecLength;

xy = x * y,
yz = y * z,
zx = z * x,
xs = x * sinSave,
ys = y * sinSave,
zs = z * sinSave;

mMatrix[0] = (oneMinusCos * x*x) + cosSave;
mMatrix[4] = (oneMinusCos * xy) - zs;
mMatrix[8] = (oneMinusCos * zx) + ys;
mMatrix[12] = 0.0f;

mMatrix[1] = (oneMinusCos * xy) + zs;
mMatrix[5] = (oneMinusCos * y*y) + cosSave;
mMatrix[9] = (oneMinusCos * yz) - xs;
mMatrix[13] = 0.0f;

mMatrix[2] = (oneMinusCos * zx) - ys;
mMatrix[6] = (oneMinusCos * yz) + xs;
mMatrix[10] = (oneMinusCos * z*z) + cosSave;
mMatrix[14] = 0.0f;

mMatrix[3] = 0.0f;
mMatrix[7] = 0.0f;
mMatrix[11] = 0.0f;
mMatrix[15] = 1.0f;

inline Vector RotateVector(const Vector vSrcVector, const float mMatrix[16])
return Vector(
mMatrix[0] * vSrcVector.x + mMatrix[4] * vSrcVector.y + mMatrix[8] * vSrcVector.z,
mMatrix[1] * vSrcVector.x + mMatrix[5] * vSrcVector.y + mMatrix[9] * vSrcVector.z,
mMatrix[2] * vSrcVector.x + mMatrix[6] * vSrcVector.y + mMatrix[10] * vSrcVector.z);

inline void SingularMatrix(float Matrix[16])


//using it in program loop
//c=camera instance
const float wsp=0.2;
if (Shift.Contains(ssShift))
if (Key==VK_UP) c.MoveUp(wsp);
else if (Key==VK_DOWN) c.MoveUp(-wsp);
} else
if (Key==VK_LEFT) c.MoveLeft(wsp);
else if (Key==VK_RIGHT) c.MoveLeft(-wsp);
if (Key==VK_UP) c.MoveForward(wsp);
else if (Key==VK_DOWN) c.MoveForward(-wsp);
if (Key=='W') c.RotateXLoc(1);
else if (Key=='S') c.RotateXLoc(-1);
if (Key=='A') c.RotateYLoc(1);
else if (Key=='D') c.RotateYLoc(-1);
if (Key=='Q') c.RotateZLoc(-1);
else if (Key=='E') c.RotateZLoc(1);

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

//Initialize the viewport
glViewport(0, 0, ClientWidth, ClientHeight);

//Initialize OpenGL projection matrix
glOrtho(0.0f, ClientWidth, ClientHeight, 0.0f, -2.0f, 2.0f);

//Clear back buffer

//draw world
//Draw some stuff
glLoadIdentity( );
//draw actors


Hope you find anything helpful here :]

Share this post

Link to post
Share on other sites

This topic is 2631 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this