PhysicsSetPosition

This function calculates and adds a force to move the entity to the specified position in one physics step.

Syntax

Parameters

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;
}