Jump to content

Character Controller Adjustments


SpiderPig
 Share

Recommended Posts

Hi @Josh, a topic of mine for a while has been making my own character controller but with no success.  Using the default controller would be fine, except changing the worlds gravity dons't change the up vector for the controller.  It just falls to the side.

I'd like to have the ability for each controller (and even each physics object) to have it's own gravity direction, or at the very least for the controller to work with world gravity.  Options like this allow for much more diverse range of games that Leadwerks (and Turbo) can make.  :)

 

Test program below;

#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;
Entity* child;
Vec3 p = Vec3(0.0f);

bool App::Start()
{
	window = Leadwerks::Window::Create();
	context = Context::Create(window);

	world = World::Create();
	world->SetGravity(9.8f, 0, 0);

	camera = Camera::Create();
	camera->Move(0, 0, -4);
	camera->SetDebugPhysicsMode(true);

	Light* light = DirectionalLight::Create();
	light->SetRotation(35, 35, 0);

	child = Model::Box();
	child->SetColor(1.0, 0.0, 0.0);
	child->SetPhysicsMode(Entity::CharacterPhysics);
	child->SetMass(1);

	Model* floor = Model::Box();
	floor->SetShape(Shape::Box());
	floor->SetPosition(5, 0, 0);
	floor->SetScale(30, 0.1, 30);
	floor->SetRotation(0,0,45);

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

	Leadwerks::Time::Update();
	world->Update();
	world->Render();

	context->Sync();

	return true;
}

 

TestingGrounds.zip

  • Upvote 1
Link to comment
Share on other sites

  • 2 months later...

Don't mean to bother you @Josh, but do you think getting the character controller to align to the direction it's falling could be in the next update?  I'm hoping to release my game early next year...

What I'm doing is adding a force to each character controller in the direction of gravity.  I just need the controller to align itself to that direction, and behave like normal.

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