Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | keke | |
Old Version | Published before Godot 3 was released. |
Hello, I’m brand new to godot, and Im working on a 2D space game. I’ve gone through the docs and set up a spaceship which I can fly and a planet. I have two questions, one easy and one complicated
Easy question: How do I apply_impulse relative to the ships rotation? i.e. when I thrust I want the ship to go the direction it’s pointing. Right now it uses some dumb trig I did, so it works fine, but Im sure “local axes” are built into godot somehow.
Hard question:
Here’s my gravity code (runs in _integrate_forces under the ship):
var distance=get_pos().distance_to(planet.get_pos())
var gravity=(self.get_pos()-planet.get_pos()).normalized()
gravity*=(-(G*planet_mass*get_mass())/pow(distance,2))
apply_impulse(Vector2(0,0),gravity)
(Basically, I get the distance to the planet, get a normalized vector thats points towards the planet, multiply/account for the inverse square rule, and apply an impulse with that vector)
My problem is this: I want to write a function that puts the ship in a perfect circular orbit (not with the thrusters - just magically, for when the scene starts). Google tells me that orbital speed is . Here’s the function so far:
func make_orbit():
var distance=self.get_pos().distance_to(planet.get_pos())
var magnitude=sqrt( (G*(get_mass()+planet_mass))/distance )
var velo=Vector2(0,-magnitude)
set_linear_velocity(velo)
(You’ll notice that this doesn’t take into account where the ship is around the planet - i.e. the angle - so right now this only works when the angle from the ship the the planet is 0 - so on the left or on the right, in line with the planet. I’m testing with the ship directly to the left of the planet, so this can’t be the problem)
For some reason, this velocity is way too slow, and the ship hits the planet. Any idea why?