Determining speed in X/Y

Recommended Posts

I have an asteroids style game in the making and i'm wondering if there is a better way to go about some things: x_new_speed -= float(cos(self.facing * NEW_PI) * (self.is_accel * self.accel_rate * time_multiplier)) y_new_speed += float(sin(self.facing * NEW_PI) * (self.is_accel * self.accel_rate * time_multiplier)) This accelerates the ship according to the direction its facing. To determine the ships present speed I add x_new_speed and y_new_speed then divide by two but that doesnt sound right to me. If I'm traveling along x at 100 and y by 1 my math gives me roughly 50 but it's obviously moving faster then that. What is a better way to go about determining speed? Thx in advance. -Ahl

Share on other sites
In other words (if I understand correctly), you take the average of the x- and y- components to get the speed of the object?

I believe that speed is rather:

square_root(x_component2 + y_component2).

This is essentially the Pythagorean Theroem - the two components can be seen as the two perpendicular sides of a right-angled triangle, with the speed being given by the length of the hypotenuse.

(This is also a matter of vector mathematics; if you haven't yet encountered that, then I suggest that you look into it - it can be quite useful for these things, I find.)

Share on other sites
Now that I see it that makes total sense. I don't know why I didn't think of that.

Thank you.

Share on other sites
'Tis my pleasure - good luck with your project. ^_^

Create an account

Register a new account

• Forum Statistics

• Total Topics
628357
• Total Posts
2982227

• 10
• 9
• 10
• 24
• 11