IGNORED

Computing velocity vectors

Recommended Posts

I had a need to build a velocity vector table to translate disc directions into the X and Y velocity components, so I built a simple Perl script to generate it for me.  I am posting it here in case anybody else can make use of it.

For an example of how such a table can be used, check out @intvnut's excellent example program "accel.bas" illustrating how to implement sub-pixel movement with basic acceleration physics.  You can find the "accel.bas" program in the "contrib" folder within the IntyBASIC distribution package, or in the "Contributions" folder of the IntyBASIC SDK.

The script takes three arguments:

• -m (--max):  The maximum speed, in pixels per second.  This argument supports fractional speeds and defaults to 1.00 p/s.
• -s (--scale):  The binary fixed-point scale of the vector -- that is, the total number of significant bits.  The default is 16 bits.
• -p (--precision):  The binary fixed-point precision of the vector -- that is, the number of fractional bits.  The default is 8 bits.

When run without any arguments, the script will use the default of 1.00 pixel/second maximum speed and generate a table of Q8.8 signed values.  A "Q8.8" value means a 16-bit value in which the upper 8 bits represent the integer portion, and the lower 8 bits represent the fractional portion.

So, a value of 1.00 (one pixel per second) would appear in the table as \$0100 (binary: &000000100000000), and a value of 1.50 (one and 1/2 pixels per second) would show up in the table as \$0180 (binary: &0000000110000000).  Here's an illustration of what this means:

```          Integer Part                       Fractional Part
.___________________________.       .___________________________.
/                             \     /                             \
+---+---+---+---+---+---+---+---+   +---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | . | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |  =  &0000000110000000 : \$0180
+---+---+---+---+---+---+---+---+   +---+---+---+---+---+---+---+---+
^
|
Binary Fixed-Point```

The script will saved the output to a file named "vector-data.bas".

To generate a similar table to the one used in the "accel.bas" example program*, use the following arguments:

`compute_vectors.pl -m 1.99 -s 8 -p6`

That will create a table using a maximum speed of 1.99 and values in Q2.6 notation -- that is, 2 integer bits and 6 fractional bits.  This allows it to fit in a single 8-bit variable.  The output table will look like this:

```'' ----------------------------------------------
''  Table of velocity vectors computed using the
''  following configuration:
''      Maximum Speed (pix/sec): 1.99
''      Fixed-Point Precision:   Q2.6
'' ----------------------------------------------
ObjectVelocityTable:
'         X      Y        Dir    Velocity
'       -----  -----     ------ ------------
Data    \$0000, \$FF81    '  0:  (+0.00, -1.99)
Data    \$0030, \$FF8B    '  1:  (+0.76, -1.84)
Data    \$005A, \$FFA6    '  2:  (+1.41, -1.41)
Data    \$0075, \$FFD0    '  3:  (+1.84, -0.76)
Data    \$007F, \$0000    '  4:  (+1.99, +0.00)
Data    \$0075, \$0030    '  5:  (+1.84, +0.76)
Data    \$005A, \$005A    '  6:  (+1.41, +1.41)
Data    \$0030, \$0075    '  7:  (+0.76, +1.84)
Data    \$0000, \$007F    '  8:  (+0.00, +1.99)
Data    \$FFD0, \$0075    '  9:  (-0.76, +1.84)
Data    \$FFA6, \$005A    ' 10:  (-1.41, +1.41)
Data    \$FF8B, \$0030    ' 11:  (-1.84, +0.76)
Data    \$FF81, \$0000    ' 12:  (-1.99, +0.00)
Data    \$FF8B, \$FFD0    ' 13:  (-1.84, -0.76)
Data    \$FFA6, \$FFA6    ' 14:  (-1.41, -1.41)
Data    \$FFD0, \$FF8B    ' 15:  (-0.76, -1.84)```

* Note that some of the values may not match exactly @intvnut's original table due to rounding errors.

Attached find the script and a sample output file with the above table.

-dZ.

Edited by DZ-Jay
Share on other sites

Many thanks go to @intvnut for the "accel.bas" sample program.  My own implementation of acceleration was actually based on (read: ripped off from) it.  I created the script because I wanted to scale the values differently, using 16 bits instead of 8, and support higher velocities; but I took the principles and the insight from his code.

-dZ.

Edited by DZ-Jay

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.