Wednesday, December 19, 2018

Why are my 2D positions "off" after adding a camera transformation in XNA?


My game's source is on github.


I am following this tutorial to create a 2D camera. After I adding the transformation to the SpriteBatch, all of my positions seem to be "off" when I move the ship (and hence camera) away from the screen center.


Any idea what might be wrong?



Answer



Since my other answer was incorrect, I deleted it, pulled your source and traced down the actual solution.



The issue is that your particle renderer never gets your camera's transform matrix. Here's how you can fix it:


Update your ParticleManager.Draw method to be the following (very slight change, just the passing of the matrix:


public void Draw(Matrix transform)
{
foreach (KeyValuePair effectPair in particleEffects)
{
particleRenderer.RenderEffect(effectPair.Value, ref transform);
}
}


Then within your ActionScreen.Draw method, make sure to pass along the transformation matrix:


particleManager.Draw(cam.get_transformation(game.GraphicsDevice));

You might want to grab the matrix from the camera earlier in this method, so you don't end up calculating it more than once.


Now comes the annoying part - this alone didn't work for me. I'm guessing it's a bug in the Mercury Particle Engine 3.1. I ended up pulling the source of the current version of Mercury (from their trunk) and compiled it myself. After dropping those assemblies in, the trails and explosions were placed correctly.


For reference, I used Mercury Particle Engine's changeset 87447.


I just looked into the mouse aiming issue and found a quick fix (may want to modify it long-term). In Player.Update, change this:


direction = position - mousePosition;

To this:



direction = new Vector2(Game1.SCREEN_WIDTH * 0.5f, Game1.SCREEN_HEIGHT * 0.5f) - mousePosition;

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...