Hello!
I have problems with creating spectator camera with C++ using this tutorial
sphere moving faster then camera, but this is not main problem.
I dont understand why, but sphere have the inertia. I mean, when i stop pressing 'w' sphere dont stop movement, just slow down a bit. And also she pushes off from the wall and flies.
I cant understand why it hapends! Please, give me advice. There is my code
#include "App.h"
using namespace Leadwerks;
App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {}
App::~App() { delete world; delete window; }
Vec3 camRotation;
Vec2 centerMouse;
Vec2 mouseDef;
Vec3 camMovement;
Vec3 Temp;
float MouseSens;
float strafeM;
float moveM;
float forceM;
bool cameraMode;
bool App::Start()
{
// Создание основных коммпонентов
window = Window::Create("Learning", 0, 0, 1920, 1080, Window::Titlebar);
context = Context::Create(window);
world = World::Create();
camera = Camera::Create();
camera->SetPosition(0, 2, 5);
light = DirectionalLight::Create();
// Определение переменных
MouseSens = 10;
strafeM = 0.4;
moveM = 0.6;
forceM = 1000;
centerMouse = Vec2(context->GetWidth() / 2, context->GetHeight() / 2);
// Создание спектатора
spectator = Model::Sphere(8);
spectator->SetPosition(Vec3(0, 8, 0));
spectator->SetScale(Vec3(5, 5, 5));
Shape* spectatorShape = Shape::Sphere(0, 0, 0, 0, 0, 0, 1, 1, 1);
spectatorShape = Shape::Sphere();
spectator->SetShape(spectatorShape);
spectator->SetMass(1);
spectator->SetGravityMode(false);
spectatorShape->Release();
cameraMode = false;
// Загрузка карты
Map::Load("maps/test.map");
window->HideMouse();
return true;
}
bool App::Loop()
{
Time::Update();
world->Update();
world->Render();
context->Sync(false);
// Возможность закрыть окно
if (window->Closed() || window->KeyHit(Key::Escape))
return false;
// Переключение между режимами камеры
if (window->KeyHit(Key:))
cameraMode = !cameraMode;
// Удерживание указателя мыши по центру
Vec3 CMP = window->GetMousePosition();
mouseDef.x = CMP.x - centerMouse.x;
mouseDef.y = CMP.y - centerMouse.y;
window->SetMousePosition(centerMouse.x, centerMouse.y);
// Вращение камеры
camRotation.x += mouseDef.y / MouseSens;
camRotation.y += mouseDef.x / MouseSens;
camera->SetRotation(camRotation);
//Движение камеры
camMovement.x = (window->KeyDown(Key:) - window->KeyDown(Key::A)) * Time::GetSpeed() * strafeM;
camMovement.y = (window->KeyDown(Key::E) - window->KeyDown(Key::Q)) * Time::GetSpeed() * strafeM;
camMovement.z = (window->KeyDown(Key::W) - window->KeyDown(Key::S)) * Time::GetSpeed() * moveM;
if (cameraMode)
{
camera->SetPosition(spectator->GetPosition());
Vec3 tForce = Transform::Vector(camMovement, camera, 0);
spectator->AddForce(camMovement * forceM);
}
else
{
camera->Move(camMovement);
};
return true;
}