Convert vector to angle?

Posts: 1,487
Joined: 2002.09
Post: #31
OneSadCookie Wrote:On a modern processor, a sin/cos lookup table with any accuracy is almost certainly more expensive than just calculating the sin or cosine directly.

Cache coherency?

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Posts: 1,403
Joined: 2005.07
Post: #32
Nevada Wrote:Whoa there. Why are we squaring everything? The square of a ratio is not identical to the ratio itself. I realize we're trying to avoid the overhead of the expensive sqrt call, but if you square the ratio, you'll have to take the square root anyway. Hog had it right...

Oh yeah. Sin, cos, and atan2 are all very expensive (performance-wise) function calls and are very prone to rounding errors. So if we can avoid all that with one call to sqrt, the routine should perform better.

correct a/b is not equal to (a*a)/(b*b)... but no one said it was.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #33
Skorche Wrote:Cache coherency?

Memory access latency (if your lookup isn't in L1 you're going to pay as much in stall cycles as you would in just calculating the thing).

Possibly particularly true on x86 where there is an fsincos instruction that can be used.
Quote this message in a reply
Posts: 100
Joined: 2006.05
Post: #34
unknown Wrote:
    vector.x = new_world_dst.x - player.x;
    vector.y = new_world_dst.y - player.y;
    float sqd = vector.x * vector.x + vector.y * vector.y;
    if(sqd > 0.00001f) {
        float scale = SPRITE_SPEED2/sqd;
        position.x += vector.x*vector.x*scale;
        position.y += vector.y*vector.y*scale;

Correct me if I'm wrong, but it seems this translates to:

x = x + (dx*dx)*(speed*speed)/(dx*dx+dy*dy) = x + [dx*speed/sqrt(dx^2 + dy^2)]^2

It should simply be:

x = x + dx*speed/sqrt(dx^2 + dy^2)

Unfortunately, the former wouldn't work even if you added it to x^2 instead, because of it's inherent non-linearity.
Quote this message in a reply
Posts: 1,140
Joined: 2005.07
Post: #35
I think he was joking due to the incorrectness of the previous post.
Quote this message in a reply
Posts: 1,403
Joined: 2005.07
Post: #36
No it wasn't a joke but it mistake.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Formula for converting angle to vector? komirad 2 14,812 Jul 29, 2011 07:29 AM
Last Post: ThemsAllTook
  Question Regarding the Reflect Angle of a Transition iBaby 3 5,766 Apr 27, 2010 03:15 PM
Last Post: JustinFic
  ending location from angle and speed Kazooless 5 7,388 Apr 3, 2009 02:40 PM
Last Post: Gillissie
  Angle between two points? Graphic Ace 6 8,428 Nov 8, 2008 12:11 PM
Last Post: macnib
  calculating X and Y coordinates w/ an angle and distance ferum 13 22,138 Jun 25, 2008 10:53 PM
Last Post: rosenth