Top Down Shooter, cursor turning issue
by ben calder · in Torque Game Engine Advanced · 12/23/2008 (5:14 am) · 33 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.
MathUtils::getAnglesFromVector
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
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
About the author
#22
Here's the repost of the relevant code:
01/02/2009 (1:56 pm)
I included a fix for the GuiShapeNameHud issue in one of the previous posts I think. If for some reason it isn't working though, let me know. Of course, this is only if you'd like to keep GuiShapeNameHud. All it does it render names over players' heads.Here's the repost of the relevant code:
Quote:
P.S- I forgot to mention a change I made to guiShapeNameHud. Without this change, some mouse events will be lost in top-down mode, as the shapename ctrl covers the entire screen. To fix this, make these changes:
In guiShapeNameHud.cpp, add this in the GuiShapeNameCtrl class:
... virtual void onRender(Point2I offset, const RectI &updateRect); //find this //add everything below void onMouseDown(const GuiEvent &evt) { GuiControl *parent = getParent(); if ( parent ) parent->onMouseDown( evt ); } void onMouseUp(const GuiEvent &evt) { GuiControl *parent = getParent(); if ( parent ) parent->onMouseUp( evt ); } void onMouseMove(const GuiEvent &evt) { GuiControl *parent = getParent(); if ( parent ) parent->onMouseMove( evt ); } void onMouseDragged(const GuiEvent &evt) { GuiControl *parent = getParent(); if ( parent ) parent->onMouseDragged( evt ); } ...
#23
05/06/2009 (7:30 am)
Anyone got a copy of the files mike originaly linked to?
#24
05/06/2009 (7:54 am)
in a few days my 3D isometric kit should be available for purchase from the garage games store which features this code.
#25
05/06/2009 (8:01 am)
@Henry: yeah I've got a copy of it, shoot me an email and I'll send it to you.
#26
05/06/2009 (5:09 pm)
@Tim, Wish I had known that a week ago before I spent it starting from the ground up for a project :P
#28
05/20/2009 (1:02 am)
Hey michael, i dont suppose you could send a copy to me? I would be most appreciative! Ive been scouring the broken search function for something like this for a week or two now.
#29
You could always buy the isometric pack for $50 if you need it immediatley, that pack includes Mike's code, and a few other resources, but it seems like a waste of money to me as you can find the resources used to make the pack around the site.
05/20/2009 (1:51 am)
I have the code on my computer at home, but I wont be able to get it for you for a week or so. If no one gives it to you by then, I will shoot u a copy of it. You could always buy the isometric pack for $50 if you need it immediatley, that pack includes Mike's code, and a few other resources, but it seems like a waste of money to me as you can find the resources used to make the pack around the site.
#30
05/20/2009 (5:43 am)
@Drew: you don't have an e-mail address listed, look on my profile and send me an email and I'll send that to you.
#31
@callum i would, buy it, except you need to have the AFX combopack for 110 first.
05/20/2009 (2:27 pm)
oh wow, my profile is out of date by a couple years. Updated it and sent an email at you micheal, thanks.@callum i would, buy it, except you need to have the AFX combopack for 110 first.
#32
Nevermind, turns out it needs to me in the afxctrl instead.
08/03/2009 (10:19 pm)
Im trying to get this to work with the AFX combo pack. The only major difference i saw was in player.ccp and player.h. But when i get everything compiled, everything stays in the regular yaw mode. Any ideas on what went wrong?Nevermind, turns out it needs to me in the afxctrl instead.
#33
Im going to get this to work, dangit.
08/07/2009 (12:45 am)
Blast. I tried to do a frankenstein combo of the mouse code from the isometric kit to get it to work in the AFX combo pack, but no luck. It doesnt register the movement of the mouse at all now. I had thought i got it working, but it turns out that the code from the isometric kit is only good for one client, and one client only. Any additional clients and CRASH, probably due to conflicting data.Im going to get this to work, dangit.
Torque 3D Owner Greg Barnes
Mad Scratch Games