Question 477106: I'm an independent video game programmer 10 years out of high school, and while working on an AI I've come across an equation I need to solve that I can't remember how to work with. I used a website to get an answer, but I'd like to see the steps necessary to come across a solution.
The game I'm programming is a racing game. Assuming that a vehicle turns more slowly at higher speeds, I want to find the speed at which a vehicle could turn a given number of degrees in a given amount of time. Here's the equation I need to solve for speed or for the throttle, since either of those values will allow the AI to determine how much it needs to slow down to make a turn.
turnTime * turnRate = angle
and here is an explanation of the variables, and possible substitutions.
turnTime = the amount of time available in which to turn. It varies with the vehicle's speed, and speed is what I'm solving for, so I can't say it's a known quantity.
turnTime = distance/speed
Distance is a known quantity, gotten from analyzing the upcoming sections of the race track.
turnRate = handling * (2 - throttle)
The handling represents how many degrees the vehicle can turn in one second. Handling is a static attribute for a vehicle. It may change from vehicle to vehicle, but the same vehicle always has the same handling. Therefore handling is a known quantity.
throttle = speed/maxSpeed
Throttle is always a value between 0 and 1, and expresses the vehicle's current speed as percentage of the vehicle's maximum speed. maxSpeed is a known quantity like handling.
angle = how far the vehicle needs to turn in the specified time. This is a known quantity determined from analyzing the upcoming sections of the race track. Like handling, it is in degrees.
Here's what I've done so far:
turnTime * turnRate = angle
distance/speed * handling * (2 - throttle) = angle
subsituted distance/speed for turnTime and handling * (2 - throttle) for turnRate
distance/speed * handling * (2 - speed/maxSpeed) = angle
substitued speed/maxSpeed for throttle
disance/speed * (2 - speed/maxSpeed) = angle/handling
multiply both sides by 1/handling
2 - speed/maxSpeed = angle/handling * speed/distance
multiply both sides by speed/distance
And this is where I'm stuck. I'm not sure how to solve for speed from here. The answer I got from QuickMath.com is:
speed = (2 * distance * handling * maxSpeed) / (angle * maxSpeed + distance * handling)
I'd like to know how it arrived at that solution.
It is possible for the angle of the turn to be too large for the vehicle to ever successfully make, I know, but in that case I believe I'd get a negative speed from this calculation. I can account for that pretty easily in my code.
Answer by bucky(2189) (Show Source):
You can put this solution on YOUR website! Sounds like a fun exercise. I'm not sure that I followed it all entirely, but from a purely math standpoint I can get you to where you want to go.
.
Let's begin with where you got to:
.

.
What I did next is multiplied the distance/speed by each of the two quantities in the parentheses. This results in:
.

.
Notice that after the minus sign you are multiplying two fractions, one having "speed" in the denominator and one having "speed" in the numerator. Since one is in the denominator and one is in the numerator, they cancel and the equation becomes:
.

.
next move the - distance/maxSpeed to the other side of the equation by adding +distance/maxSpeed to both sides:
.

.
Observe that on the left side the terms - distance/maxSpeed + distance/maxSpeed cancel each other due to their opposite signs and this reduces the equation to:
.

.
Now let's focus on the right side of this equation. Note that we have two fractions with different denominators. We can give them both a common denominator by multiplying the first fraction by maxSpeed/maxSpeed. This is the same as multiplying the first fraction by 1. Similarly multiply the second fraction by handling/handling. This is as follows:
.

.
Doing the multiplications in the two fractions on the right side results in:
.

.
Since the two fractions on the right side have a common denominator, we can now add the numerators together and place them over the common denominator to get:
.

.
Now, both sides consist of a fraction. We can flip both sides upside down without affecting the equality. So invert both sides and you have:
.

.
Finally, get rid of the denominator on the left side. You can do this by multiplying both sides by the denominator on the left side. That is to say, multiply both sides by 2*distance. On the left side this multiplier cancels with the denominator and you are just left with "speed." On the right side, this multiplier of 2*distance becomes part of the numerator as shown below:
.

.
And, unless I'm mistaken, this is the answer that you got from QuickMath.com ... at least I hope so.
.
Hopefully this helps you get on with designing your game. Good luck!!!
.
|
|
|