Paul Thomas Posted July 21, 2023 Share Posted July 21, 2023 Copy and pasted as-is, edit for your purposes, this is a direct recode (with minor additions) of the provided LUA script. #pragma once #include "Leadwerks.h" using namespace Leadwerks; class Spectator { private: // context width int width = 1024; // context height int height = 768; // parent Entity* parent = nullptr; // camera Camera* camera = nullptr; public: Spectator() {} Spectator(int _width, int _height) : width(_width), height(_height) {} Spectator(int _width, int _height, Entity* _parent) : width(_width), height(_height), parent(_parent) {} Spectator(int _width, int _height, Camera* _camera) : width(_width), height(_height), camera(_camera) {} Spectator(int _width, int _height, Camera* _camera, Entity* _parent) : width(_width), height(_height), camera(_camera), parent(_parent) {} void Create() { camera = Camera::Create(); } void Create(Entity* _parent) { parent = _parent; camera = Camera::Create(_parent); } void Create(int _width, int _height) { width = _width; height = _height; camera = Camera::Create(); } void Create(int _width, int _height, Entity* _parent) { width = _width; height = _height; parent = _parent; camera = Camera::Create(_parent); } void Create(int _width, int _height, Camera* _camera) { width = _width; height = _height; camera = _camera; } void SetWidth(int _width) { width = _width; } int GetWidth() { return width; } void SetHeight(int _height) { height = _height; } int GetHeight() { return height; } void SetDimensions(Vec3 dimension) { width = dimension.x; height = dimension.y; } Vec2 GetDimensions() { return Vec2(width, height); } void SetParent(Entity* _parent) { parent = _parent; } Entity* GetParent() { return parent; } void SetCamera(Camera* _camera) { camera = _camera; } Camera* GetCamera() { return camera; } private: bool enabled = true; Vec2 center = Vec2(0.0f, 0.0f); Model* spectator; public: float moveSpeed = 5.0f; float maxSpeed = 50.0f; float speedStep = 5.0f; float moveSmoothing = 0.3f; float radius = 0.5f; float lookSpeed = 0.1f; float lookSmoothing = 0.5f; Vec3 mousePos = Vec3(0.0f, 0.0f, 0.0f); Vec3 cameraRot = Vec3(0.0f, 0.0f, 0.0f); virtual void Initialize() { if (camera == nullptr) { Create(width, height); } center = Vec2(width * 0.5f, height * 0.5f); mousePos = Vec3(center.x, center.y, 0.0f); cameraRot = camera->GetRotation(); spectator = Model::Sphere(8); spectator->SetMass(10.0f); spectator->SetGravityMode(false); spectator->SetBuoyancyMode(false); spectator->SetCollisionType(Collision::Projectile); spectator->SetShadowMode(0); spectator->SetFriction(0.0f, 0.0f); spectator->SetElasticity(0.0f); spectator->SetSweptCollisionMode(true); camera->SetPosition(spectator->GetPosition()); camera->SetParent(spectator); } virtual void Update(Window* window) { if (window == nullptr) return; // debug if (window->KeyDown(Key::F1)) { enabled = !enabled; } if (window->KeyDown(Key::F2)) { camera->SetDebugEntityBoxesMode(!camera->GetDebugEntityBoxesMode()); } if (window->KeyDown(Key::F3)) { camera->SetDebugPhysicsMode(!camera->GetDebugPhysicsMode()); } if (!enabled) return; Vec3 mpos = window->GetMousePosition(); window->SetMousePosition(center.x, center.y); mpos = mpos * lookSmoothing + mousePos * (1 - lookSmoothing); float dx = (mpos.x - center.x) * lookSpeed; float dy = (mpos.y - center.y) * lookSpeed; cameraRot.x = cameraRot.x + dy; cameraRot.y = cameraRot.y + dx; mousePos = mpos; Vec3 move = Vec3(0.0f, 0.0f, 0.0f); if (window->KeyDown(Key::W) || window->KeyDown(Key::Up)) { move.z += moveSpeed; } if (window->KeyDown(Key::S) || window->KeyDown(Key::Down)) { move.z -= moveSpeed; } if (window->KeyDown(Key::D) || window->KeyDown(Key::Right)) { move.x += moveSpeed; } if (window->KeyDown(Key::A) || window->KeyDown(Key::Left)) { move.x -= moveSpeed; } if (window->KeyDown(Key::Q) || window->KeyDown(Key::Space) || window->KeyDown(Key::PageDown)) { move.y += moveSpeed; } if (window->KeyDown(Key::E) || window->KeyDown(Key::ControlKey) || window->KeyDown(Key::PageUp)) { move.y -= moveSpeed; } if (window->KeyDown(Key::Shift)) { moveSpeed = maxSpeed; } else { moveSpeed = 5.0f; } Vec3 velocity = spectator->GetVelocity(false); Vec3 desired = Vec3(move.x, 0.0f, move.z) + Transform::Vector(0.0f, move.y, 0.0f, nullptr, spectator); spectator->AddForce((desired - velocity) * spectator->GetMass() / moveSmoothing, false); spectator->PhysicsSetRotation(cameraRot); } virtual ~Spectator() { delete camera; } virtual void Enable() { enabled = true; } virtual void Disable() { enabled = false; } }; I will edit the class later if needed. Cheers! 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.