#### Archived

This topic is now archived and is closed to further replies.

# Continued rotation, based on mouse position

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

## Recommended Posts

I've been trying for days to rotate a cube based on the mouse position just like this one: http://images.flashkit.com/movies/3D/Engines/Real-Tim-Ryan_Whe-1937/Real-Tim-Ryan_Whe-1937.swf I can't get it to do a full 360, it just kinda wiggles... I think i have the formula right, but I believe I'm missing something. Formula(?) y1 = oldY * cos(newX) + oldZ * sin(newX) z1 = oldY * -sin(newX) + oldZ * cos(newX) x1 = oldX * cos(newY) + z1 * -sin(newY) z2 = oldX * sin(newX) + z1 * cos(newY) x2 = x1 * cos(-newX) + y1 * sin(-newX) y2 = x1 * -sin(-newX) + y1 * cos(-newX) I'm rendering the cube on a 500x500 form, with the mouse relative to the center. I scaled the mouse position by 1000, so the min/max value is -/+ 0.25. I know I have to track the angle, and update it based on the mouse position, but after looking at the forums and googling I'm still confused.. heres my current code:
//Globals

public float mousePosX = .25f;
public float mousePosY = .25f;
public float oldPosX = .25f;
public float oldPosY = .25f;
public float oldPosZ = .25f;

//-------------------------------------------------------------------------------

public void UpdateMatrices()
{
//Rotate the cube, based on the mouse location

if (oldPosX == 0) 			//Testing

{
oldPosX = mousePosX;
oldPosY = mousePosY;
oldPosZ = -mousePosX;
}

float y1 = (float) (oldPosY * Math.Cos((double) mousePosX) + oldPosZ * Math.Sin((double) mousePosX));
float z1 = (float) (oldPosY * -Math.Sin((double) mousePosX) + oldPosZ * Math.Cos((double) mousePosX));
float x1 = (float) (oldPosX * Math.Cos((double) mousePosY) + z1 * -Math.Sin((double) mousePosY));

float z2 = (float) (oldPosX * Math.Sin((double) mousePosY) + z1 * Math.Cos((double) mousePosY));
float x2 = (float) (x1 * Math.Cos((double) -mousePosX) + y1 * Math.Sin((double) -mousePosX));
float y2 = (float) (x1 * -Math.Sin((double) -mousePosX) + y1 * Math.Cos((double) -mousePosX));

oldPosX = x2;
oldPosY = y2;
oldPosZ = z2;

device.Transform.World = Matrix.RotationYawPitchRoll(x2,y2,-x2);

device.Transform.View = Matrix.LookAtLH( new Vector3( 0.0f, 3.0f,-5.0f ), new Vector3( 0.0f, 0.0f, 0.0f ), new Vector3( 0.0f, 1.0f, 0.0f ) );
device.Transform.Projection = Matrix.PerspectiveFovLH( (float)Math.PI / 4, 1.0f, 1.0f, 100.0f );
}


Its C#, but should be easy to understand. [edited by - _untitled on November 30, 2003 7:05:45 PM]

##### Share on other sites
quote:
Its C#, but should be easy to understand.

What makes you think we wouldn''t be able to understand C#?!

##### Share on other sites
hehe.. didn''t mean it like that

##### Share on other sites
Nevermind, I figured it out.. common sense :S
angleX += mouseX * 0.0002f;angleY += (mouseY)* 0.0002f ;	if (angleX >= 360)	angleX = 0;if (angleY >= 360)	angleY = 0;matX.RotateX(angleX);matY.RotateY(angleY);device.Transform.World = Matrix.Multiply(matY,matX);

[edited by - _untitled on December 2, 2003 9:37:57 PM]

1. 1
Rutin
69
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633421
• Total Posts
3011797
• ### Who's Online (See full list)

There are no registered users currently online

×