Game Development Community

Player Rotation using the Mouse Cursor

by ben calder · in Torque Game Engine Advanced · 12/24/2008 (9:16 am) · 0 replies

Hi all,

I am trying to make a top down shooter, similar to PrototypeB (www.garagegames.com/mg/snapshot/view.php?qid=1675) But I am having some real difficulty trying to get the cursor functionailty. Basically at the moment I am trying to make it so that the player turns to face the cursor's location on the screen. I should probably mention that I am using AFX, and that I have set the camera in god mode. I have made some progress with this cursor issue, but It doesnt work properly. I have been modifying void afxTSCtrl::onMouseMove(const GuiEvent& evt), the code is shown below.

void afxTSCtrl::onMouseMove(const GuiEvent& evt)
{
	GameConnection *con = GameConnection::getConnectionToServer();
    
    if (con->isFirstPerson())
    {
       return;// 0.0f;
    }

    if (!con->getControlObject())
    {
       return;// 0.0f;
    }

    GuiCanvas* Canvas = getRoot();

    if (Canvas == NULL)
    {
       return;// 0.0f;
    }

    // rotate the player to look at where the mouse pointer is pointing to
	// If in 3rd person
	F32 yaw, pitch;
	Point2I point = evt.mousePoint;
	Point2I size =Canvas->getWindowSize();
	Point2I center = size / 2;

	// get the vector for the mouse pointer from the center of the screen
   MatrixF camTrans = con->getCameraObject()->getRenderTransform();      
		 
   VectorF camF;
   camTrans.getColumn(1, &camF);
   VectorF newVec;

   // Check if camera is straight down or on an angle
   if (mFabs(camF.z) == 1.0f) 
   {
      newVec = VectorF(-(point.y - center.y), -(point.x-center.x), 0.0f);
   }
   else 
   {
      newVec = VectorF(point.x-center.x, -(point.y-center.y), 0.0f);
   }
		
		// get the yaw of the vector
   MathUtils::getAnglesFromVector(newVec, yaw, pitch);

	
   MatrixF conTrans = con->getControlObject()->getWorldTransform();

	// get the current rotation around the Z-axis
	F32 curYaw = conTrans.toEuler().z;
	
   yaw = curYaw + yaw;

	// Check if we are taking the correct way round
    
	if( yaw > M_PI_F )
   {
      yaw -= M_2PI_F;
   }
   else
   if( yaw < -M_PI_F )
   {
      yaw += M_2PI_F;
   }

MathUtils::getAnglesFromVector

}

MathUtils::getAnglesFromVector

void getAnglesFromVector( VectorF &vec, F32 &yawAng, F32 &pitchAng )
{
   yawAng = mAtan( vec.x, vec.y );
   if( yawAng < 0.0f )
      yawAng += M_2PI_F;

   if( mFabs(vec.x) > mFabs(vec.y) )
      pitchAng = mAtan( mFabs(vec.z), mFabs(vec.x) );
   else
      pitchAng = mAtan( mFabs(vec.z), mFabs(vec.y) );
   if( vec.z < 0.0f )
      pitchAng = -pitchAng;
}

I have hit a brick wall with the code, and I really need some help.

There are two main bugs.

Firstly, the player never actually faces the cursor, he always faces slightly above the cursor. so if the mouse is moved directly to the right of the center of the screen, the player turns to face it, but faces a point slightly above the cursor. I am completely lost as to how I can solve this.

The second problem I have, is that when the cursor is very close to the player he doesnt face the cursor, the player will just face the top of the screen.

Any help would be appretiated,

Thanks,

Callum