Jump to content

[SOLVED] mouse position and cpp orientation


photo

Recommended Posts

Posted

Hi, You have your selection example, that is able to select the cubes in the world. I've managed to waste like 8 hours so far on trying to make it work in my cpp project. I'm almost sure that I'm just messing up all the includes and such (since I am new to the engine and c++ is also a challenge)... Could anyone please guide me towards a working solution?

In the sample, there is this:

Unigine::getPlayerMouseDirection(p0,p1);

which is implemented in utils.h It also uses this:

Player getPlayer()
{
	Player player = engine.editor.getPlayer();
	if (player == NULL)
		player = engine.game.getPlayer();
	return player;
}

But when I shamelessly copy it, "engine" is unknown. I fully expect this to be really newbie question, but I just cannot find it. Your examples are not a buildable project, so I can only check each file separately, without "goto definition/declaration"... I've tried using fulltext search, but that has gotten me only to utils.

When I use Engine::get(); I don't see any "game" in it...

I've actually tried to make it work in C# (because of typehinting - I plan to work like this for a while) and while it still didn't actually work, I've managed to make it at least a buildable code:

		public override bool Init()
		{
			GetMouseDirection(out vec3 p0, out vec3 p1);
			PhysicsIntersection intersection = new PhysicsIntersection();
			var obj = Physics.GetIntersection(p0, p1, ~0, intersection);
			if (obj != null)
			{
				obj.Position.SetX(obj.Position.x + 1);
			}
			return true;
		}

		public void GetMouseDirection(out vec3 p0, out vec3 p1)
		{
			var player = Game.Player;
			int width = App.GetWidth();
			int height = App.GetHeight();
			int mouse_x = App.GetMouseX();
			int mouse_y = App.GetMouseY();
			mat4 projection = player.Projection;
			mat4 imodelview = player.WorldTransform;

			projection.m00 *= (float)height / width;
			float x = -((float)mouse_x / width * 2.0f - 1.0f + projection.m02) / projection.m00;
			float y = ((float)mouse_y / height * 2.0f - 1.0f - projection.m12) / projection.m11;
			if (projection.m32 == 0.0f)
			{
				p0 = imodelview * new vec3(-x, -y, -1.0f);
				p1 = imodelview * new vec3(-x, -y, 1.0f);
			}
			else
			{
				//p0 = imodelview.m03m13m23;
				p0 = imodelview.GetColumn3(3);
				p1 = imodelview * new vec3(x, y, 1.0f);
			}
			vec3 a = new vec3();
			//p1 = p0 - normalize(p1 - p0) * player.ZFar;
			p1 = p0 - (p1 - p0).Normalized * player.ZFar;
		}

Maybe it isn't supposed to be physics intersecion, or I botched m03m13m23 replacement, or something else... but I've ported it in like 20 minutes. My real problem is that I've spent like 6 hours in cpp (only on this issue) :D.

So I have two questions:

  • How do I make cpp example work in my project?
  • More importantly, what is it that I am missing so badly that I cannot make it work? Did I skip something important in the docs, or something else?

And one bonus one: Log works nicely but Is there some onscreen debug message?

Posted

Hi Gonzi,

The code that you tried to copy is written in UnigineScript, so it will not work in C++. engine.game analogue in C++ is #

#include <UnigineGame.h>
#include <UniginePlayers.h>

PlayerPtr player = Game::getPlayer();

Also in C++ you can use getDirectionFromScreen instead of writing your own analogue.

Btw, you can switch between different languages in Documentation at the top right corner:
image.png

How to submit a good bug report
---
FTP server for test scenes and user uploads:

Posted

Thank you very much, I've managed to get it working :)

int AppWorldLogic::update()
{
	if (Input::isKeyDown(Input::KEY_U)) {
		ControlsApp::setMouseHandle(Input::MOUSE_HANDLE_USER);
	}
	if (Input::isKeyDown(Input::KEY_I)) {
		ControlsApp::setMouseHandle(Input::MOUSE_HANDLE_GRAB);
	}
	PlayerPtr player = Game::getPlayer();
	if (player.get() == NULL)
		return 0;
	Vec3 p0 = player->getWorldPosition();
	Vec3 p1 = p0 + Vec3(player->getDirectionFromScreen(App::getMouseX(), App::getMouseY())) * 100;
	WorldIntersectionPtr intersection = WorldIntersection::create();
	auto obj = World::getIntersection(p0, p1, ~0, intersection);
	if (obj) {
		if (Input::isMouseButtonDown(Input::MOUSE_BUTTON_LEFT)) {
			auto pos = intersection->getPoint();
			Log::message("%f %f %f \n\n", pos.x, pos.y, pos.z);
			SpawnMyObject(pos);
		}
	}
	return 1;
}

image.png.0c69709a88a69c9fadb0e2028e4eccfb.png

×
×
  • Create New...