Skip to main content
gamedev.net
🔒 Locked

Mouse Movement

Started by DimeBag Apr 28, 2004 at 1:45 PM 1 replies 950+ views
Original Post
DimeBag
DimeBag
I am trying to add mouse movement to my camera class, but when I got to do the rotation, it automaticly puts me in the sky. I use the NeHe basecode for the Win32 accessing. When it gets to the code to access the rendering loop, i have it call layout(). From layout I have the scences all setup based on time, and each scene has its own init() and draw() functions. I want to add mouse rotation to my opening scene, opening.cpp, but after setting the parameters and compiling, the screen zeros in on the sky, all blue. Does it matter I am getting the mouse position outside the Win32 access file. I set my width and height accordingly (640, 480).
main.cpp
********
int WINAPI WinMain(...)
...
layout(hDC);
...
*********
layout.cpp
*********
void layout(HDC hDC)
{
...
drawOpening();
...
***********
opening.cpp
***********
int screenHeight = 480;
int screenWidth = 640;
drawOpening()
{
...
GetInput();
...
   drawscene
...
}
void GetInput()
{
int h = screenHeight >> 1;
int w = screenWidth >> 1;

POINT mousePos;
GetCursorPos(&mousPos);
camera.RotateMouse(mousePos,x, mousePos.y, w, h);
}
******
camera.cpp
******
void Camera::RotateMouse(int mousePosX, int mousePosY, int midX, int midY)
{
	float yDirection = 0.0f;	// Direction angel
	float yRotation  = 0.0f;	// Rotation angle

	// If mouse position is at origin, no rotation needed
	if((mousePosX == midX) && (mousePosY == midY)) return;

	// Get direction
	yDirection = (float)((midX - mousePosX)) / 1000.0f;
	yRotation  = (float)((midY - mousePosY)) / 1000.0f;

	// Keeps camera from rotating too far
	currentRotationAngle -= yRotation;

	// Keeps camera from going too high
	if(currentRotationAngle > 1.0f)
	{
		currentRotationAngle = 1.0f;
		return;
	}

	// Keeps camera from going too low
	if(currentRotationAngle < -1.0f)
	{
		currentRotationAngle = -1.0f;
		return;
	}

	float xAxis = 0, yAxis = 0, zAxis = 0;
	float xDir  = 0, yDir  = 0, zDir  = 0;

	// Get direction of the view
	xDir = xView - xPos;
	yDir = yView - yPos;
	zDir = zView = zPos;

	// Normalize
	float len = 1 / (float)sqrt(xAxis * xAxis + yAxis * yAxis + zAxis * zAxis);
	xAxis *= len;
	yAxis *= len;
	zAxis *= len;

	// Rotate the camera
	RotateCamera(yRotation, xAxis, yAxis, zAxis);
	RotateCamera(yDirection, 0.0f,1.0f,0.0f);
}
 
I think I got everything. All help is appreciated. Thanks, Dimebag
uber_n00b
uber_n00b
If the camera immediately goes to all blue, it may be due to a few things. I have experienced these problems from the following two causes: It is the first second of the program (and therefore there is no FPS so the movements tend to be infinitely large), the second is you are dividing a large number by a small number. You may want to look at other parts of your program, because I don't see anything wrong with your class.

Edit: I'm dumb. Aren't xAxis through zAxis 0 when len is declared?

[edited by - uber_n00b on April 29, 2004 9:51:23 AM]
My fellow Americans I have just signed legislation that outlaws Russia forever. Bombing will commence in five minutes.
Hybrid666
Hybrid666
check out NeHe''s article on a Quaternion camera class, it rocks and gives u true 3D movement with 3 degrees of freedom and no gimbal lock problems..

u might not understand the maths behind sum of it.. hell i dont, but its not really necessary to, check it out from nehes home page / articles

Hybrid

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.