Sign in to follow this  
programmermattc

Problem with Screen Position and Rotation (XNA)

Recommended Posts

I am having an issue in an asteroids-style game where theplayer will pass the screen borders and the code makes them appear on the opposite screen edge (for example, passing too far to the right makes you appear on the left).

However, it seems like the movement vector I use to define which was to go gets all messed up until I rotate and fly around for awhile. Here is some of my ship rotation code where I believe the issue is happening:


private void UpdateRotationData(KeyboardState kbs)
{
if (kbs.IsKeyDown(Keys.A))
{
this.Rotation -= MathHelper.ToRadians(5);
float translatedRot = MathHelper.ToRadians(MathHelper.ToDegrees
(this.Rotation) + 240);
Matrix transform = new Matrix();
transform = Matrix.CreateTranslation(new Vector3(this.Position, 1.0f))
* Matrix.CreateRotationZ(translatedRot);
_movementDir = Vector2.Transform(_movementDir, transform);
_movementDir.Normalize();
}

if (kbs.IsKeyDown(Keys.D))
{
this.Rotation += MathHelper.ToRadians(5);
float translatedRot = MathHelper.ToRadians(MathHelper.ToDegrees
(this.Rotation) + 240);
Matrix transform = new Matrix();
transform = Matrix.CreateTranslation(new Vector3(this.Position, 1.0f))
* Matrix.CreateRotationZ(translatedRot);
_movementDir = Vector2.Transform(_movementDir, transform);
_movementDir.Normalize();
}
}


The 240 offset in the rotation is compensation for the movement vector (which, while typing this I realized a solution for).

Does anyone see why, if the this.Position changes to the other side of the screen, the transformation of this vector wouldn't work properly?

Share this post


Link to post
Share on other sites
I only skimmed your code so my reply may not be particularly relevant, but make sure you're combining your translation and rotation transforms in the right order, and also make sure that you're treating the movement vector as a vector rather than a point when you transform it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this