Player movement vector on collision
by Jake Callery · in Torque Game Engine · 03/12/2008 (7:59 am) · 7 replies
Hey guys. Lately it seems that I am unable to get my search terms to work for me. So I apologize for the barrage of questions lately. But alas, I have yet another.
Essentially I would like to get the player movement vector when the player collides with an object.
So I would like to get the player movement direction, when the object onCollision callback is tripped, reverse that direction, and apply an impulse.
However I seem to be struggling to get get the players movement direction. I can get the forward facing vector, but this is only useful if the player is moving forward. So this fails when the player is strafing or walking backwards.
Is there a way to get this direction info? There must be, I have to be missing something obvious here.
Thanks yet again for all of the help!
Jake
Essentially I would like to get the player movement vector when the player collides with an object.
So I would like to get the player movement direction, when the object onCollision callback is tripped, reverse that direction, and apply an impulse.
However I seem to be struggling to get get the players movement direction. I can get the forward facing vector, but this is only useful if the player is moving forward. So this fails when the player is strafing or walking backwards.
Is there a way to get this direction info? There must be, I have to be missing something obvious here.
Thanks yet again for all of the help!
Jake
#2
Put this in \engine\game\player.cpp somewhere:
Then, in script inside the onCollision() callback, you can do something like so (of course you'll need a reference to the player object already):
Hope that helps to get you started!
03/12/2008 (11:06 am)
Here's a quick hack I threw together to give you an example (hack as in I have not tested this, but it should work. This is a pretty standard way of doing things, you can look in sceneObject.cpp for more examples)Put this in \engine\game\player.cpp somewhere:
ConsoleMethod( Player, getVelocity, const char*, 2, 2, "Get velocity of player object.")
{
char *returnBuffer = Con::getReturnBuffer(256);
VectorF vel( object->getVelocity() );
dSprintf(returnBuffer,256,"%g %g %g", vel.x, vel.y, vel.z);
return returnBuffer;
}Then, in script inside the onCollision() callback, you can do something like so (of course you'll need a reference to the player object already):
%velocity = %player.getVelocity();
Hope that helps to get you started!
#3
Its amazing how much you learn from the little stuff like this.
Thanks again!
03/12/2008 (12:20 pm)
Ah, thats excellent. Thank you very much. I will try this out tomorrow for sure.Its amazing how much you learn from the little stuff like this.
Thanks again!
#4
Aun.
03/12/2008 (12:51 pm)
You can also use the player's forward vector by%player.getForwardVector();
Aun.
#5
I could be wrong, but I think that getForwardVector() only returns the forward facing vector of the player.
So for instance if you are strafing left or right, then getForwardVector does not give you the players movement vector, just the direction he is facing.
But like I said, I could be wrong.
03/13/2008 (5:23 am)
Thanks for the reply Aun.I could be wrong, but I think that getForwardVector() only returns the forward facing vector of the player.
So for instance if you are strafing left or right, then getForwardVector does not give you the players movement vector, just the direction he is facing.
But like I said, I could be wrong.
#6
But I seem to be having a bit of a different issue. The goal is to knock a player back opposite the direction he was traveling when he touches an object. So I do the following:
It makes the player stutter a bit when the player collides. However it doesn't really knock him back.
I've tried scaling by 1000 instead, and if there is any z amount, then he bounces up very high,
but speed in the x/y direction.
Any thoughts on this?
Thanks again!
03/15/2008 (1:12 pm)
Ok it turns out that getVelocity is already console method for the player. So the tourqeScript you posted will work without modifying the engine. Which is cool.But I seem to be having a bit of a different issue. The goal is to knock a player back opposite the direction he was traveling when he touches an object. So I do the following:
%myVel = %thisPlayer.getVelocity();
%impVec = vectorScale(%myVel, -100);
thisPlayer.applyImpulse("0 0 0", %impVec);It makes the player stutter a bit when the player collides. However it doesn't really knock him back.
I've tried scaling by 1000 instead, and if there is any z amount, then he bounces up very high,
but speed in the x/y direction.
Any thoughts on this?
Thanks again!
#7
04/09/2008 (1:13 am)
I'm pretty new to this so maybe a more seasoned person will shoot me down, but wouldn't multiplying your current velocity by -1 plus your modifier do something like that?
Torque 3D Owner Drew Parker
You'll have to setup a ConsoleMethod to either grab that (or they may be one, not sure off the top of my head, though I don't think so), or, make it available in the onCollision() method.
Good luck!