Jump to content

Help requested with PlayerPersecutor


photo

Recommended Posts

Greetings,

So trying to follow the Documentation to code - and some parts are not working/do not make sense. The issue I am trying to get working right now is a custom player class (handles the camera and movement.)

Here is the code in question,
Several questions that are not clear in the documentation: for the anchor point - do I need to create a node/object FOR the anchor? is it defaulting to the material ball as the anchor?

If this is the case why can I not get the name of the anchor? (it crashes every time I attempt to get the name of the anchor node - which I am assuming at this point either doesn't exist OR is the material ball since its fixated with it... - i.e. I cannot get it away from it)

Rest of questions are part of the comments in the code below:

Player::Player(){
	this->app = Unigine::App::get();
	this->camera = Unigine::PlayerPersecutor::create();
	
	this->camera->setFov(90.0f);
	this->camera->setZNear(0.1f);
	this->camera->setZFar(10000.0f);
	this->camera->setFixed(1); //nothing happens...
	this->camera->setControlled(0); //nothing happens...
	this->camera->setPhiAngle(-45.0f); //See below
				//expecting this to put the camera facing the material ball however the camera
				//is always looking at the ball from the right side no matter if I remove or change
				//this value
	this->camera->setThetaAngle(45.0f); //this does raise/lower the view as expected
	this->camera->setAnchor(Unigine::Math::vec3(5.0f, 5.0f, 1.0f)); //see below
				//anchor point is at 5.000000, 5.000000, 1.000000
				//however, visually no matter the values it STAYs looking at the same
				//point in space
	this->camera->setWorldPosition(Unigine::Math::vec3(5.0f, 5.0f, 5.0f)); //see below
				//position is at: -2.828427, 0.000000, 2.828427
	this->camera->setDistance(5.0f); //this does work
	
	
}

Unigine::PlayerPtr Player::getPlayer(){
	//this function just casts the camera to set the player, in AppWorldLogic.cpp: Unigine::Game::get()->setPlayer( player->getPlayer() );
	this->player = this->camera->getPlayer();
	return this->player;
}

void Player::inputHandling(){
	//Okay - so if 'w' (ascii dec 119) is pressed move forward.
	//THIS message prints out whenever I press the 'w' key, however, it does not move forward at all...
	if(this->app->getKeyState(119)){
		Unigine::Log::message("We should be moving forward...\n");
		this->camera->setWorldPosition( this->camera->getWorldPosition() + Unigine::Math::Vec3(1.0f, 0.0f, 0.0f) );
	}
	
}

Any comments, feedback would be appreciated.

 

Thanks!

Link to comment

Hi Mark,

For the anchor point - do I need to create a node/object FOR the anchor?
PlayerPersecutor follows a point that consists of a "target node" and an offset in its local coordinates that called the "anchor point".
So, the main thing is the "target node". Without it you can't use "anchor point" (PlayerPersecutor::setAnchor() method).
If you don't want to follow a specific object, create a temporary one (for example, NodeDummy) for the PlayerPersecutor.


Is it defaulting to the material ball as the anchor? It crashes every time I attempt to get the name of the anchor node
By default "target node" is equal to nullptr. It doesn't exist.
Set target node via PlayerPersecutor::setTarget() method.

 

Best regards,
Alexander

Link to comment

Hi Mark,

Let's start with the definition of the PlayerPersecutor - it is a flying camera, that follows the target node at the specified distance. Well, actually it follows an anchor point (specified in local coordinates of the target node), so you have to set a node (it is NULL by default) and an anchor point for a persecutor camera (by default it is located at the origin (0,0,0,) ).

The definition means that you control the target node (e.g. with a keyboard or whatever), while the persecutor just follows it. So in your inputHandling() method you should not move your camera, but the target node instead. Take a look at this example.

The angles you specified were ignored, as specifying anchor coordinates resets them to 0, so you should first specify your target node, then the anchor, and after that - angles, distances and so on.

So your initialization code should look somewhat like this:

	PlayerPersecutorPtr camera = PlayerPersecutor::create();

	ball = Editor::get()->getNodeByName("material_ball");
	
	camera->setFov(90.0f);
	camera->setZNear(0.1f);
	camera->setZFar(10000.0f);

	// fixing or PlayerPersecutor to keep its position relative to the anchor point and angles specified
	camera->setFixed(1);

	// disabling controls for the PlayerPersecutor
	camera->setControlled(0);

	// setting a target node to follow
	camera->setTarget(ball);

	// setting the anchor point (in the local coordinates of the target node)
	camera->setAnchor(Unigine::Math::vec3(0.0f, 0.0f, 1.0f));

	// setting Phi and Theta angles so that out camera would have a top view
	camera->setPhiAngle(90.0f);
	camera->setThetaAngle(90.0f);

	// setting maximum and minimum distance from the anchor point
	camera->setMinDistance(1.0f);
	camera->setMaxDistance(1.0f);
	
	// setting our PlayerPersecutor as a current player
	Game::get()->setPlayer(camera->getPlayer());

And in your input processing function you should change the position of the target node, not the camera, as I mentioned before.

I'll make necessary updates to the article to make things clearer!

Hope this helps!

Thank you!

Link to comment
×
×
  • Create New...