SpiderPig Posted July 27, 2018 Share Posted July 27, 2018 Here's my attempt so far at making my own character controller. The reason for this is because I'm using multiple gravity directions in the same world and the current controller doesn't like gravity in any direction other than down the y axis. The problem I'm having is getting the controller to stay constrained to the up axis, when you move it with speed (shift key) you'll see that it drags across the terrain and bounces around. I'm not sure how to correctly use the joints settings to stop this kind of movement. Any help is appreciated. #include "App.h" using namespace Leadwerks; App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {} App::~App() { delete world; delete window; } float lookspeed = 0.1, looksmoothing = 0.5; Vec3 mousepos; float jointpos = 1; bool wireframe = false; Joint* joint; Entity* parent; Joint* k = nullptr; Entity* child; bool App::Start() { window = Leadwerks::Window::Create(); context = Context::Create(window); world = World::Create(); camera = Camera::Create(); camera->Move(0, 0, -4); Light* light = DirectionalLight::Create(); light->SetRotation(35, 35, 0); parent = Model::Box(); parent->SetColor(0.0, 0.0, 1.0); parent->SetMass(1); parent->SetGravityMode(false); child = Model::Box(); child->SetColor(1.0, 0.0, 0.0); child->SetShape(Shape::Box()); child->SetMass(1); child->SetGravityMode(true); k = Joint::Kinematic(0, 0, 0, parent); joint = Joint::Slider(0, 0, 0, 0, 1, 0, child, parent); joint->EnableLimits(); joint->SetLimits(-5, 5); joint->SetMotorSpeed(100); Model* mdl = Model::Load("Models\\Terrain.mdl"); mdl->SetScale(20, 20, 20); mdl->SetPosition(0, -3, 0); Shape* shp = Shape::PolyMesh(mdl->GetSurface(0)); mdl->SetShape(shp); mousepos = window->GetMousePosition(); window->SetMousePosition(context->GetWidth() / 2, context->GetHeight() / 2); return true; } bool App::Loop() { if (window->Closed() || window->KeyDown(Key::Escape)) return false; if (window->KeyHit(Key::F3) == true) { camera->GetDebugPhysicsMode() == true ? camera->SetDebugPhysicsMode(false) : camera->SetDebugPhysicsMode(true); } if (window->KeyHit(Key::F4) == true) { camera->GetDebugEntityBoxesMode() == true ? camera->SetDebugEntityBoxesMode(false) : camera->SetDebugEntityBoxesMode(true); } if (window->KeyHit(Key::F2) == true) { if (wireframe == true) { camera->SetDrawMode(0); wireframe = false; } else { camera->SetDrawMode(2); wireframe = true; } } float cx = Math::Round(context->GetWidth() / 2); float cy = Math::Round(context->GetHeight() / 2); Vec3 mpos = window->GetMousePosition(); window->SetMousePosition(cx, cy); mpos = mpos * looksmoothing + mousepos * (1 - looksmoothing); float dx = (mpos.x - cx) * lookspeed; float dy = (mpos.y - cy) * lookspeed; Vec3 camrot = camera->GetRotation(); camrot.x += dy; camrot.y += dx; camera->SetRotation(camrot); mousepos = mpos; float _time = Time::GetSpeed(); float camspeed = 0.2f * _time; if (window->KeyDown(Key::Shift) == true) { camspeed = camspeed * 5.0f; } if (window->KeyDown(Key::W) == true) { camera->Move(0, 0, camspeed); } else if (window->KeyDown(Key::S) == true) { camera->Move(0, 0, -camspeed); } if (window->KeyDown(Key::A) == true) { camera->Move(-camspeed, 0, 0); } else if (window->KeyDown(Key::D) == true) { camera->Move(camspeed, 0, 0); } if (window->KeyDown(Key::T) == true) { camera->Move(0, camspeed, 0); } else if (window->KeyDown(Key::G) == true) { camera->Move(0, -camspeed, 0); } Vec3 p = parent->GetPosition(true); Vec3 r = parent->GetRotation(true); float speed = 0.1f; if (window->KeyDown(Key::Shift) == true) { speed = 1.0f; } if (window->KeyDown(Key::Up)) { k->SetTargetPosition(p.x, p.y, p.z + speed); } if (window->KeyDown(Key::Down)) { k->SetTargetPosition(p.x, p.y, p.z - speed); } if (window->KeyDown(Key::Left)) { k->SetTargetPosition(p.x - speed, p.y, p.z); } if (window->KeyDown(Key::Right)) { k->SetTargetPosition(p.x + speed, p.y, p.z); } //Jumping...? joint->SetAngle(jointpos); if (joint->MotorEnabled() == true) { joint->DisableMotor(); } if (window->KeyHit(Key::Space)) { joint->EnableMotor(); } Leadwerks::Time::Update(); world->Update(); world->Render(); context->SetBlendMode(Blend::Alpha); context->DrawText("Target position: " + String(jointpos), 0, 0); context->DrawText("Current position: " + String(joint->GetAngle()), 0, 20); context->DrawText("Motor enabled: " + String(joint->MotorEnabled()), 0, 40); context->SetBlendMode(Blend::Solid); context->Sync(); return true; } Other links; https://www.leadwerks.com/community/topic/17437-physics-constraints/ Project.zip Quote Link to comment Share on other sites More sharing options...
Josh Posted July 29, 2018 Share Posted July 29, 2018 You could use a kinematic joint to handle the rotation. Quote 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 More sharing options...
SpiderPig Posted July 29, 2018 Author Share Posted July 29, 2018 I've got a kinematic joint to position the sliders parent, but I'm not sure how SetFriction() is meant to stop any rotation from occurring. According to the docs I thought angular friction would be set to 0 to stop any rotation but that didn't seem to work. I think it's because I'm using a slider and am letting gravity pull it down till it collides with the terrain. Then as I move the sliders parent with the kinematic joint the sliders child drags and causes the parent to rotate not matter what the friction setting is. Can the slider be parented to the kinematic joint? Or is this an incorrect use of the slider...? 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.