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

## 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 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 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 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
{
public:
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)
{
Position.x=x;
Position.y=y;
Position.z=z;
e1=Vector(1.0f,0,0);
e2=Vector(0,1.0f,0);
e3=Vector(0,0,-1.0f);
SingularMatrix(GLMatrix);
}
~CameraV()
{

}

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;

glMultMatrixf(GLMatrix);
glTranslatef(-Position.x, -Position.y, -Position.z);
}

inline void MoveForward(float step)
{
Position+=e3*step;
}
inline void MoveLeft(float step)
{
Position-=e1*step;
}

inline void MoveUp(float step)
{
Position+=e2*step;
}

inline void MoveTo(Vector v)
{
Position=v;
}

inline void Translate(Vector v)
{
Position+=v;
}

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

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

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

inline void UseVieportData()
{
Frustum.Left*=Viewport.WtoH_ratio;
Frustum.Right*=Viewport.WtoH_ratio;
}

private:
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;

float
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])
{
Matrix[0]=1.0f;
Matrix[5]=1.0f;
Matrix[10]=1.0f;
Matrix[15]=1.0f;

Matrix[1]=Matrix[2]=Matrix[3]=Matrix[4]=
Matrix[6]=Matrix[7]=Matrix[8]=Matrix[9]=
Matrix[11]=Matrix[12]=Matrix[13]=Matrix[14]=0.0f;
}
};

//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);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

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

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

//Clear back buffer
glClear(GL_COLOR_BUFFER_BIT);

//draw world
RysujOsie();
//Draw some stuff
glMatrixMode(GL_MODELVIEW);
//draw actors

SwapBuffers(uchwytDC);

Hope you find anything helpful here :]

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628706
• Total Posts
2984309

• 23
• 10
• 9
• 13
• 13