Hi,

I’m trying to draw a line from a spaceship to a planet, but before I draw the line, I scale it down a bit. The problem is that even when the planet moves, the line isn’t facing the spaceship anymore. Can anyone fix this?

Thanks

### Attachments

gravitySimulator.blend (206 KB)

Your code confuses me, but as far as I can tell you’re calculating the vector from the origin to the spaceship, rather than from the planet to the spaceship. Then you draw a line from the planet to the end of the vector. So it seems to me the drawLine function is fine, but your vector calculation is wrong.

 here’s a modified version of gravitySimulator.py:- though I seem to have accidentally normalized the vector somewhere in the script, I’ve only just found out about the Vector() function and I don’t quite have the gist of it yet. I compensated by multiplying the result of the getVectTo with getDistTo- even though getVectTo should already be the proper length. Sorry for the convolution.

``````
class GravitySimulator(object):
def __init__(self, planet, spaceship):
self.planet = planet
self.spaceship = spaceship
print "Initialised."

def cycle(self):
import Rasterizer
from Mathutils import Vector
import Mathutils
factor = .6

vector = Vector(self.planet.getVectTo(self.spaceship.worldPosition)[1]) * self.planet.getDistanceTo(self.spaceship) * factor

Rasterizer.drawLine(self.planet.worldPosition, vector + Vector(self.planet.worldPosition), [0.0, 1.0, 0.0])

``````

You not only have to get the vector from the planet to the spaceship, for the sake of the DrawLIne function you also have to add the planet’s position to the vector.

Well, vectors are not exactly my strong point. Forgive me, I want trying to draw a line from the planet, to the spaceship, but have the line not fully reach the spaceship based on a factor variable, which stretches it. Any ideas?

I edited my previous post with the solution.

Here’s how I would get a vector from one object to another:

``````
from_vec = Vector(from_object.worldPosition)
to_vec = Vector(to_object.worldPosition)

draw_vec = to_vec - from_vec

``````

Then you can pass it to draw line very simply:

``````
draw_vec.magnitude *= 0.6 # or whatever factor you want
Rasterizer.drawLine(from_vec, from_vec + draw_vec, color)

``````

The fact that “getVectTo” returns a normalized direction vector is ridiculous - this function should simply return the actual vector, and then we wouldn’t need the “getDistanceTo” function.

… Any of the devs listening?

Hmm, I really need to get used to working with vectors rather than lists, I forgot you can just subtract them.

All python classes upwards of python 2.2 are automatically subclasses of object, you do not need to specify it in the class declaration as has been done in the GravitySimulator class.

Many thanks for all your help! Case closed. :yes: