Jump to content

Mouse Lag in Prefab


reepblue
 Share

Recommended Posts

If you edit the FirstPersonControls component to with the below, then create a prefab using it, the mouse look will be sluggish. I've spent 4 hours last night trying to figure this out and I can confirm this is a bug with the prefab system. If you were to assign this to an entity normally, it'll work fine. 

The Raw mouse code tends to work ok regardless. 

#pragma once
#include "UltraEngine.h"
#include "FirstPersonControls.h"

using namespace UltraEngine;

FirstPersonControls::FirstPersonControls()
{
	name = "FirstPersonControls";
}

void FirstPersonControls::Start()
{
	auto entity = GetEntity();
	entity->SetPhysicsMode(PHYSICS_PLAYER);
	if (entity->GetMass() == 0.0f) entity->SetMass(78);
	entity->SetCollisionType(COLLISION_PLAYER);
	camera = CreateCamera(entity->GetWorld());
	camera->SetPosition(0, eyeheight, 0);
	camera->SetRotation(0, 0, 0);
	camera->SetFov(70);
	currentcameraposition = camera->GetPosition(true);
	freelookrotation = entity->GetRotation(true);
}

void FirstPersonControls::Update()
{
	Vec3 movement;
	float jump = 0;
	bool crouch = false;
	auto entity = GetEntity();
	auto window = ActiveWindow();
	if (window)
	{
		auto cx = Round((float)window->GetFramebuffer()->GetSize().x / 2);
		auto cy = Round((float)window->GetFramebuffer()->GetSize().y / 2);
		auto mpos = window->GetMousePosition();
		window->SetMousePosition(cx, cy);
		auto centerpos = window->GetMousePosition();

		if (freelookstarted)
		{
			float looksmoothing = mousesmoothing; //0.5f;
			float lookspeed = mouselookspeed / 10.0f;

			if (looksmoothing > 0.00f)
			{
				mpos.x = mpos.x * looksmoothing + freelookmousepos.x * (1 - looksmoothing);
				mpos.y = mpos.y * looksmoothing + freelookmousepos.y * (1 - looksmoothing);
			}

			auto dx = (mpos.x - centerpos.x) * lookspeed;
			auto dy = (mpos.y - centerpos.y) * lookspeed;

			freelookrotation.x = freelookrotation.x + dy;
			freelookrotation.y = freelookrotation.y + dx;
			camera->SetRotation(freelookrotation, true);
			freelookmousepos = Vec3(mpos.x, mpos.y);
		}
		else
		{
			freelookstarted = true;
			freelookrotation = camera->GetRotation(true);
			freelookmousepos = Vec3(window->GetMousePosition().x, window->GetMousePosition().y);
		}

		float speed = movespeed;// / 60.0f;
		bool jumpkey = window->KeyHit(KEY_SPACE);
		if (entity->GetAirborne())
		{
			speed *= 0.25f;
		}
		else
		{
			if (window->KeyDown(KEY_SHIFT))
			{
				speed *= 2.0f;
			}
			else if (window->KeyDown(KEY_CONTROL))
			{
				speed *= 0.5f;
			}
			if (jumpkey)
			{
				jump = jumpforce;
			}
		}
		if (window->KeyDown(KEY_D)) movement.x += speed;
		if (window->KeyDown(KEY_A)) movement.x -= speed;
		if (window->KeyDown(KEY_W)) movement.z += speed;
		if (window->KeyDown(KEY_S)) movement.z -= speed;
		if (movement.x != 0.0f and movement.z != 0.0f) movement *= 0.707f;
		if (jump != 0.0f)
		{
			movement.x *= jumplunge;
			if (movement.z > 0.0f) movement.z *= jumplunge;
		}
		crouch = window->KeyDown(KEY_CONTROL);
	}
	entity->SetInput(camera->rotation.y, movement.z, movement.x, jump, crouch);
		
	float eye = eyeheight;
	float y = TransformPoint(currentcameraposition, nullptr, entity).y;
	float h = eye;
	if (y < eye) h = Mix(y, eye, 0.5f);
	currentcameraposition = TransformPoint(0, h, 0, entity, nullptr);
	camera->SetPosition(currentcameraposition, true);
}

//This method will work with simple components
shared_ptr<Component> FirstPersonControls::Copy()
{
	return std::make_shared<FirstPersonControls>(*this);
}

bool FirstPersonControls::Load(table& properties, shared_ptr<Stream> binstream, shared_ptr<Map> scene, const LoadFlags flags)
{
    if (properties["mousesmoothing"].is_number()) mousesmoothing = properties["mousesmoothing"];
    if (properties["mouselookspeed"].is_number()) mouselookspeed = properties["mouselookspeed"];
    if (properties["movespeed"].is_number()) movespeed = properties["movespeed"];
	return true;
}

bool FirstPersonControls::Save(table& properties, shared_ptr<Stream> binstream, shared_ptr<Map> scene, const SaveFlags flags)
{
	properties["mousesmoothing"] = mousesmoothing;
	properties["mouselookspeed"] = mouselookspeed;
	properties["movespeed"] = movespeed;
	return true;
}

 

testplayer.zip

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

I am guessing there is somehow two active components being updated, which causes the mouse to be moved back to the center twice each frame...

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