Monday, July 29, 2019

python - Finding vectors with two points



We're are trying to get the direction of a projectile but we can't find out how


For example:


[1,1] will go SE


[1,-1] will go NE


[-1,-1] will go NW


and [-1,1] will go SW


we need an equation of some sort that will take the player pos and the mouse pos and find which direction the projectile needs to go.


Here is where we are plugging in the vectors:


def update(self):


self.rect.x += self.vector[0]
self.rect.y += self.vector[1]

Then we are blitting the projectile at the rects coords.



Answer



To compute a direction you just need subtraction, and ideally normalisation.


self.vector = (mouse.position - player.position).normalize()

Note that your loop update should take the velocity and time between two frames into account:


self.rect.x += speed * timestep * self.vector[0]

self.rect.y += speed * timestep * self.vector[1]

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...