Jump to content

GetRotation() returning incorrect Z axis


SpiderPig
 Share

Recommended Posts

When turning an entity, GetRotation() will return correct for a while then the Z axis will pop to zero for a bit before it starts reading properly again.  Below is some test code showing the problem.

On my current project, sometimes the Turn() function will suddenly pop to random values, but I'm unable to reproduce this here yet.

I'm using the latest BETA build.
 

#include "App.h"

using namespace Leadwerks;

App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {}

App::~App() { delete world; delete window; }

Model* box;

bool App::Start()
{
	window = Window::Create("Turn Test",50,50, 1024, 768);
	window->Show();

	context = Context::Create(window);

	world = World::Create();

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

	Camera* camera = Camera::Create();
	
	box = Model::Box();
	box->Move(0, 0, 2);

	return true;
}

bool App::Loop()
{
	if (window->KeyHit(Key::Escape) == true) { return false; }

	box->Turn(0.2, 0.2, 0.2);

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

	context->SetBlendMode(Blend::Alpha);

	std::stringstream ss;
	ss << "rot = " << box->GetRotation().ToString();
	context->DrawShadowText(ss.str(), 20, 20);

	context->Sync(true);

	return true;
}

 

Link to comment
Share on other sites

I tried that and and it seems to do the exact same thing.  With the following code though, GetRotation(false) works but GetRotation(true) dosn't.  I think the global xy & z will vary to the local rotation due to rotation on multiple axis's, but it shouldn't set to zero suddenly then back to some other number should it?

	Vec3 rot = box->GetRotation();
	rot.x += 0.2f;
	rot.y += 0.2f;
	rot.z += 0.2f;
	box->SetRotation(rot);

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

	context->SetBlendMode(Blend::Alpha);

	std::stringstream ss;
	ss << "rot = " << box->GetRotation().ToString();
	context->DrawShadowText(ss.str(), 20, 20);

	ss.str("");
	ss << "rot = " << box->GetRotation(true).ToString();
	context->DrawShadowText(ss.str(), 20, 40);

Test1.thumb.png.e0db7102844bed0e5f066c81cf20fc2c.png

Link to comment
Share on other sites

Euler angles are obtained from the Quaternion as such:

Vector3 Quaternion::GetEulerAngles() const {
	float yy = y * y;
	float t0 = -2.f * (x * z + w * y);
	t0 = t0 > 1.f ? 1.f : t0;
	t0 = t0 < -1.f ? -1.f : t0;

	return Vector3(
		Math::ToDegrees(std::asinf(t0)),
		Math::ToDegrees(std::atan2(2.f * (x * y - w * z), -2.f * (yy + z * z) + 1.f)),
		Math::ToDegrees(std::atan2(2.f * (y * z - w * x), -2.f * (x * x + yy) + 1.f))
	);
}

So yeah, what Josh said. If you just simply incremented pitch, yaw, roll and that was it you'd experience gimble lock.

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