SpiderPig Posted April 3, 2017 Share Posted April 3, 2017 I'm unsure how exactly to use Transform::Rotation in order to rotate my physics object about it's local Y axis. The code below works when the player is upright, but if it rotates 90 degrees on the X axis (lying down basically) it rotates about the global Y. How can I use this to transform to local? float cx = Math::Round(context->GetWidth() / 2); float cy = Math::Round(context->GetHeight() / 2); Vec3 mpos = window->GetMousePosition(); window->SetMousePosition(cx, cy); mpos = mpos * looksmoothing + mousepos * (1 - looksmoothing); float dx = (mpos.x - cx) * lookspeed; float dy = (mpos.y - cy) * lookspeed; Vec3 prot = player->GetModel()->GetRotation(true); prot.y += dx; Vec3 v = Transform::Rotation(prot, NULL, player->GetModel()); player->GetModel()->PhysicsSetRotation(v, 1.0); Thanks. Quote Link to comment Share on other sites More sharing options...
Josh Posted April 4, 2017 Share Posted April 4, 2017 Do this: Vec3 prot = player->GetModel()->GetRotation(false); prot.y += dx; Vec3 v = Transform::Rotation(prot, player->GetModel(), NULL); player->GetModel()->PhysicsSetRotation(v, 1.0); I am also adding an additional "global" argument for convenience. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
SpiderPig Posted April 4, 2017 Author Share Posted April 4, 2017 Thanks Josh, an extra argument would be great. I tried the code but moving the mouse left and right just added to the players rotation so I changed the code from; prot.y += dx; to prot.y = dx; which did help. It gave me a rotation about the Y axis which followed the mouse. But when the player rotates 90 on the X axis and then I use this code in the update loop it goes haywire. Rotating in all manor of directions without stopping. My player is setup like this. Model* model = Model::Cylinder(); model->SetShape(Shape::Cylinder()); model->SetCollisionType(COLLISION::PLAYER); model->SetMass(10.0, 0, -1, 0, 1, 1, 1);//to make it more stable standing up model->SetPosition(Vec3(0, 5, 0)); model->SetRotation(90, 0, 0);//this makes it go hay wire Basically I'm making my own character controller because the player is pulled toward different gravity directions through out the game. So I always want the player to rotate left and right with the mouse no matter what direction is down. Quote Link to comment Share on other sites More sharing options...
Josh Posted April 6, 2017 Share Posted April 6, 2017 You are probably running into Gimbal lock. If you kept a second pivot that you used just to calculate rotations, you could call Entity->Turn() on it, and then get its rotation and use that as the rotation for PhysicsSetRotation(). You should probably use Entity->GetQuaternion() to be sure it works. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
SpiderPig Posted April 6, 2017 Author Share Posted April 6, 2017 Thanks, that did the trick. For those who are interested here's the code I used. float cx = Math::Round(context->GetWidth() / 2); float cy = Math::Round(context->GetHeight() / 2); Vec3 mpos = window->GetMousePosition(); window->SetMousePosition(cx, cy); mpos = mpos * looksmoothing + mousepos * (1 - looksmoothing); float dx = (mpos.x - cx) * lookspeed; float dy = (mpos.y - cy) * lookspeed; Vec3 prot = player->GetRotation(); prot.y = dx; player->SetRotation(prot); Quat phy_rot = player->GetQuaternion(true); player_pivot->PhysicsSetRotation(phy_rot, 1.0); The physics shape is assigned to the pivot, and the model of the player is parented to the pivot. 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.