Jump to content

[1.4.1][KOS] Atmospheric Drag script


Recommended Posts

Right now im still learning the mechanics of KOS, and while doing this i was trying to use air drag to calculate throttle during ascent.
But when i started searching online i coulnd't find any proper scripts to do this, apart from the ones before 1.0.
Everyone was saying that you couldnt calculate it now because we don't know the Cd value of ships anymore. 
But you can actually calculate it on the fly using: Thrust vector + Gravity Vector + Drag vector = total force on ship(Vector).
After puzzling for a while with vectors i managed to come up with this script which returns the drag as the vector AirResVec:

Spoiler

clearscreen.
	
//put this declarations and the functions at the top of your script.	
	set lastVecTime to time:seconds.
	set lastThrust to 0.
	set currThrust to 1.
	set myAcc to V(0,0,0).
	set vesTime to time:seconds.
	set lastVes to V(0,0,0).	
		
function drawVec { //declares a drawing function or later use in loop.
	if lastVecTime < time:seconds - 0.005 {
		clearvecdraws().
		
		vecdraw(V(0,0,0),thrustVec*0.00001,green,"",7,true,0.1).
		vecdraw(V(0,0,0),gravVec *0.00001,purple,"",7,true,0.1).
		vecdraw(V(0,0,0),realForceOnShip*0.00001, blue ,"",7,true,0.1).
		vecdraw(V(0,0,0),AirResVec*0.00001,red,"",7,true,0.1).		
		
		set lastVecTime to time:seconds.
	}
}

function printNumbers { //prints the value of  each of the four vectors to the terminal
	print "Total Force:   " + round(realForceOnShip:mag,2) + "   "at (0,0).
	print "Gravity: " + round(gravVec:mag,2) + "   "at (0,1).
	print "Thrust: " + round(thrustVec:mag,2)+ "         " at (0,2).
	print "Air resistance: " + round(AirResVec:mag , 2)+ "     " at (0,3).
}.
	

//make sure this part is in a loop at the place where you want to calculate the air resistance/drag(might not work without the wait statement or you have to increase your instructions per tick).
until false {
	set g to body:mu / (altitude + body:radius)^2.
	
  	if (time:seconds - vesTime ) > 0 { //For determining true acceleration
		set myAcc to (ship:velocity:surface - lastVes)/(time:seconds - vesTime ).
		set vesTime to time:seconds.
		set lastVes to ship:velocity:surface.
	}
  
	//calculates starting vectors
	set thrustVec to ship:facing:forevector*ship:maxthrust*throttle*1000.
	set gravVec to ship:up:vector *-1*g*ship:mass*1000.
	set accVec to myAcc.
	set realForceOnShip to accVec * ship:mass*1000.
	
	//uses starting vectors to find out the vector for air resistance(AirResVec), wich is the output for this script.
	if(ship:status = "LANDED" or ship:status = "SPLASHED" or ship:status = "PRELAUNCH"){
		set AirResVec to V(0,0,0).
		} else {
		set AirResVec to realForceOnShip  - thrustVec - gravVec.
		}
	
	//Uncomment this to draw all the vectors and see what's happening behind the screens
	//drawVec().
	
	//uncomment this to show the calculated values on the terminal.
	//printNumbers().
	
	wait 0.005.
}.

 

Important notes!!:

  • Doesn't work while landed because the script will think the drag is keeping the craft from falling instead of the ground its standing on(edit: now fixed by adding a failsafe that will return 0 drag if the ship is either landed, not launched yet or splashed down)
  • There is a slight delay when changing the throttle which will cause the air drag vector to freak out for a split second
  • You need an accelerometer on the craft to calculate the (wait for it...) acceleration.(edit: these two issues are now fixed by using the KO's built-in ship:velocity instead of ksp's accelerometer which averages the acceleration from the last 0.4 seconds which caused glitches)

I hope this helps the community since the script wasnt available when i needed it.

 

BTW. here is a pictures of the vectors in action:

Spoiler

PXxcY1E.jpg

Purple = Gravity
Green = thrust
Blue = Sum of forces
Red = Air resistance(upscaled by 3 compared to the rest or it wouldn't be visible because it is a relatively small vector)

 

 

 

 

 

 

 

Edited by Cheetah_stuck_in_space
Fixed some bugs
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

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.

×
×
  • Create New...