1. Re: Trig and the ...

Pete Eberlein wrote:

> speed = sqrt((xv*xv)+(yv*yv))
> if speed > maxspeed then
>   xv = xv* maxspeed / speed
>   yv = yv * maxspeed / speed
> end if

This is nice, straight forward and accurate. The square root operation
is usually quite expensive, so I'll show you slightly faster, but more
complicated way of calculating the scaling factor with sufficient
accuracy. Instead of Pete's

    f = maxspeed / sqrt(xv*xv+yv*yv)

you can try

    vvmax = maxspeed * maxspeed     -- known, can be pre-calculated
    vv = xv*xv + yv*yv              -- speed squared
    f = (vvmax+vv) / (vv+vv)

I shall not bore you with the mathematics of it - unless, of course,
you insist. Jiri

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu