Jump to content

Transform::Rotation()


SpiderPig
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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.

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

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.

Link to comment
Share on other sites

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.

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

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.

  • Upvote 2
Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...