General Question How to implement Thrust Vectoring?

BruceJohnJennerLawso

Dread Lord of the Idiots
Addon Developer
Joined
Apr 14, 2012
Messages
2,585
Reaction score
0
Points
36
Pretty much what the title says. How exactly can I get a thruster to vector with a users input, and change its exhaust flame to follow it (or is that done automatically?)

I have found some API functions related to changing the direction of a given thruster, but Im mostly unsure about how to handle the users input and pass that to the engine. Any help is appreciated.

:hailprobe:
 

Zatnikitelman

Addon Developer
Addon Developer
Joined
Jan 13, 2008
Messages
2,302
Reaction score
6
Points
38
Location
Atlanta, GA, USA, North America
Here's what I used with the Solar Service Module, this didn't make it into production for what's on OrbitHangar because it's unstable with the autopilots, but can probably give you a starting point. This method should be called from clbkPreStep or clbkPostStep with the deltat passed into it:
Code:
void S_S_M::gimbalengines(double simdt)
{
	if(GetThrusterGroupLevel(THGROUP_MAIN) !=0)
	{
		pitch = GetThrusterGroupLevel(THGROUP_ATT_PITCHUP) - GetThrusterGroupLevel(THGROUP_ATT_PITCHDOWN);
		lpitchangle = pitch*(PI/12);
		rpitchangle = lpitchangle;

		yaw = GetThrusterGroupLevel(THGROUP_ATT_YAWLEFT) - GetThrusterGroupLevel(THGROUP_ATT_YAWRIGHT); //right is positive
		lyawangle = yaw*(PI/12);
		ryawangle = lyawangle;

		rollA = GetThrusterGroupLevel(THGROUP_ATT_BANKRIGHT)- GetThrusterGroupLevel(THGROUP_ATT_BANKLEFT);

		if(rollA != 0)
		{
			if(pitch > 0)
			{
				if(rollA > 0) //roll right
				{
					lpitchangle -= (rollA*lpitchangle);
					//drop left engine;
				}
				if(rollA < 0) //roll left
				{
					rpitchangle += (rollA*rpitchangle);
					//drop right engine;
				}
			}
			else if(pitch < 0)
			{
				if(rollA > 0) //roll right
				{
					rpitchangle -= (rollA*rpitchangle);
					//raise right engine;
				}
				if(rollA < 0) //roll left
				{
					lpitchangle += (rollA*lpitchangle);
					//raise left engine;
				}
			}
			else
			{
				lpitchangle = -rollA*(PI/12);
				rpitchangle = rollA*(PI/12);
			}
		}
		if(pitch!=0 || yaw!=0 || rollA !=0)
		{
			gimf = simdt * GIMBAL_SPEED;

			if(lcurrentpangle - lpitchangle > 0)
			{
				lpitchangle = lcurrentpangle - gimf;
			}
			if(lcurrentpangle - lpitchangle < 0)
			{
				lpitchangle = lcurrentpangle + gimf;
			}
			if(rcurrentpangle - rpitchangle > 0)
			{
				rpitchangle = rcurrentpangle - gimf;
			}
			if(rcurrentpangle - rpitchangle < 0)
			{
				rpitchangle = rcurrentpangle + gimf;
			}

			if(lcurrentyangle - lyawangle > 0)
			{
				lyawangle = lcurrentyangle - gimf;
			}
			if(lcurrentyangle - lyawangle < 0)
			{
				lyawangle = lcurrentyangle + gimf;
			}
			if(rcurrentyangle - ryawangle > 0)
			{
				ryawangle = rcurrentyangle - gimf;
			}
			if(rcurrentyangle - ryawangle < 0)
			{
				ryawangle = rcurrentyangle + gimf;
			}

			SetThrusterDir(th_main[0],_V(sin(lyawangle)*cos(lpitchangle),-sin(lpitchangle),cos(lyawangle)*cos(lpitchangle)));//left
			SetThrusterDir(th_main[1],_V(sin(ryawangle)*cos(rpitchangle),-sin(rpitchangle),cos(ryawangle)*cos(rpitchangle)));//right
		}
		else
		{
			SetThrusterDir(th_main[0],_V(0,0,1));
			SetThrusterDir(th_main[1],_V(0,0,1));
		}
		lcurrentpangle = lpitchangle;
		rcurrentpangle = rpitchangle;
		lcurrentyangle = lyawangle;
		rcurrentyangle = ryawangle;
	}
}
I could probably refine it a bit better now since that was created when I was still a novice coder. However, it relies on active thrusters rather than simply user input. Possibly you could use dummy thrustless thrusters to deal with that. Hopefully this can get you started, if you have any questions, don't hesitate to ask!
 

fred18

Addon Developer
Addon Developer
Donator
Joined
Feb 2, 2012
Messages
1,667
Reaction score
104
Points
78
I thought about it in Jarvis DLL, and did some research. Actually I found out that thrust vectoring physics will make impossible the use of any MFD for autopilots and should have required a dedicated control system for the built in autopilot. So I thought about having basically a "fake system" only for eye candy that would have turned the nozzles and relevant exhausts, but then i noticed that the gimbaling of the engine if properly calculated is a matter of only a few decimals of degree usually, so it's barely noticeable. I think that the implementation is really hard compared with what you get in the end
 

Hlynkacg

Aspiring rocket scientist
Addon Developer
Tutorial Publisher
Donator
Joined
Dec 27, 2010
Messages
1,870
Reaction score
3
Points
0
Location
San Diego
AAPO's CSM does this for the SPS if you want to check my code samples.

Otherwise here is the simplest way...

Code:
	// Engine Gimbal and CG balancing
	VECTOR3 cgdir, cgpos, enginepos;
	GetSuperstructureCG (cgpos);														// The position of combined CG of the stage and any docked modules in vessel frame
	GetThrusterRef (th_main, enginepos);												// The position of main engine in vessel frame
	cgdir = _V(cgpos.x - enginepos.x, cgpos.y - enginepos.y, cgpos.z - enginepos.z);	// Get the vector between them...  
	VECTOR3 thrustdir = -cgdir;
	normalise (thrustdir);		

	// Restrict X and Y to valid range
	if (thrustdir.y > sin (DISC_GIMBAL_RANGE)) thrustdir.y = sin (DISC_GIMBAL_RANGE);  
	else if (thrustdir.y < sin (-DISC_GIMBAL_RANGE)) thrustdir.y = sin (-DISC_GIMBAL_RANGE); 

	if (thrustdir.x > sin (DISC_GIMBAL_RANGE)) thrustdir.x = sin (DISC_GIMBAL_RANGE);  
	else if (thrustdir.x < sin (-DISC_GIMBAL_RANGE)) thrustdir.x = sin (-DISC_GIMBAL_RANGE);  

// Apply change
	normalise (thrustdir);
	SetThrusterDir (th_main, thrustdir);
 

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
9,286
Reaction score
3,254
Points
203
Location
Toulouse
I managed to code it and even had it working once upon a time. I'll see if I can dig that old project out.
 
Top