Using mouse to move
by J. Alan Atherton · in Torque Game Engine · 05/22/2003 (12:09 am) · 11 replies
For the type of game I am creating, the mouse will be used for motion, much like a cursor. I found where the moves are updated inside player->updateMove, and changed things around, so the position of the player is changed by move->yaw and move->pitch. This works, as long as I am pushing w,s,a, or d along with moving the mouse. Just moving the mouse does nothing. I'm guessing this has something to do with an event being sent that updateMove needs to be called, but I can't find what constitutes such an event.
I just want to change the x,y coordinates of the player with the mouse!
I just want to change the x,y coordinates of the player with the mouse!
#2
I'll dig through the resource deeper and see if I can find something related that will make the connection... I appreciate your help.
05/22/2003 (9:51 am)
Alas, I haven't explained myself quite clearly enough. The resource you provided is excellent, though not what I need. I don't mean mouse-based movement in the sense of point and click where you want to go. Instead, I need to have it so you slide the mouse left, and the player strafes left. Slide the mouse forward, and the player moves forward. If you remember, Wolfenstein had the option of controlling like this. It was really a drag to play like that, but that's the idea. My game is more of a puzzle game, and this control method is really the basis of it.I'll dig through the resource deeper and see if I can find something related that will make the connection... I appreciate your help.
#3
instead of w being forward script function you want y axis mouse to call it instead.
05/22/2003 (3:59 pm)
umm couldnt you just do this in the script?instead of w being forward script function you want y axis mouse to call it instead.
#4
05/22/2003 (4:32 pm)
Ron, where is the patch?
#5
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=4198
I just reread the patch info and it is not directly related.
sorry to post mis-information
-Ron
05/22/2003 (4:48 pm)
I was speaking of this one www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=4198
I just reread the patch info and it is not directly related.
sorry to post mis-information
-Ron
#6
05/22/2003 (7:42 pm)
@Badguy: Nope, scripting does not work in this case. I tried. It's because the y-axis is not just an on/off value like the w key, it's a large range of values. That and the y-axis goes forward and back, not just forward. I got the movement to work just fine, the problem is, player doesn't see the mouse movement as an event. Mouse movement is just sent to the camera and some other player routines (head looking, for example), but not the player movement routine. I just need to figure out how to get the player updateMove function to be called when the mouse is moved, just like it is called when you push the w key.
#7
Scripting side of it is fairly simple.
You may need to add the moveMap binds to config.cs as well.
Now, the C++ side is where all the business sits.
moveManager.h
gameConnectionMoves.cc
gameConnectionMoves.cc with the rest of the bunch of mv variables
gameConnectionMoves.cc
gameConnectionMoves.cc
gameConnectionMoves.cc
gameConnectionMoves.cc
WHERE ARE WE
Now, we're all processed, packed up, sent across the network. Now we need to actually use that information to do something to the player. Also, like I said above, I'll explain the S32 and F32 thing. The mouse event handler processes the mouse position each tick. If there is a change in the X or Y position, it processes an event accordingly. The screen coordinates are in U32 format, so when it subtracts before and after, we're always going to end up with an integer, so there's no real reason to use a float; however, the script processes it as a float, so we just need to convert it back. While I'm talking about this, it's important to note, that right out of the box, torque does NOT process NON-MOVEMENT, which makes sense. Who wants to create a mouse event if the mouse isnt even moving? Well.. we do. If we do not process an empty mouse movement, then we'll never know when the mouse stops, so if we move the mouse slightly to the left, then it's going to continually move to the left until we move it to the right, etc. Let's handle that now.
Open winWindow.cc and just replace this function, I left the comments in there so you can undo it
Now on to the player to process the mouse's move events, the easy part. I'm going to use my vehicle class. This is where you come into play, and it's time for you to decide what you want the mouse values to do, and how fast you want them to effect the turning, moving, etc. Here is just an example of how I make my cars turn left and right using my mouse. Now, in either the processTick or the updateMove functions you can access your new mouse move values by looking at move->mouseYaw and move->mousePitch
In vehicle.cc, in the updateMove function
I hope I didnt leave anything out, good luck.
05/24/2003 (2:45 pm)
Ok here goes.Scripting side of it is fairly simple.
function mouseYaw(%val)
{
if(%val){
$mvMouseYaw = %val;
}
else
$mvMouseYaw = 0;
}
moveMap.bind(mouse0, "xaxis", mouseYaw);
function mousePitch(%val)
{
if(%val){
$mvMousePitch = %val;
}
else
$mvMousePitch = 0;
}
moveMap.bind(mouse0, "yaxis", mousePitch);You may need to add the moveMap binds to config.cs as well.
Now, the C++ side is where all the business sits.
moveManager.h
//PAY ATTENTION TO WHERE YOU ADD THESE //Within the Move class U32 id; // sync'd between server & client - debugging tool. U32 sendCount; [b]S32 mouseYaw, mousePitch;[/b] //End the Move Class //Within the MoveManager Class [b]static F32 mMouseYaw; static F32 mMousePitch;[/b] static U32 mTriggerCount[MaxTriggerKeys]; static U32 mPrevTriggerCount[MaxTriggerKeys]; //End MoveManager class
gameConnectionMoves.cc
const Move NullMove =
{
16,16,16,
0,0,0,
0,0,0, // x,y,z
0,0,0, // Yaw, pitch, roll,
0,0,
[b] 0,0, //Mouse Yaw/Pitch[/b]
false,
false,false,false,false,false,false
};gameConnectionMoves.cc with the rest of the bunch of mv variables
[b]
Con::addVariable("mvMouseYaw", TypeF32, &mMouseYaw);
Con::addVariable("mvMousePitch", TypeF32, &mMousePitch);
[/b]gameConnectionMoves.cc
//At the top, these are global variables
[b]
F32 MoveManager::mMouseYaw = 0;
F32 MoveManager::mMousePitch = 0;
[/b]
U32 MoveManager::mTriggerCount[MaxTriggerKeys] = { 0, };
U32 MoveManager::mPrevTriggerCount[MaxTriggerKeys] = { 0, };gameConnectionMoves.cc
// in the function --> bool GameConnection::getNextMove(Move &curMove) // Note we are storing a F32 into an S32, I'll explain why at the end. [b] curMove.mouseYaw = MoveManager::mMouseYaw; curMove.mousePitch = MoveManager::mMousePitch; [/b]
gameConnectionMoves.cc
// in the function -> void Move::pack(BitStream *stream)
// Note, we flip the sign because it doesnt like packing negative numbers
stream->writeFlag(freeLook);
[b]
if(stream->writeFlag(mouseYaw != 0)){
//If our mouse yaw is negative, switch the sign, and send it over
//if it's not negative, then we'll just send the one we have
if( stream->writeFlag(mouseYaw < 0) ){
S32 yawTemp = -mouseYaw;
stream->writeInt(yawTemp, 8);
}
else
stream->writeInt(mouseYaw, 8);
}
if(stream->writeFlag(mousePitch != 0)){
if( stream->writeFlag(mousePitch < 0) ){
S32 pitchTemp = -mousePitch;
stream->writeInt(pitchTemp, 8);
}
else
stream->writeInt(mousePitch, 8);
}
[/b]gameConnectionMoves.cc
// in the function -> void Move::unpack(BitStream *stream)
// Now we just unpack the stuff we just packed
freeLook = stream->readFlag();
[b]
if(stream->readFlag()){
bool neg = stream->readFlag();
mouseYaw = stream->readInt(8);
if(neg)
mouseYaw = -mouseYaw;
}
else
mouseYaw = 0;
if(stream->readFlag()){
bool neg = stream->readFlag();
mousePitch = stream->readInt(8);
if(neg)
mousePitch = -mousePitch;
}
else
mousePitch = 0;
[/b]WHERE ARE WE
Now, we're all processed, packed up, sent across the network. Now we need to actually use that information to do something to the player. Also, like I said above, I'll explain the S32 and F32 thing. The mouse event handler processes the mouse position each tick. If there is a change in the X or Y position, it processes an event accordingly. The screen coordinates are in U32 format, so when it subtracts before and after, we're always going to end up with an integer, so there's no real reason to use a float; however, the script processes it as a float, so we just need to convert it back. While I'm talking about this, it's important to note, that right out of the box, torque does NOT process NON-MOVEMENT, which makes sense. Who wants to create a mouse event if the mouse isnt even moving? Well.. we do. If we do not process an empty mouse movement, then we'll never know when the mouse stops, so if we move the mouse slightly to the left, then it's going to continually move to the left until we move it to the right, etc. Let's handle that now.
Open winWindow.cc and just replace this function, I left the comments in there so you can undo it
//--------------------------------------
static void CheckCursorPos()
{
if(windowLocked && windowActive)
{
POINT mousePos;
GetCursorPos(&mousePos);
RECT r;
GetWindowRect(winState.appWindow, &r);
S32 centerX = (r.right + r.left) >> 1;
S32 centerY = (r.bottom + r.top) >> 1;
//We're going to send empty events down the pipe so the scripts know when the mouse has stopped - Jafa
//if(mousePos.x != centerX)
//{
InputEvent event;
event.deviceInst = 0;
event.deviceType = MouseDeviceType;
event.objType = SI_XAXIS;
event.objInst = 0;
event.action = SI_MOVE;
event.modifier = modifierKeys;
event.ascii = 0;
event.fValue = (mousePos.x - centerX);
Game->postEvent(event);
//}
//if(mousePos.y != centerY)
//{
//InputEvent event;
event.deviceInst = 0;
event.deviceType = MouseDeviceType;
event.objType = SI_YAXIS;
event.objInst = 0;
event.action = SI_MOVE;
event.modifier = modifierKeys;
event.ascii = 0;
event.fValue = (mousePos.y - centerY);
Game->postEvent(event);
//}
SetCursorPos(centerX, centerY);
}
}Now on to the player to process the mouse's move events, the easy part. I'm going to use my vehicle class. This is where you come into play, and it's time for you to decide what you want the mouse values to do, and how fast you want them to effect the turning, moving, etc. Here is just an example of how I make my cars turn left and right using my mouse. Now, in either the processTick or the updateMove functions you can access your new mouse move values by looking at move->mouseYaw and move->mousePitch
In vehicle.cc, in the updateMove function
if (move != &NullMove) {
F32 y = move->yaw;
F32 mouseY = F32(move->mouseYaw);
y += (mouseY / 320.0f);
if(mouseY != 0.0f){
Con::printf("**Received mouse yaw %f", mouseY);
}
if(y)
mSteering.x = mClampF(mSteering.x + y,-mDataBlock->maxSteeringAngle, mDataBlock->maxSteeringAngle);
else
mSteering.x = 0.0f;I hope I didnt leave anything out, good luck.
#8
Wow! Thanks for the tutorial! This is just the kind of thing I've been looking for for quite some time. Not just a hand-fed code dump, but now I'm starting to get a better feel for how torque works. I'm so used to working with my own code or at most a simple engine, but torque is rather large and works in a different way than I'm used to. Thanks again!
05/24/2003 (6:20 pm)
@Jared: Wow! Thanks for the tutorial! This is just the kind of thing I've been looking for for quite some time. Not just a hand-fed code dump, but now I'm starting to get a better feel for how torque works. I'm so used to working with my own code or at most a simple engine, but torque is rather large and works in a different way than I'm used to. Thanks again!
#9
Good luck..
05/24/2003 (9:24 pm)
You're welcome, hope it all works. That's not really a tutorial, but a copy of what I'm using. I process the mouse inputs to move the gun turret on my vehicles, so I hope I didnt miss anything.Good luck..
#10
05/25/2003 (12:42 am)
Well, short quite a few hairs, I finally got it figured out. For the last two hours I couldn't figure out why the mouse events were only being processed when I was holding a movement key down (like my initial problem). Turns out that inside player.cc the updateMove function uses a moveSpeed variable, and if that's zero, it cancels out any other input. The function was still using the old move->x and such instead of the new move->mouseYaw. Once I noticed that it works better than a charm. In that two hours I learned more about Torque than I possibly could have any other way. Sheesh, I traced the whole event loop. It was a rush, dude. So, like, thanks again. Catchya later dude!
#11
Now, once you get something neat going, show us a picture of the day :)
05/25/2003 (8:49 am)
You're welcome, glad it helped. It took me a bit to figure out where the events were created from, but the best thing to do is simply set up the debugger in your software program, and use breakpoints to trace what's happening. Took me a while but I have a fairly good picture of what's happening. Now, once you get something neat going, show us a picture of the day :)
Associate Ron Yacketta