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.
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.
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.
//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);
Then set this coordinates for your player
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.