Jump to content

Problems with creating spectator camera with C++


Lostghbear
 Share

Recommended Posts

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::P))

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::D) - 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;

}

Link to comment
Share on other sites

The sphere has inertia because you are adding a force to move it.

 

You could use the SetVelocity() command instead, which will set the speed.

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

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...