KontrolSystem2 icon indicating copy to clipboard operation
KontrolSystem2 copied to clipboard

QUESTION about debug vector

Open PhilouDS opened this issue 1 year ago • 5 comments
trafficstars

I tried to use DEBUG.add_vector to draw a vector from my plane to the runway. But I probably don't understand how GlobalPosition and GlobalVector work. I can't have what I want. Here is my code:

// KONTROL SYSTEM 2 - v0.5.6.0
use { yield } from ksp::game
use { CONSOLE, RED } from ksp::console
use { vec3, vec2 } from ksp::math
use { Vessel } from ksp::vessel
use { GeoCoordinates } from ksp::orbit
use { DEBUG } from ksp::debug

pub fn main_flight(vessel: Vessel) -> Unit = {
	
	const landing_height = vessel.main_body.terrain_height(-0.64, -75.02)
	const landing_point = vessel.main_body.global_surface_position(-0.63, -75.02, landing_height)
	
	const vessel_pos = Cell(vessel.global_position)
	const landing_pos = Cell(landing_point - vessel_pos.value)

	let draw_vector = DEBUG.add_vector(fn() -> vessel_pos.value, fn() -> landing_pos.value, RED, "", 1)
	draw_vector.visible = true

	while (vessel.actions.brakes == false) {
		vessel_pos.value = vessel.global_position
		landing_pos.value = landing_point - vessel_pos.value
		draw_vector.start = vessel_pos.value
		draw_vector.set_vector_provider(fn() -> landing_pos.value)

		yield()
	}
}

My red vector is clearly not pointing at the runway! How should I do that please? 20240313003218_1

PhilouDS avatar Mar 12 '24 23:03 PhilouDS

I think the problem is that neither your plane nor the surface position is fixed in time. The only global fix point is Kerbol (and maybe even that will change with interstellar?). So if you move

const landing_point = vessel.main_body.global_surface_position(-0.63, -75.02, landing_height)

into the while loop it should work.

Here is a more compact version, I tested with a rover:

use { Vessel } from ksp::vessel
use { CONSOLE, RED, WHITE } from ksp::console
use { sleep } from ksp::game
use { DEBUG } from ksp::debug

pub fn main_flight(vessel: Vessel) -> Result<Unit, string> = {
    const geo = vessel.main_body.geo_coordinates(-0.64, -75.02)
    DEBUG.clear_markers()

    DEBUG.add_billboard(fn() -> vessel.global_position + 15 * vessel.global_facing.right_vector, 
        fn() -> $"Lat: {vessel.geo_coordinates.latitude:N3}\nLon: {vessel.geo_coordinates.longitude:N3}", WHITE, 8)
    DEBUG.add_vector(fn() -> vessel.global_position, fn() -> geo.global_altitude_position(geo.terrain_height + 100) - vessel.global_position, RED, "", 1)

    sleep(100000)
}

Though I noticed that geo.global_altitude_position(geo.terrain_height) is several meters below the ground, that's why the + 100 ... there might be a bug in the .terrain_height calculation.

Another alternative would be to do the calculation with normal vectors in the the main_body.body_frame (i.e. the rotating reference frame of the planet):

use { Vessel } from ksp::vessel
use { CONSOLE, RED, WHITE } from ksp::console
use { sleep } from ksp::game
use { DEBUG } from ksp::debug

pub fn main_flight(vessel: Vessel) -> Result<Unit, string> = {
    const body_frame = vessel.main_body.body_frame

    const height = vessel.main_body.terrain_height(-0.64, -75.02)
    const surf_pos = vessel.main_body.global_surface_position(-0.64, -75.02, height + 100).to_local(body_frame)

    DEBUG.clear_markers()

    DEBUG.add_vector(fn() -> vessel.global_position, fn() -> (surf_pos - vessel.global_position.to_local(body_frame)).to_global(body_frame), RED, "", 1)

    sleep(100000)
}

untoldwind avatar Mar 13 '24 07:03 untoldwind

Thank you. The first script works well. I used a while loop because I didn't know that the vector was updated even without loop. That's great!

PhilouDS avatar Mar 13 '24 10:03 PhilouDS

That is the reason why DEBUG.add_vector takes callback functions as arguments and not just vectors. The callbacks are run with every frame-update just before the actual 3D-line is added to the game-scene.

untoldwind avatar Mar 13 '24 11:03 untoldwind

In 0.5.6.2 the terrain_height should be correct. Internally there is an additional scenery offset, which (I guess) is referring to Buildings etc.

untoldwind avatar Mar 13 '24 20:03 untoldwind

This issue is stale because it has been open for 60 days with no activity.

github-actions[bot] avatar May 13 '24 02:05 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar May 27 '24 02:05 github-actions[bot]