Trig and the Infamous
- Posted by Larry Gregg <lgregg at BIG12.METROBBS.COM> May 05, 1998
- 655 views
= > speed = sqrt((xv*xv)+(yv*yv)) > if speed > maxspeed then > xv = xv * maxspeed / speed > yv = yv * maxspeed / speed > end if Let us find the speed using the new xv and yv, call them xv2 and yv2. speed2 = sqrt(xv2*xv2 + yv2*yv2) = sqrt(xv*maxspeed/speed * xv*maxspeed/speed + yv*maxspeed/speed * yv*maxspeed/speed) = sqrt((maxspeed/speed)**2 * (xv**2 + yv**2)) = (maxspeed / speed) * sqrt(xv**2 + yv**2) but sqrt(xv**2 + yv**2) is speed as defined above, so speed2 = (maxspeed / speed) * speed = maxspeed In your if statement above, try replacing xv and yv with: theta = arctan(yv / xv) xv = maxspeed * cos(theta) yv = maxspeed * sin(theta) The nice thing here is that this will work for arbitrary angle and speed. So, if you are traveling at some speed s, and say angle = pi / 4 radians, and change the angle to pi / 8 radians, but your speed remains constant, you can just find xv and yv as: xv = s * cos(pi / 8) yv = s * sin(pi / 8) Good stuff. Hope I did this all correctly. It has been a while. ------------------------------------------------------------------ Larry Gregg