This project has moved and is read-only. For the latest updates, please go here.

# Rotation around a point

 Topics: User Forum Wiki Link: [discussion:29679]
 chrisaswain Jun 15, 2008 at 5:39 PM Pardon my rusty math in advance, but I'm trying to figure out how to calculate the coordinates of a point in a Body after the body has been rotated X degrees.  I'm writing a simple top view tank game.  I have a tank sprite that has a cannon that fires bullets.  I need to fire the bullets from the end of the cannon, but as the user drives the cannon around the screen, they are rotating it to go in different direction.  So, I need to be able to get the new coordinates for the end of the cannon as the tank rotates.  Since my math is rusty, I figured I'd ask the experts on the forum. Carlsen Jun 15, 2008 at 6:38 PM The position and orientation of the body defines a local 2d coordinate system. You want to store the position of your cannon mount in this local coordinate system. Then, to get the cannon position you simply write: Vector2 cannonPos; // in local coord system of body Body body; ... ... {     Vector2 worldCannonPos = Vector2.Transform(body.GetBodyMatrix(), cannonPos); //GetBodyMatrix() is the "world" matrix of the body } chrisaswain Jun 15, 2008 at 11:49 PM chrisaswain wrote: Pardon my rusty math in advance, but I'm trying to figure out how to calculate the coordinates of a point in a Body after the body has been rotated X degrees.  I'm writing a simple top view tank game.  I have a tank sprite that has a cannon that fires bullets.  I need to fire the bullets from the end of the cannon, but as the user drives the cannon around the screen, they are rotating it to go in different direction.  So, I need to be able to get the new coordinates for the end of the cannon as the tank rotates.  Since my math is rusty, I figured I'd ask the experts on the forum.  I tried your solution above, but it didn't accomplish what I need.  I found a solution that works for me.  See the following code.... the keyword this refers to the tank object.   Bullet bullet = new Bullet(PhysicsSimulatorObject, container); this.bulletCount++; bullet.Expired += new EventHandler(bullet_Expired); this.gamePlayArea.SpritesEnvironment.Add(bullet); Vector2 bulletPosition = this.BodyObject.Position; bulletPosition.Y -= (float)this.Height/2 + (float)bullet.Height; float radians = this.BodyObject.Rotation; fsm.Matrix translateToOrigin; fsm.Matrix.CreateTranslation(-this.BodyObject.Position.X, -this.BodyObject.Position.Y, 0.0f, out translateToOrigin); fsm.Matrix rotateAroundZ = fsm.Matrix.CreateRotationZ(radians); fsm.Matrix translateFromOrigin; fsm.Matrix.CreateTranslation(this.BodyObject.Position.X, this.BodyObject.Position.Y, 0.0f, out translateFromOrigin); fsm.Matrix finalMatrix; fsm.Matrix.Multiply(ref translateToOrigin, ref rotateAroundZ, out finalMatrix); fsm.Matrix.Multiply(ref finalMatrix, ref translateFromOrigin, out finalMatrix); Vector2 rotatedVector = Vector2.Transform(bulletPosition, finalMatrix); bullet.BodyObject.Position = rotatedVector; container.Children.Add(bullet); Vector2 bulletThrust = new Vector2(-this.BodyObject.GetBodyMatrix().Up.X, -this.BodyObject.GetBodyMatrix().Up.Y); bulletThrust *= bulletThrustMagnitude; bullet.BodyObject.ApplyForce(bulletThrust); bullet.Update();