Sign in to follow this  
Ahl

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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this