Hi, I recently started learning movement with WASD. As of now I learned how to make a mesh move where W & S would move along the X axis, and A & D would move along the Z axis. I also learned how to make it so the mesh would rotate with A & D and W & S moves the mesh where ever it's facing.
Now, so far I would like to learn how to move a mesh depending on camera angle where W & S would move a mesh towards and away the camera along X, Z axis. And A & D would move a mesh to the left or right of the camera, like in many 3rd person games. But sadly I don't know how to do this, I've tried searching for information on google but could't find anything that would help.
So just wondering, how do I get this type of movement to work? Thanks.
wasd movement depending on camera
-
- Posts: 1186
- Joined: Fri Dec 29, 2006 12:04 am
Re: wasd movement depending on camera
I have a camera orbiting a sphere node. This sphere is moved by bullet physics.
In order to roll the sphere left / right relative to the camera lookat direction, i am using this:
Remember to
otherwise it won't work.
I am sure this is not a copy n paste solution for you, but you should get the idea.
In order to roll the sphere left / right relative to the camera lookat direction, i am using this:
Code: Select all
void Player::left() const
{
this->properties.body->activate(true);
irr::core::vector3d<irr::f32> rot(0.0, 0.0, this->speed*game->getElapsed());
this->camera->getAbsoluteTransformation().rotateVect(rot);
const irr::f32 length = rot.getLength();
rot.Y = 0.0;
rot.normalize();
rot.setLength(length);
this->properties.body->applyTorque(btVector3(rot.X, rot.Y, rot.Z));
}
void Player::right() const
{
this->properties.body->activate(true);
irr::core::vector3d<irr::f32> rot(0.0, 0.0, -this->speed*game->getElapsed());
this->camera->getRelativeTransformation().rotateVect(rot);
const irr::f32 length = rot.getLength();
rot.Y = 0.0;
rot.normalize();
rot.setLength(length);
this->properties.body->applyTorque(btVector3(rot.X, rot.Y, rot.Z));
}
Code: Select all
camera->bindTargetAndRotation(true);
I am sure this is not a copy n paste solution for you, but you should get the idea.
"Whoops..."
ericaus better for search on this forum, google not help in this case.
Also for do this you need know basic geometry and in accordance with this set position for yours models.
it`s standart in game movement:
Then set this coordinates for your player
Also for do this you need know basic geometry and in accordance with this set position for yours models.
it`s standart in game movement:
Code: Select all
//fragment of my code
//fragment from my code
#include <math.h> // for sin/cos
#define M_PI 3.14159265358979323846 //also you can find this constant in math.h
core::vector3df CameraRotation = this->camera->getRotation();
//CameraRotation.X = 0; // it`s for me special
bool can_move = false;
float angle = 0;
if(this->receiver->IsKeyHold(irr::KEY_KEY_W))
{
can_move = true;
angle = 0;
if(this->receiver->IsKeyHold(irr::KEY_KEY_A))
angle -= 45;
if(this->receiver->IsKeyHold(irr::KEY_KEY_D))
angle += 45;
}
else if(this->receiver->IsKeyHold(irr::KEY_KEY_S))
{
can_move = true;
angle = 180;
if(this->receiver->IsKeyHold(irr::KEY_KEY_A))
angle += 45;
if(this->receiver->IsKeyHold(irr::KEY_KEY_D))
angle -= 45;
}
if(!can_move)
{
if(this->receiver->IsKeyHold(irr::KEY_KEY_A))
{angle = -90;can_move = true;}
if(this->receiver->IsKeyHold(irr::KEY_KEY_D))
{angle = 90;can_move = true;}
}
if(can_move)
{
PlayerPos.X += REAL_SPEED*((f32)sin((CameraRotation.Y + angle)*M_PI/180) * (180/M_PI));
PlayerPos.Z += REAL_SPEED*((f32)cos((CameraRotation.Y + angle)*M_PI/180) * (180/M_PI));
}
Player->setPosition(PlayerPos);
from Russia with love )))
Ohh, I think I have an understanding of that now. The last part looks similar to how moving a mesh in direction of its rotation works, so instead of getting Y rotation of the mesh its the Y rotation of the camera? Thanks for the info.Serg88 wrote:ericaus better for search on this forum, google not help in this case.
Also for do this you need know basic geometry and in accordance with this set position for yours models.
it`s standart in game movement:
Then set this coordinates for your playerCode: Select all
//fragment of my code //fragment from my code #include <math.h> // for sin/cos #define M_PI 3.14159265358979323846 //also you can find this constant in math.h core::vector3df CameraRotation = this->camera->getRotation(); //CameraRotation.X = 0; // it`s for me special bool can_move = false; float angle = 0; if(this->receiver->IsKeyHold(irr::KEY_KEY_W)) { can_move = true; angle = 0; if(this->receiver->IsKeyHold(irr::KEY_KEY_A)) angle -= 45; if(this->receiver->IsKeyHold(irr::KEY_KEY_D)) angle += 45; } else if(this->receiver->IsKeyHold(irr::KEY_KEY_S)) { can_move = true; angle = 180; if(this->receiver->IsKeyHold(irr::KEY_KEY_A)) angle += 45; if(this->receiver->IsKeyHold(irr::KEY_KEY_D)) angle -= 45; } if(!can_move) { if(this->receiver->IsKeyHold(irr::KEY_KEY_A)) {angle = -90;can_move = true;} if(this->receiver->IsKeyHold(irr::KEY_KEY_D)) {angle = 90;can_move = true;} } if(can_move) { PlayerPos.X += REAL_SPEED*((f32)sin((CameraRotation.Y + angle)*M_PI/180) * (180/M_PI)); PlayerPos.Z += REAL_SPEED*((f32)cos((CameraRotation.Y + angle)*M_PI/180) * (180/M_PI)); } Player->setPosition(PlayerPos);
last 2 lines calculate OC( X - axis) and AC(Z-axis) from this picture
http://en.wikipedia.org/wiki/File:Circle-trig6.svg
this articles can help:
http://en.wikipedia.org/wiki/Cosinus
PS Yes, because my player turns with camera
http://en.wikipedia.org/wiki/File:Circle-trig6.svg
this articles can help:
http://en.wikipedia.org/wiki/Cosinus
PS Yes, because my player turns with camera
from Russia with love )))