vdub_bobby Posted April 30, 2008 Share Posted April 30, 2008 If an object has it's overall velocity described by X-velocity and Y-velocity, is there a quick-n-dirty way to get the direction of the overall velocity? I.e., I want the direction of the vector, I don't care about the magnitude. I just need a really rough estimate; within ~30 degrees. I can take the arctan of Vy/Vx, but that involves division... Quote Link to comment Share on other sites More sharing options...
gauntman Posted April 30, 2008 Share Posted April 30, 2008 Depending on the magnitude of the X and Y velocities, the fastest method is probably a look-up table. For instance, if your velocity is +/-7 in each direction, you can simply shift one of the axis 4 bits, and OR the other vector. Then use a 256 byte (precalculated) look-up table. lda dx asl a asl a asl a asl a ora dy tax lda angle,x Note: It should be possible to still use a 256 byte look-up table with velocities up to 15, taking advantage of reflection. Quote Link to comment Share on other sites More sharing options...
vdub_bobby Posted April 30, 2008 Author Share Posted April 30, 2008 (edited) My velocities will be 16-bit, but using the most significant 4-bits will probably be good enough. Thanks! Edited April 30, 2008 by vdub_bobby Quote Link to comment Share on other sites More sharing options...
Cybergoth Posted May 2, 2008 Share Posted May 2, 2008 Didn't Rob Fulop recently post some approximation for this (or similar ) problem in one of the Actionauts threads? Quote Link to comment Share on other sites More sharing options...
vdub_bobby Posted May 2, 2008 Author Share Posted May 2, 2008 His was a fast fast distance calculation, something like X + 3/8 Y when Y > X. Reverse for X > Y. Or something similar. I'll be using that as well, most likely. Quote Link to comment Share on other sites More sharing options...
+SpiceWare Posted May 3, 2008 Share Posted May 3, 2008 that would have been handy for MM. Quote Link to comment Share on other sites More sharing options...
supercat Posted May 4, 2008 Share Posted May 4, 2008 (edited) I just need a really rough estimate; within ~30 degrees. Getting it to a quadrant is easy. An octant isn't much harder, though you might want to scale X and Y first (x*5 and y*3 for best accuracy at 1x resolution, though you could use x*1 and y*2). Your next subdivision could be done reasonably accurately by seeing whether the larger value was more or less than twice as big as the smaller. That would divide the circle into 16 parts. They wouldn't all be 22.5 degrees, unfortunately--they'd be 26.6 and 18.4 degrees, but that may be good enough, especially given the simplicity. Maybe do something like: ; Assumes dx and dy are usable as scratchpad lda dx cmp #128 ror temp bpl nonegdx eor #255 sta dx nonegdx: lda dy cmp #128 ror temp bpl nonegdy eor #255 sta dy nonegdy: lda dx cmp dy ; carry if dx >= dy ror temp bmi noswap; branch if dx >= dy ldx dy sta dy stx dx ; Now dx >= dy noswap: lda dx lsr cmp dy ror temp ; The top four bits of temp now define one of 16 directions (not in order). ; Shift right four bits and do a table lookup. Edited May 4, 2008 by supercat Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.