Turret target tracking/Missile target tracking[solved]

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
radishlaw
Posts: 6
Joined: Tue Jan 16, 2007 2:25 pm

Turret target tracking/Missile target tracking[solved]

Post by radishlaw »

Hi all,

I have been trying my hands at doing a 3D capital ship battle game in space. Irrlicht has been very useful until I got to the turret/missile part.
I have tried to search for a way to determine which way to turn for a turret/missile to track their target. Unfortunately, it's a lot more difficult than doing such things in 2D and all the method I can find either didn't work or become very strange when the target pass over the turret/missile (maybe it's the dreaded gimble lock?)
I have tried to look for solutions in this forum, GameDev.net and Google, and so far I do not have any code that has the function of "give amount of angle needed to turn to target".
Perhaps someone can be so kind as to enlighten me on this matter. Any example would be most welcome.
Last edited by radishlaw on Sat Jan 20, 2007 6:16 am, edited 1 time in total.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

A simple solution I use for simple steering is to add a FPS camera to the turret...
Then set the target (look at) of the camera at the enemy...
If the turret is a child of the camera, the turret then will also look at the enemy...
And/or you can get the angles of the camera...
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
radishlaw
Posts: 6
Joined: Tue Jan 16, 2007 2:25 pm

Post by radishlaw »

I tried something like this but get very strange results

Code: Select all

irr::core::matrix4 test;
		
GameEngine* game = GameEngine::Instance();

irr::core::matrix4 Rot;		
		
Rot.setRotationDegrees(this->ProjectileNode->getRotation());

core::vector3df up_vec = core::vector3df(0,1,0);

Rot.rotateVect(up_vec);
                           test.buildCameraLookAtMatrixLH(this->ProjectileNode->getAbsolutePosition(),game->GetShip(this->Target_ID)->getPosition(),up_vec);

this->ProjectileNode->setRotation(test.getRotationDegrees());
The projectile just go in the direction with opposite "yaw" (x-axis flipped?)
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

I think you want buildCameraLookAtMatrixRH.

I'm guessing that your ships and projectile are children of the root scene node. If they are not you need to be sure to use getAbsolutePosition() and to undo the rotation from parent to world so that the new rotation is just the rotation from parent to projectile.

Travis
radishlaw
Posts: 6
Joined: Tue Jan 16, 2007 2:25 pm

Post by radishlaw »

I tried 9 methods and 2 finally works
it seems that there were some problem with my code such that
it didn't work before

Code: Select all

//
		//method 7 (Working)
		//http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=18538
		//
		irr::core::matrix4 test;
		
		GameEngine* game = GameEngine::Instance();

		irr::core::matrix4 Rot;

		Rot.setRotationDegrees(this->ProjectileNode->getRotation());
		
		core::vector3df up_vec = core::vector3df(0,1,0);
		
		Rot.transformVect(up_vec);
		test.buildCameraLookAtMatrixLH(this->ProjectileNode->getAbsolutePosition(),game->GetShip(this->Target_ID)->getPosition(),up_vec);
		
		core::vector3df test_Rot = test.getRotationDegrees();		
		
		this->setRotation(core::vector3df(0,0,0));
		this->rotate(core::vector3df(0,0,test_Rot.Z));
		this->rotate(core::vector3df(test_Rot.X,0,0));
		this->rotate(core::vector3df(0,test_Rot.Y,0));

Code: Select all

//periodic change in height
					float Height_Diff = this->getPosition().Y - game->GetShip(this->Target_ID)->getPosition().Y;
					float Dist = sqrt( (this->getPosition().Z - game->GetShip(this->Target_ID)->getPosition().Z) * 
						(this->getPosition().Z - game->GetShip(this->Target_ID)->getPosition().Z)
						+ (this->getPosition().X - game->GetShip(this->Target_ID)->getPosition().X) *
						(this->getPosition().X - game->GetShip(this->Target_ID)->getPosition().X));
					float Y_angle = atanf(Height_Diff / Dist);

					//track hozi
					core::vector3df Difference = game->GetShip(this->Target_ID)->getPosition() - this->getPosition();
					core::vector3df XZ_Angle = Difference.getHorizontalAngle();

					
					this->setRotation(XZ_Angle);
					this->rotate(core::vector3df(0,Y_angle*2,0));
Post Reply