PhysicsSetRotation
This function calculates and adds torque to rotate the entity to the specified rotation in one physics step.
Syntax
- void PhysicsSetRotation(const float pitch, const float yaw, const float roll, const float strength=0.5)
- void PhysicsSetRotation(const Vec3& rotation, const float strength=0.5)
- void PhysicsSetRotation(const Quat& rotation, const float strength=0.5)
Parameters
- pitch: the X component of the rotation to move to.
- yaw: the Y component of the rotation to move to.
- roll: the Z component of the rotation to move to.
- strength: a multiplier to determine how strong the torque to rotate the entity should be. This should be 1.0, or a little less than 1.0.
Example
#include "Leadwerks.h"
using namespace Leadwerks;
Model* model = NULL;
Vec3 pos;
Vec3 rot;
Vec3 mousepos;
int main(int argc, const char *argv[])
{
Leadwerks::Window* window = Leadwerks::Window::Create();
Context* context = Context::Create(window);
World* world = World::Create();
Camera* camera = Camera::Create();
camera->Move(0, 1, -5);
Light* light = DirectionalLight::Create();
light->SetRotation(35, 35, 0);
//Create a model
model = Model::Box(1, 1, 1);
model->SetColor(0.0, 0.0, 1.0);
model->SetMass(1);
//Add a physics shape
Shape* shape = Shape::Box();
model->SetShape(shape);
mousepos = window->GetMousePosition();
rot = Vec3(45, 0, 0);
while (true)
{
if (window->Closed() || window->KeyDown(Key::Escape)) return false;
mousepos = window->GetMousePosition();
mousepos.z = 5;
pos = camera->UnProject(mousepos);
float turnspeed = 2;
if (window->KeyDown(Key::Up)) rot.x += turnspeed*Leadwerks::Time::GetSpeed();
if (window->KeyDown(Key::Down)) rot.x -= turnspeed*Leadwerks::Time::GetSpeed();
if (window->KeyDown(Key::Right)) rot.y += turnspeed*Leadwerks::Time::GetSpeed();
if (window->KeyDown(Key::Left)) rot.y -= turnspeed*Leadwerks::Time::GetSpeed();
model->PhysicsSetPosition(pos.x, pos.y, pos.z, 0.5);
model->PhysicsSetRotation(rot.x, rot.y, rot.z, 0.5);
Leadwerks::Time::Update();
world->Update();
world->Render();
context->Sync();
context->SetBlendMode(Blend::Alpha);
context->DrawText("Mouse moves the object", 2, 2);
context->DrawText("Arrow keys turn the object", 2, 2);
}
return 0;
}