The code is as follows

function RotateGun(gun:Bitmap, x:Number, y:Number):void {

gun.transform.matrix = new Matrix(x, y, -y, x,

fGunX + y * fSize / 2, fGunY - x * fSize / 2);

}

The rotation is calculated from 'x' and 'y', the components of a normalised direction vector. These are passed to the first four parameters of the matrix, as shown, and used to offset its position it so it rotates around the midpoint of its shortest length.

I've updated my ballistics app (again embedded below) with this code rotating two guns, one for each ball. Note that the above works for an object normally aligned along the 'x' axis and so horizontally, which is how I made the gun. The normalised direction vector is simply the velocity divided by the speed.

## No comments:

## Post a Comment