How to tell direction in player.cc
by Joel Hargarten · in Torque Game Engine · 07/16/2005 (8:15 pm) · 6 replies
Hey,
In player.cc around line 1590 you'll find the coding statement:
This decides which jump to use based on the player velocity. But the problem with that is that you can have the same velocity going either forwards or backwards, so I need another way to tell which direction the player is moving.
I have re-written this statement to the following:
This is the only thing that is stopping me right now, so if someone could give me the formula to calculate direction in player.cc that would be great. Thanks.
In player.cc around line 1590 you'll find the coding statement:
setActionThread((mVelocity.len() < 0.5)?
PlayerData::StandJumpAnim: PlayerData::JumpAnim, true, false, true);This decides which jump to use based on the player velocity. But the problem with that is that you can have the same velocity going either forwards or backwards, so I need another way to tell which direction the player is moving.
I have re-written this statement to the following:
if( mVelocity.len() < 0.5 )
{
setActionThread(PlayerData::StandJumpAnim, true, false, true);
}
else if(????need calculation to tell direction here????)
{
setActionThread(PlayerData::JumpAnim, true, false, true);//forward jump
}
else if(????need calculation to tell direction here????)
{
setActionThread(PlayerData::BackJumpAnim, true, false, true);//backwards jump
}This is the only thing that is stopping me right now, so if someone could give me the formula to calculate direction in player.cc that would be great. Thanks.
#2
07/17/2005 (4:28 am)
Mmmmm, that could work. However, a player could be going forward, then quickly hit the back button which may cause undesirable effects.
#3
A little messy, but it should work...
- Brett
07/17/2005 (6:44 am)
const U32 DIR_NONE = 0;
const U32 DIR_FWD = 1;
const U32 DIR_BACK = 2;
const U32 DIR_LEFT = 3;
const U32 DIR_RIGHT = 4;
const U32 DIR_FB = 5;
const U32 DIR_LR = 6;
U32 dirMove = DIR_NONE;
// Establish left/right or forward/back
VectorF vel = mVelocity;
vel.normalize();
dirMove = (mFabs(vel.x) == mFabs(vel.y) ? DIR_NONE : (mFabs(vel.x) > mFabs(vel.y) ? DIR_LR : DIR_FB));
if (dirMove == DIR_LR)
{
if (mVelocity.x > 0)
dirMove = DIR_RIGHT;
else
dirMove = DIR_LEFT;
}
else if (dirMove == DIR_FB)
{
if (mVelocity.y > 0)
dirMove = DIR_FWD;
else
dirMove = DIR_BACK;
}A little messy, but it should work...
- Brett
#4
before testing vel. That should rotate vel to Local Object Space.
Also, the check "mFabs(vel.x) == mFabs(vel.y) ? DIR_NONE" will result in a DIR_NONE when the player is moving at a perfectly diagonal angle. Shouldn't moving forward and left, for example, result in something like DIR_FWDLFT instead of DIR_NONE? Or just leave off that check entirely, and let it result in a DIR_FWD? That would make more sense actually, since there's really a very slim chance that x and y will ever be exactly the same.
07/17/2005 (9:47 am)
I'm pretty sure that mVelocity is in World Coordinate Space. That means that mVelocity.y is not your forward velocity, but only your velocity along World Axis Y. I don't have much experience with the Player class, but I know this is true for the Vehicle classes. If I'm correct, Brett, you might want to do this:mWorldToObject.mulV(vel);
before testing vel. That should rotate vel to Local Object Space.
Also, the check "mFabs(vel.x) == mFabs(vel.y) ? DIR_NONE" will result in a DIR_NONE when the player is moving at a perfectly diagonal angle. Shouldn't moving forward and left, for example, result in something like DIR_FWDLFT instead of DIR_NONE? Or just leave off that check entirely, and let it result in a DIR_FWD? That would make more sense actually, since there's really a very slim chance that x and y will ever be exactly the same.
#5
After that just check forward, side, up value to find which combination you have like forward left, up right etc....
I have that code in Player::pickActionAnimation(). And it work.
07/17/2005 (12:29 pm)
Try this it work for me.F32 curMax = 0.1;
VectorF vel;
mWorldToObj.mulV(mVelocity,&vel);
S32 forward = 0, up = 0, side = 0 ;
Point3F F, B, L, R, U, D ;
F.set(0, 1, 0) ; // forward
B.set(0, -1, 0) ; // back
L.set(-1, 0, 0) ; // left
R.set(1, 0, 0) ; // right
U.set(0, 0, 1) ; // Up (z axis)
D.set(0, 0, -1) ; // down (z axis)
if(mDot(vel, F) > curMax)
forward = 1 ;
if(mDot(vel, B) > curMax)
forward = -1 ;
if(mDot(vel, L) > curMax)
side = -1 ;
if(mDot(vel, R) > curMax)
side = 1 ;
if(mDot(vel, U) > curMax)
up = 1 ;
if(mDot(vel, D) > curMax)
up = -1 ;After that just check forward, side, up value to find which combination you have like forward left, up right etc....
I have that code in Player::pickActionAnimation(). And it work.
#6
07/17/2005 (8:44 pm)
Well, thanks to everyone for their help. I got it working and everything looks great. You all have really helped me get past this bump in the road and now I can continue with my project. Thanks again!
Torque Owner Dreamer
Default Studio Name