Skip to main content
added 284 characters in body
Source Link
Theraot
  • 28.3k
  • 4
  • 55
  • 83

Oxy is correct that parenting is the easy way. And, yes, I'm aware I'm answering years later. Yet, if that is not convinient, we can still do this.

The KinematicBody2D (which I'll call ship) and the StaticBody2D (which I'll call planet) has their own transforms:

var ship_transform := ship.global_transform
var planet_transform := planet.global_transform

We are looking for a relative_transform, such that:

planet_transform * relative_transform = ship_transform

In other words:

var relative_transform := planet_transform.affine_inverse() * ship_transform

Or if you prefer:

var relative_transform := planet.global_transform.affine_inverse() * ship.global_transform

Once you have that, you can rotate the planet… Consider using a KinematicBody2D for the planet… Anyway, for example:

planet.rotation_degrees += somavelue

Which would change the transform of the planet. In fact, if you prefer, you could work with the transform directly:

planet_transform = planet_transform * Transform2D.IDENTITY.rotated(rad2deg(somevalue))
planet.global_transform = planet_transform

This also gives you the option to not set planet.global_transform, so we would be doing this as if it rotated. You would probably want to keep track of the rotation elsewhere in that case.

And then we can computed the updated transform for the ship:

ship.global_transformship_transform = planet.global_transformplanet_transform * relative_transform
ship.global_transform = ship_transform

Notice that if the planet.global_transformplanet_transform didn't change, ship.global_transformshipl_transform would not change either. Because the planet.global_transformplanet_transform and the   planet.global_transformplanet_transform.affine_inverse() (part of relative_transform) cancel each other out.

Oxy is correct that parenting is the easy way. And, yes, I'm aware I'm answering years later. Yet, if that is not convinient, we can still do this.

The KinematicBody2D (which I'll call ship) and the StaticBody2D (which I'll call planet) has their own transforms:

var ship_transform := ship.global_transform
var planet_transform := planet.global_transform

We are looking for a relative_transform, such that:

planet_transform * relative_transform = ship_transform

In other words:

var relative_transform := planet_transform.affine_inverse() * ship_transform

Or if you prefer:

var relative_transform := planet.global_transform.affine_inverse() * ship.global_transform

Once you have that, you can rotate the planet… Consider using a KinematicBody2D for the planet… Anyway, for example:

planet.rotation_degrees += somavelue

Which would change the transform of the planet. And then we can computed the updated transform for the ship:

ship.global_transform = planet.global_transform * relative_transform

Notice that if the planet.global_transform didn't change, ship.global_transform would not change either. Because the planet.global_transform and the planet.global_transform.affine_inverse() (part of relative_transform) cancel each other out.

Oxy is correct that parenting is the easy way. And, yes, I'm aware I'm answering years later. Yet, if that is not convinient, we can still do this.

The KinematicBody2D (which I'll call ship) and the StaticBody2D (which I'll call planet) has their own transforms:

var ship_transform := ship.global_transform
var planet_transform := planet.global_transform

We are looking for a relative_transform, such that:

planet_transform * relative_transform = ship_transform

In other words:

var relative_transform := planet_transform.affine_inverse() * ship_transform

Once you have that, you can rotate the planet… Consider using a KinematicBody2D for the planet… Anyway, for example:

planet.rotation_degrees += somavelue

Which would change the transform of the planet. In fact, if you prefer, you could work with the transform directly:

planet_transform = planet_transform * Transform2D.IDENTITY.rotated(rad2deg(somevalue))
planet.global_transform = planet_transform

This also gives you the option to not set planet.global_transform, so we would be doing this as if it rotated. You would probably want to keep track of the rotation elsewhere in that case.

And then we can computed the updated transform for the ship:

ship_transform = planet_transform * relative_transform
ship.global_transform = ship_transform

Notice that if the planet_transform didn't change, shipl_transform would not change either. Because the planet_transform and   planet_transform.affine_inverse() (part of relative_transform) cancel each other out.

Source Link
Theraot
  • 28.3k
  • 4
  • 55
  • 83

Oxy is correct that parenting is the easy way. And, yes, I'm aware I'm answering years later. Yet, if that is not convinient, we can still do this.

The KinematicBody2D (which I'll call ship) and the StaticBody2D (which I'll call planet) has their own transforms:

var ship_transform := ship.global_transform
var planet_transform := planet.global_transform

We are looking for a relative_transform, such that:

planet_transform * relative_transform = ship_transform

In other words:

var relative_transform := planet_transform.affine_inverse() * ship_transform

Or if you prefer:

var relative_transform := planet.global_transform.affine_inverse() * ship.global_transform

Once you have that, you can rotate the planet… Consider using a KinematicBody2D for the planet… Anyway, for example:

planet.rotation_degrees += somavelue

Which would change the transform of the planet. And then we can computed the updated transform for the ship:

ship.global_transform = planet.global_transform * relative_transform

Notice that if the planet.global_transform didn't change, ship.global_transform would not change either. Because the planet.global_transform and the planet.global_transform.affine_inverse() (part of relative_transform) cancel each other out.